擅长:python、mysql、java
<p><code>streaming_bulk</code>使用<code>actions</code>的迭代器并为每个操作生成一个响应。
因此,首先需要在文档上编写一个简单的迭代器,如下所示:</p>
<pre><code>def document_stream(file_to_index):
with open(file_to_index, "rb") as csvfile:
for row in csv.reader(csvfile):
yield {"_index": index_name,
"_type": type_name,
"_source": transform_row(row)
}
</code></pre>
<p>然后执行流式大容量插入</p>
<pre><code>stream = document_stream(file_to_index)
for ok, response in streaming_bulk(es, actions = stream):
if not ok:
# failure inserting
print response
</code></pre>