Bri*_*ian 5 python json elasticsearch python-requests
我正在尝试使用来自 Elasticsearch 的批量 API,我发现这可以使用以下请求来完成,这很特别,因为作为“数据”给出的不是正确的 JSON,而是使用 \n 作为分隔符的 JSON .
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d '
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
'
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在 python 中执行这样的请求?ElasticSearch 的作者建议不要漂亮地打印 JSON,但我不确定它是什么意思(请参阅https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html)
我知道这是一个有效的 python 请求
import requests
import json
data = json.dumps({"field":"value"})
r = requests.post("localhost:9200/_bulk?pretty", data=data)
Run Code Online (Sandbox Code Playgroud)
但是如果 JSON 是 \n 分隔的,我该怎么办?
这实际上是一组单独的 JSON 文档,用换行符连接在一起。所以你可以做这样的事情:
data = [
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } },
{ "field1" : "value1" },
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" }, },
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" }, },
{ "field1" : "value3" },
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} },
{ "doc" : {"field2" : "value2"} }
]
data_to_post = '\n'.join(json.dumps(d) for d in data)
r = requests.post("localhost:9200/_bulk?pretty", data=data_to_post)
Run Code Online (Sandbox Code Playgroud)
但是,正如评论中指出的那样,Elasticsearch Python 客户端可能更有用。
| 归档时间: |
|
| 查看次数: |
2633 次 |
| 最近记录: |