Skip to content

Commit ab01867

Browse files
committed
新增导出bulk文件工具类
1 parent 483b6f9 commit ab01867

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

tools/export.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# encoding: utf-8
2+
__author__ = 'zhanghe'
3+
4+
import json
5+
import time
6+
import os
7+
8+
9+
class ExportBulk(object):
10+
"""
11+
导出bulk文件工具类
12+
"""
13+
def __init__(self, index_name, type_name, file_name=None):
14+
self._index = index_name
15+
self._type = type_name
16+
if file_name is None:
17+
file_name = 'bulk_%s.bulk' % time.time()
18+
file_path = os.path.dirname(file_name)
19+
if not os.path.isdir(file_path):
20+
os.mkdir(file_path)
21+
self.bulk_fp = open(file_name, 'a')
22+
23+
def write(self, index_id, body):
24+
"""
25+
文件写入
26+
"""
27+
self.bulk_fp.write(json.dumps({"index": {"_index": self._index, '_type': self._type, 'id': index_id}})+"\n")
28+
self.bulk_fp.write(json.dumps(body)+"\n")
29+
30+
def close(self):
31+
"""
32+
关闭文件资源
33+
"""
34+
self.bulk_fp.close()
35+
36+
37+
def test():
38+
"""
39+
测试
40+
"""
41+
export_bulk = ExportBulk('service', 'provider')
42+
test_id = '1'
43+
test_body = {'a': '001', 'b': '002'}
44+
export_bulk.write(test_id, test_body)
45+
export_bulk.close()
46+
47+
48+
if __name__ == '__main__':
49+
test()

0 commit comments

Comments
 (0)