LoginSignup
0
0

More than 1 year has passed since last update.

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

Posted at

こちらと同じことを Python3 で行いました。
Elastic Cloud に API でデータを投入する

elastic_cloud_insert.py
#! /usr/bin/python
#
#	elastic_cloud_insert.py
#
#					Apr/04/2022
# ------------------------------------------------------------------
import  sys
import  json
import  requests
# ------------------------------------------------------------------
def file_to_str_proc(file_in):
	str_out = ""
	try:
		fp_in = open(file_in,encoding='utf-8')
		str_out = fp_in.read()
		fp_in.close()
	except Exception as ee:
		sys.stderr.write("*** error *** file_to_str_proc ***\n")
		sys.stderr.write(str (ee))
#
	return	str_out
# ------------------------------------------------------------------
host="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
private_key="private-*******"
name_engine="blog-apr0102"
#
sys.stderr.write("*** 開始 ***\n")
file_json = sys.argv[1]
sys.stderr.write(file_json + "\n")
#
json_str = file_to_str_proc(file_json)
#
url=host + "/api/as/v1/engines/" + name_engine + "/documents"
headers = {"Content-Type": "application/json",
	"Authorization": "Bearer " + private_key}

rr=requests.post(url, headers=headers, data=json_str.encode('utf-8'))
#
print(rr.text)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./elastic_cloud_insert.py all_data.json
*** 開始 ***
all_data.json
[{"id":"t001","errors":[]},{"id":"t002","errors":[]},{"id":"t003","errors":[]}]
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0