Elastic Cloud に API でデータを投入する


blog-apr0102 というエンジンを作成します。

Credentials で private-key を調べます。

curl_insert.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-***********"
NAME_ENGINE="blog-apr0102"
#
echo ${PRIVATE-KEY}
#
curl -X POST ${HOST}'/api/as/v1/engines/'${NAME_ENGINE}'/documents' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer '${PRIVATE_KEY} \
  -d@all_data.json
all_data.json
[
{
	"id": "t001",
	"name": "山本太郎",
	"title": "My Name Is Yamato",
	"content": "I love dog",
	"tags": ["red", "green", "blue"]
},
{
	"id": "t002",
	"name": "田中康夫",
	"title": "My Name Is Tanaka",
	"content": "I love cat",
	"tags": ["Earth", "Moon", "Mars"]
},
{
	"id": "t003",
	"name": "渡辺五郎",
	"title": "My Name Is Watanabe",
	"content": "I love fish",
	"tags": ["apple", "orange", "banana"]
}
]

実行結果

データの読み込み

Httpie

http_read.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-******"
NAME_ENGINE="blog-apr0102"
#
http GET ${HOST}'/api/as/v1/engines/'$NAME_ENGINE'/documents/list' \
	'Authorization: Bearer '${PRIVATE_KEY} < query00.json
query00.json
{
  "page": {
    "size": 5
}
}

Curl

curl_read.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-******"
NAME_ENGINE="blog-apr0102"
#
#
curl -X GET ${HOST}'/api/as/v1/engines/'$NAME_ENGINE'/documents/list' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '${PRIVATE_KEY} \
-d '{
  "page": {
    "size": 5
  }
}'

参考

Engines を API で取り扱う方法
参考ページ
Engines API

Create

create_engine.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-********"
#
curl -X POST $HOST'/api/as/v1/engines' \
	-H 'Authorization: Bearer '$PRIVATE_KEY \
	-d '{"name": "blog-apr0102"}'

List

list_engines.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-*******"
#
http $HOST'/api/as/v1/engines' \
	'Authorization: Bearer '$PRIVATE_KEY

Delete

delete_engine.sh
HOST="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
PRIVATE_KEY="private-******"
NAME_ENGINE="blog-apr0102"
#
curl -XDELETE $HOST'/api/as/v1/engines/'$NAME_ENGINE \
	-H 'Authorization: Bearer '$PRIVATE_KEY

参考

Python3 の例はこちら
Python3: Elastic Cloud に API でデータを投入する