32. hstore
32.1. 插件hstore简介
hstore支持hstore类型及其相关的函数。
插件名为hstore
插件版本 V1.0
32.2. 插件hstore加载方式
使用时需要create extension hstore。
示例:
create extension hstore;
32.3. 插件hstore参数配置
无需配置任何参数。
32.4. 插件hstore使用方法
32.4.1. hstore类型
字符串的一种,可以将字符串与'=>'进行单元分割重组形成新的字符串。
示例
select ''::hstore;
hstore
--------
(1 row)
select 'a=>b'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select ' a=>b'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select 'a =>b'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select 'a=>b '::hstore;
hstore
----------
"a"=>"b"
(1 row)
select 'a=> b'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select '"a"=>"b"'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select ' "a"=>"b"'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select '"a" =>"b"'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select '"a"=>"b" '::hstore;
hstore
----------
"a"=>"b"
(1 row)
select '"a"=> "b"'::hstore;
hstore
----------
"a"=>"b"
(1 row)
select 'aa=>bb'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select ' aa=>bb'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select 'aa =>bb'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select 'aa=>bb '::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select 'aa=> bb'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select '"aa"=>"bb"'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select ' "aa"=>"bb"'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select '"aa" =>"bb"'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select '"aa"=>"bb" '::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select '"aa"=> "bb"'::hstore;
hstore
------------
"aa"=>"bb"
(1 row)
select 'aa=>bb, cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>bb , cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>bb ,cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>bb, "cc"=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>bb , "cc"=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>bb ,"cc"=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>"bb", cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>"bb" , cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>"bb" ,cc=>dd'::hstore;
hstore
------------------------
"aa"=>"bb", "cc"=>"dd"
(1 row)
select 'aa=>null'::hstore;
hstore
------------
"aa"=>NULL
(1 row)
select 'aa=>NuLl'::hstore;
hstore
------------
"aa"=>NULL
(1 row)
select 'aa=>"NuLl"'::hstore;
hstore
--------------
"aa"=>"NuLl"
(1 row)
select e'\\=a=>q=w'::hstore;
hstore
-------------
"=a"=>"q=w"
(1 row)
select e'"=a"=>q\\=w'::hstore;
hstore
-------------
"=a"=>"q=w"
(1 row)
select e'"\\"a"=>q>w'::hstore;
hstore
--------------
"\"a"=>"q>w"
(1 row)
32.4.2. hstore_to_jsonb
将hstore类型转换为jsonb类型。
语法
hstore_to_jsonb(hstore) returns jsonb。
示例
select hstore_to_jsonb('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
hstore_to_jsonb
-------------------------------------------------------------------------------------------------
{"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
(1 row)
32.4.3. hstore_to_json
将hstore转换为json类型。
语法
hstore_to_json(hstore) returns json。
示例
select hstore_to_json('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
hstore_to_json
-------------------------------------------------------------------------------------------------
{"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
(1 row)
32.5. 插件hstore卸载方法¶
不需要hstore插件只需要卸载插件即可。
示例:
drop extension hstore;
32.6. 插件hstore升级方法
hstore扩展插件通常随KingbaseES安装包一并升级。通常情况下用户无法单独升级插件。
33. http
33.1. 插件http简介
插件http是KingbaseES 的一个扩展插件。插件功能是提供数据库的http功能,允许在数据库内检索网页。
插件名为 http
插件版本 V1.4
33.2. 插件http加载方式
CREATE EXTENSION http;
33.3. 插件http的参数配置
http插件可以设置http.keepalive参数和http.timeout_msec参数。
33.3.1. http.keepalive变量
功能描述
默认情况下,每个请求都使用一个全新的连接,并确保在完成请求后关闭该连接。当扩展程序在较长的时间段内运行时,设置http.keepalive为'on'的操作减少了消耗系统资源(套接字)的机会。
高性能应用程序可能希望启用保持活动状态和连接持久性,以减少延迟并提高吞吐量。该GUC变量更改了http扩展名的行为,以尽可能长时间地保持连接。
示例
set http.keepalive = 'on';
33.3.2. http.timeout_msec变量
功能描述
默认情况下,http.timeout_msec为0。如果需要不同的超时时间,则可以使用该GUC变量以毫秒为单位进行设置。
示例
set http.timeout_msec = 200;
33.4. 插件http使用方法
33.4.1. 数据类型
33.4.1.1. UTL_HTTP.req
功能描述
使用此类型表示HTTP请求。
语法格式
CREATE TYPE UTL_HTTP.req AS(
url VARCHAR2(32767),
method VARCHAR2(64),
http_version VARCHAR2(64));
类型说明
参数 | 描述 |
|---|---|
url | http请求的URL |
method | 对URL所标识的资源执行的方法,目前仅支持GET |
http_version | 兼容性参数,无意义 |
33.4.1.2. UTL_HTTP.resp
功能描述
使用此类型表示HTTP响应。
语法格式
CREATE TYPE UTL_HTTP.resp AS(
status_code INTEGER ,
reason_phrase VARCHAR2(256),
http_version VARCHAR2(64),
content TEXT);
类型说明
参数 | 描述 |
|---|---|
status_code | web服务器返回的状态代码 |
reason_phrase | 兼容性参数,无意义 |
http_version | 兼容性参数,无意义 |
content | web服务器返回的内容 |
33.4.1.3. http_method
功能描述
在text类型上增加检查http方法名的约束。
语法格式
CREATE DOMAIN http_method AS text
CHECK (
VALUE ILIKE 'get' OR
VALUE ILIKE 'post' OR
VALUE ILIKE 'put' OR
VALUE ILIKE 'delete' OR
VALUE ILIKE 'patch' OR
VALUE ILIKE 'head'
);
33.4.1.4. http_header
功能描述
表示一个http的头信息。
语法格式
CREATE TYPE http_header AS (
field VARCHAR,
value VARCHAR
);
类型说明
参数 | 描述 |
|---|---|
field | http请求头的名字 |
value | http请求头的值 |
33.4.1.5. http_response
功能描述
表示http返回的响应。
语法格式
CREATE TYPE http_response AS (
status INTEGER,
content_type VARCHAR,
headers http_header[],
content VARCHAR
);
类型说明
参数 | 描述 |
|---|---|
status | web服务器返回的状态代码 |
content_type | web服务器返回内容的类型 |
headers | web服务器返回的http头信息 |
content | web服务器返回的内容 |
33.4.1.6. http_request
功能描述
表示http请求。
语法格式
CREATE TYPE http_request AS (
method http_method,
uri VARCHAR,
headers http_header[],
content_type VARCHAR,
content VARCHAR
);
类型说明
参数 | 描述 |
|---|---|
method | 对URL所标识的资源执行的方法 |
uri | http请求的URL |
headers | 请求头信息 |
content_type | 请求类型 |
content | 请求内容 |
33.4.2. UTL_HTTP.BEGIN_REQUEST
功能描述
这个函数开始一个新的HTTP请求。
语法格式
UTL_HTTP.BEGIN_REQUEST (
url IN VARCHAR2,
method IN VARCHAR2 DEFAULT 'GET',
http_version IN VARCHAR2 DEFAULT NULL,
request_context IN VARCHAR2 DEFAULT NULL,
https_host IN VARCHAR2 DEFAULT NULL)
RETURN UTL_HTTP.req
参数说明
参数 | 描述 |
|---|---|
url | http请求的URL |
method | 对URL所标识的资源执行的方法,目前仅支持GET |
http_version | 兼容性参数,无意义 |
request_context | 兼容性参数,无意义 |
https_host | 兼容性参数,无意义 |
**返回值说明**返回HTTP请求。
33.4.3. UTL_HTTP.SET_HEADER
功能描述
设置一个HTTP请求头。
语法格式
UTL_HTTP.SET_HEADER (
r IN OUT UTL_HTTP.req,
name IN VARCHAR2,
value IN VARCHAR2
参数说明
参数 | 描述 |
|---|---|
r | http的请求 |
name | http请求头的名字 |
value | http请求头的值 |
33.4.4. UTL_HTTP.GET_RESPONSE
功能描述
此函数用于读取http响应,目前仅支持get功能。
语法格式
UTL_HTTP.GET_RESPONSE (
r IN UTL_HTTP.req,
return_info_response IN BOOLEAN DEFAULT FALSE)
RETURN UTL_HTTP.resp
参数说明
参数 | 描述 |
|---|---|
r | http的请求 |
return_info_response | 兼容性参数,无意义 |
**返回值说明**返回HTTP响应。
33.4.5. UTL_HTTP.READ_LINE
功能描述
这个过程以文本形式读取HTTP响应体,直到到达行尾,并在调用者提供的缓冲区返回输出。如果到达HTTP响应正文的末尾,将引发NO_DATA_FOUND异常。
语法格式
UTL_HTTP.READ_LINE( r IN OUT UTL_HTTP.resp, data OUT VARCHAR2, remove_crlf IN BOOLEAN DEFAULT FALSE)
参数说明
参数 | 描述 |
|---|---|
r | http的响应 |
data | http的响应正文 |
remove_crlf | 兼容性参数,无意义 |
33.4.6. UTL_HTTP.READ_TEXT
功能描述
这个过程以文本形式读取HTTP响应体,并在调用者提供的缓冲区返回输出。如果到达HTTP响应正文的末尾,将引发NO_DATA_FOUND异常。
语法格式
UTL_HTTP.READ_TEXT( r IN OUT UTL_HTTP.resp, data OUT VARCHAR2, len IN INTEGER DEFAULT NULL)
参数说明
参数 | 描述 |
|---|---|
r | http的响应 |
data | http的响应正文 |
len | 要读取数据的最大字符数 |
注意
如果len为NULL,这个过程将读取尽可能多的输入来填充数据中分配的字符串。
33.4.7. UTL_HTTP.END_RESPONSE
功能描述
此过程结束http响应。
语法格式
UTL_HTTP.END_RESPONSE ( r IN OUT UTL_HTTP.resp)
参数说明
参数 | 描述 |
|---|---|
r | http的响应 |
33.4.8. UTL_HTTP.END_REQUEST
功能描述
此过程结束http请求。
语法格式
UTL_HTTP.END_REQUEST ( r IN OUT UTL_HTTP.req)
参数说明
参数 | 描述 |
|---|---|
r | http的请求 |
33.4.9. http_header函数
功能描述
组成一个简单数组返回。
语法格式
http_header(field VARCHAR, value VARCHAR) returns http_header
参数说明
参数名称
描述
field
指定名字
value
指定值
返回值说明
返回一个请求头。
示例
SELECT content::json->'headers'->>'Authorization' FROM http((
'GET',
'http://httpbin.org/headers',
ARRAY[http_header('Authorization','Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9')],
NULL,
NULL
)::http_request)
content
----------------------------------------------
Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
(1 row)
33.4.10. http函数
功能描述
可以进行get、post、put、delete、patch功能。
语法格式
http(request http_request) returns http_response
参数说明
参数名称
描述
request
输入的请求
返回值说明
从网页获取的响应。
示例
SELECT status,
content::json->'args' as args,
content::json->>'data' as data,
content::json->'url' as url,
content::json->'method' as method
from http(('GET', 'https://httpbin.org/anything', NULL, 'application/json', '{"search": "toto"}'));
33.4.11. http_get函数
功能描述
请求指定的页面信息,并返回响应主体。
语法格式
http_get(uri VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
返回值说明
从网页获取的响应。
示例
SELECT content FROM http_get('http://httpbin.org/ip');
content
-----------------------------
{"origin":"24.69.186.43"} +
(1 row)
33.4.12. http_post函数
功能描述
该请求会向指定的资源提交数据,请求服务器处理,如表单数据提交、文件上传等。
语法格式
http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
content
请求内容
content_type
请求类型
返回值说明
从网页获取的响应。
示例
SELECT status,
content::json->'data' AS data,
content::json->'args' AS args,
content::json->'url' AS url,
content::json->'method' AS method
FROM http_post('https://httpbin.org/anything?foo=bar','payload','text/plain');
status | data | args | url | method
--------+-----------+------------------+----------------------------------------+--------
200 | "payload" | { +| "https://httpbin.org/anything?foo=bar" | "POST"
| | "foo": "bar"+| |
| | } | |
(1 row)
33.4.13. http_put函数
功能描述
put请求会向指定资源位置上传其最新的内容,不能创建新的资源。
语法格式
http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
content
请求内容
content_type
请求类型
返回值说明
从网页获取的响应。
示例
SELECT status, content_type, content::json->>'data' AS data
FROM http_put('http://httpbin.org/put', 'some text', 'text/plain');
status | content_type | data
--------+------------------+-----------
200 | application/json | some text
33.4.14. http_patch函数
功能描述
patch请求与put请求类似都是用于资源的更新。但二者有以下不同 patch请求一般用于资源的部分更新,而put一般用于资源的整体更新。
语法格式
http_patch(uri VARCHAR, content VARCHAR, content_type VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
content
请求内容
content_type
请求类型
返回值说明
从网页获取的响应。
示例
SELECT status,
content::json->'data' AS data,
content::json->'args' AS args,
content::json->'url' AS url,
content::json->'method' AS method
FROM http_patch('https://httpbin.org/anything?foo=bar','{"this":"that"}','application/json');
status | data | args | url | method
--------+-----------------------+------------------+----------------------------------------+---------
200 | "{\"this\":\"that\"}" | { +| "https://httpbin.org/anything?foo=bar" | "PATCH"
| | "foo": "bar"+| |
| | } | |
(1 row)
33.4.15. http_delete函数
功能描述
删除资源。
语法格式
http_delete(uri VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
返回值说明
从网页获取的响应。
示例
SELECT status,
content::json->'args' AS args,
content::json->'url' AS url,
content::json->'method' AS method
FROM http_delete('https://httpbin.org/anything?foo=bar');
status | args | url | method
--------+------------------+----------------------------------------+----------
200 | { +| "https://httpbin.org/anything?foo=bar" | "DELETE"
| "foo": "bar"+| |
| } | |
(1 row)
33.4.16. http_head函数
功能描述
请求网页响应的头部信息。
语法格式
http_head(uri VARCHAR) returns http_response
参数说明
参数名称
描述
uri
标识互联网资源名称的字符串
返回值说明
从网页获取的响应。
示例
SELECT
http.status,
headers.value AS location
FROM
http_head('http://google.com') AS http
LEFT OUTER JOIN LATERAL (SELECT value
FROM unnest(http.headers)
WHERE field = 'Location') AS headers
ON true;
status | location
--------+-----------------------------------------------------------
302 | http://www.google.ch/?gfe_rd=cr&ei=ACESWLy_KuvI8zeghL64Ag
33.4.17. http_set_curlopt函数
功能描述
设置curl选项。
语法格式
http_set_curlopt(curlopt VARCHAR, value varchar) returns boolean
参数说明
参数名称
描述
curlopt
设置项
value
设置值
返回值说明
返回设置状态,为真则设置成功,为假则设置失败。
示例
-- Alter options and and reset them and throw errors
SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1');
http_set_curlopt
------------------
t
(1 row)
-- Error because proxy is not there
SELECT status FROM http_get('https://httpbin.org/status/555');
ERROR: Failed to connect to 127.0.0.1 port 1080: Connection refused
-- Still an error
SELECT status FROM http_get('https://httpbin.org/status/555');
ERROR: Failed to connect to 127.0.0.1 port 1080: Connection refused
33.4.18. http_reset_curlopt函数
功能描述
重置curl选项。
语法格式
http_reset_curlopt() returns boolean
返回值说明
返回状态,为真则设置成功,为假则设置失败。
示例
SELECT http_reset_curlopt();
http_reset_curlopt
--------------------
t
(1 row)
-- Now it should work
SELECT status FROM http_get('https://httpbin.org/status/555');
status
--------
555
(1 row)
33.4.19. http_list_curlopt函数
功能描述
打印已经设置的curl选项。
语法格式
http_list_curlopt() returns setof(curlopt text, value text)
返回值说明
返回已经设置的curl选项。
示例
SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1');
http_set_curlopt
------------------
t
(1 row)
select http_list_curlopt();
http_list_curlopt
---------------------------
(CURLOPT_PROXY,127.0.0.1)
33.4.20. urlencode函数
功能描述
对包含任何“特殊”字符(实际上是a-z和0-9以外的任何其他字符)的内容进行URL编码。
语法格式
urlencode(string VARCHAR) returns text
参数说明
参数名称
描述
string
需要编码的字符串
返回值说明
返回已编码后的字符串。
示例
SELECT urlencode('my special string''s & things?');
urlencode
-------------------------------------
my+special+string%27s+%26+things%3F
(1 row)
33.5. 插件http卸载方法
drop EXTENSION http;
33.6. 插件http升级方法¶
若该插件有升级版本则通过 alter extension 来升级插件。
示例,由 1.4 升级到 1.5:
ALTER EXTENSION http UPDATE TO '1.5';




