-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer_async.py
executable file
·145 lines (107 loc) · 3.39 KB
/
indexer_async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3.5
import sys
import asyncio
import logging
from asyncio import Queue
from elasticsearch_async import AsyncElasticsearch
from dbp_files import *
from es_helpers import *
from pprint import pprint
logger = logger(__name__, 'info')
logger.info('file("%s").class("%s") Initialized.' % (__file__, __name__))
q = Queue()
# ES
op_dict = {"index": {"_index": 'dbpedia',
"_type": 'dbpedia',
"_id": ''}}
es = ElasticsearchHelper(port='9200',
loglevel='info')
#es.create_index(delete_first=True)
es.create_index(delete_first=True)
COMMIT_SIZE = 100
# /ES
MAX = 100
PATH = '../nobackup/dbp/' #'/opt/home/wfa011/code/create_dbpedia_index/3.0/current/nl/'
disambiguations = Disambiguations()
disambiguations.PATH = PATH
labels = Labels()
labels.PATH = PATH
labels_en_uris = Labels_en_uris()
labels_en_uris.PATH = PATH
redirects = Redirects()
redirects.PATH = PATH
interlanguage_links = Interlanguage_links()
interlanguage_links.PATH = PATH
client = AsyncElasticsearch()
@asyncio.coroutine
def indexer_looper(q):
bulk_data = []
done = False
doc = {}
while not done:
size = q.qsize
if size:
doc = yield from q.get()
if isinstance(doc, str):
yield from asyncio.sleep(0.1)
done = True
elif doc.get('id_wd'):
op_dict['index']['_id'] = doc.get('id_wd')
bulk_data.append(op_dict)
bulk_data.append(doc)
doc = {}
if len(bulk_data) > COMMIT_SIZE:
res = yield from client.bulk(index='dbpedia',
body=bulk_data,
refresh=False)
if res.get('errors') == False:
#print("added %i records" % COMMIT_SIZE)
sys.stdout.flush()
sys.stdout.write('#')
pass
#pprint(doc)
bulk_data = []
q.task_done()
@asyncio.coroutine
def object_looper(obj, q):
for i, p in enumerate(obj.loop()):
if i > MAX:
break
if p:
obj = {}
for item in p[list(p.keys())[1]]:
key = list(item.keys())[0]
obj[key] = item[list(item.keys())[0]]
doc = (interlanguage_links.prepare_es_doc(obj))
sys.stdout.flush()
if i % COMMIT_SIZE - 1 == 0:
sys.stdout.write('?')
yield from q.put(doc)
yield from asyncio.sleep(0.0001)
yield from q.put("STOP!")
loop = asyncio.get_event_loop()
#loop.set_debug(True)
tasks = [
asyncio.ensure_future(object_looper(interlanguage_links, q)),
#asyncio.ensure_future(object_looper(labels, q)),
asyncio.ensure_future(indexer_looper(q)),
#asyncio.ensure_future(object_looper(disambiguations))
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
client.transport.close()
print('\n')
'''
loop = asyncio.get_event_loop()
#loop.set_debug(True)
tasks = [
#asyncio.ensure_future(object_looper(interlanguage_links, q)),
asyncio.ensure_future(object_looper(labels('nl'), q)),
asyncio.ensure_future(object_looper(labels('en'), q)),
asyncio.ensure_future(indexer_looper(q)),
#asyncio.ensure_future(object_looper(disambiguations))
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
'''
logger.done('file("%s").class("%s") Initialized.' % (__file__, __name__))