流式插入
insertStream 和 insertFullduplex 是用于流式 JSON 输入的实验性插入端点。
认证
需要 Api-Key 和 Api-Secret,或者在数据库 owner 被允许时使用 Glue owner token。
Insert Stream
insertStream 会接管 HTTP 连接,并使用 chunked 输出返回 202 Accepted。
curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream" \
-H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
-H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
-H "Content-Type: application/jsonl" \
-d '{"id":"evt-1","type":"signup"}
{"id":"evt-2","type":"login"}'POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/jsonl
{"id":"evt-1","type":"signup"}
{"id":"evt-2","type":"login"}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"id": "evt-1", "type": "signup"}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
req.Header.Set("Content-Type", "application/jsonl")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['id' => 'evt-1', 'type' => 'signup'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
'Content-Type: application/jsonl',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl",
}
payload = {"id": "evt-1", "type": "signup"}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream",
headers=headers,
data=body
)
print(response.text)
const payload = {"id": "evt-1", "type": "signup"};
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream", {
method: "POST",
headers: {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"id": "evt-1", "type": "signup"};
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream", {
method: "POST",
headers: {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("id", "evt-1", "type", "signup");
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertStream"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
.header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
.header("Content-Type", "application/jsonl")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}Insert Full Duplex
insertFullduplex 启用 HTTP full duplex,并在读取请求体时把每个已插入文档编码为 JSON。
curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex" \
-H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
-H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
-H "Content-Type: application/jsonl" \
-d '{"id":"evt-1","type":"signup"}
{"id":"evt-2","type":"login"}'POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/jsonl
{"id":"evt-1","type":"signup"}
{"id":"evt-2","type":"login"}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"id": "evt-1", "type": "signup"}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
req.Header.Set("Content-Type", "application/jsonl")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['id' => 'evt-1', 'type' => 'signup'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
'Content-Type: application/jsonl',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl",
}
payload = {"id": "evt-1", "type": "signup"}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex",
headers=headers,
data=body
)
print(response.text)
const payload = {"id": "evt-1", "type": "signup"};
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex", {
method: "POST",
headers: {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"id": "evt-1", "type": "signup"};
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex", {
method: "POST",
headers: {
"Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
"Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
"Content-Type": "application/jsonl"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("id", "evt-1", "type", "signup");
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/events:insertFullduplex"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
.header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
.header("Content-Type", "application/jsonl")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}两个端点的响应体都是包含已插入文档的 JSON Lines;insertStream 也可能返回逐文档错误字符串。
评论