-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelastic_search_utils.py
217 lines (176 loc) · 9.02 KB
/
elastic_search_utils.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from datetime import date, datetime, timedelta
from dateutil import tz
from elasticsearch import Elasticsearch, TransportError
import os
def datetime_from_iso_format(str_timestamp):
date_str = str_timestamp[:10]
time_str = str_timestamp[11:]
year, month, day = date_str.split('-')
hh, mm, ss = time_str.split(':')
return datetime(int(year), int(month), int(day), int(hh), int(mm), int(ss))
class ElasticSearchImporter:
def __init__(self, host, port, html_dir, lang, logger=None):
self.html_dir = html_dir
self.lang = lang
self.logger = logger
self.es = Elasticsearch([f'http://{host}:{port}'], use_ssl=False)
def update_record(self, input_file, index, is_data_stream=False):
"Import a page into Elastic Search database."
"Returns the record_id if the page has been successfully imported."
"None otherwise."
if self.logger is not None:
self.logger.info(f"ES update_record: {input_file}")
dirs = input_file[len(self.html_dir) + 1:].split("/")
region = dirs[0]
domain = dirs[2]
path = "/".join(dirs[3:-4])
if self.logger is not None:
self.logger.debug(f"region={region} domain={domain} path={path}")
timestamp_year = dirs[-4]
timestamp_month = dirs[-3]
timestamp_day_time = dirs[-2]
if self.logger is not None:
self.logger.debug(f"timestamp_year={timestamp_year} timestamp_month={timestamp_month} timestamp_day_time={timestamp_day_time}")
timestamp_day, timestamp_hh, timestamp_mm = timestamp_day_time.split("-")
timestamp_local = datetime(int(timestamp_year), int(timestamp_month), int(timestamp_day), int(timestamp_hh), int(timestamp_mm))
timestamp_utc = timestamp_local.astimezone(tz.gettz('UTC')).replace(tzinfo=None)
filename = dirs[-1][:-4]
record_id = "/".join([region, domain, path, filename])
must_index_record = False
if (self.es.exists(index=index, id=record_id)):
index_record = self.es.get(index=index, id=record_id)
index_record_timestamp_str = index_record['_source']['timestamp']['local']
if self.logger is not None:
self.logger.debug(f"index_record={index_record}")
index_record_timestamp = datetime_from_iso_format(index_record_timestamp_str)
if timestamp_local > index_record_timestamp:
if self.logger is not None:
self.logger.info(f"timestamp is newer!!! rec_id={record_id} file_ts={timestamp_local} ndx_rec_ts={index_record_timestamp}")
must_index_record = True
else:
must_index_record = True
if must_index_record:
url_filename = f"{self.html_dir}/{region}/orig/{domain}/{path}/{timestamp_year}/{timestamp_month}/{timestamp_day_time}/{filename}.url"
txt_filename = f"{self.html_dir}/{region}/{self.lang}_translated/{domain}/{path}/{timestamp_year}/{timestamp_month}/{timestamp_day_time}/{filename}.txt"
if self.logger is not None:
self.logger.debug(f"record_id={record_id} url_filename={url_filename} txt_filename={txt_filename}")
if not os.path.isfile(url_filename):
if self.logger is not None:
self.logger.info(f"url_file {url_filename} not found so skip it.")
return None
if not os.path.isfile(txt_filename):
if self.logger is not None:
self.logger.info(f"txt_file {txt_filename}is not found so skip it.")
return None
with open(url_filename, encoding='utf-8') as url_file:
url = url_file.read()
with open(txt_filename, encoding='utf-8') as text_file:
text = text_file.read()
record = {
'text': text,
'region': region,
'domain': domain,
'path': path,
'timestamp': {
'year': int(timestamp_year),
'month': int(timestamp_month),
'day': int(timestamp_day),
'hh': int(timestamp_hh),
'mm': int(timestamp_mm),
'local': timestamp_local,
'utc': timestamp_utc
},
'filename': filename,
'url': url
}
if is_data_stream:
record['@timestamp'] = timestamp_utc
if self.logger is not None:
self.logger.debug(f"ES Record: id={record_id} content={record}")
try:
res = self.es.index(index=index, id=record_id, body=record, op_type="create" if is_data_stream else "index")
if self.logger is not None:
self.logger.debug(f"Response for es.index(id={record_id}) -> {res}")
except TransportError as te:
if self.logger is not None:
self.logger.info(f"An error has occurred when indexing record with id={record_id}: {te}")
return None
return record_id
class ElasticSearchTwitterImporter:
def __init__(self, host, port, html_dir, lang, logger=None):
self.html_dir = html_dir
self.lang = lang
self.logger = logger
self.es = Elasticsearch([f'http://{host}:{port}'], use_ssl=False)
def update_record(self, input_file, index, is_data_stream=False):
"Import a tweet into Elastic Search database."
"Returns the record_id if the page has been successfully imported."
"None otherwise."
if self.logger is not None:
self.logger.info(f"ES update_record: {input_file}")
dirs = input_file[len(self.html_dir) + 1:].split("/")
country = dirs[0]
if self.logger is not None:
self.logger.debug(f"country={country}")
timestamp_year = dirs[-5]
timestamp_month = dirs[-4]
timestamp_day = dirs[-3]
timestamp_time = dirs[-2]
if self.logger is not None:
self.logger.debug(f"timestamp_year={timestamp_year} timestamp_month={timestamp_month} timestamp_day={timestamp_day} timestamp_time={timestamp_time}")
timestamp_hh, timestamp_mm = timestamp_time.split("-")
timestamp_local = datetime(int(timestamp_year), int(timestamp_month), int(timestamp_day), int(timestamp_hh), int(timestamp_mm))
timestamp_utc = timestamp_local.astimezone(tz.gettz('UTC')).replace(tzinfo=None)
filename = dirs[-1][:-4]
record_id = filename
must_index_record = False
if (self.es.exists(index=index, id=record_id)):
index_record = self.es.get(index=index, id=record_id)
index_record_timestamp_str = index_record['_source']['timestamp']['local']
if self.logger is not None:
self.logger.debug(f"index_record={index_record}")
index_record_timestamp = datetime_from_iso_format(index_record_timestamp_str)
if timestamp_local > index_record_timestamp:
if self.logger is not None:
self.logger.info(f"timestamp is newer!!! rec_id={record_id} file_ts={timestamp_local} ndx_rec_ts={index_record_timestamp}")
must_index_record = True
else:
must_index_record = True
if not must_index_record:
return None
txt_filename = f"{self.html_dir}/{country}/{self.lang}_translated/{timestamp_year}/{timestamp_month}/{timestamp_day}/{timestamp_time}/{filename}.txt"
if self.logger is not None:
self.logger.debug(f"record_id={record_id} txt_filename={txt_filename}")
if not os.path.isfile(txt_filename):
if self.logger is not None:
self.logger.info(f"txt_file {txt_filename}is not found so skip it.")
return None
with open(txt_filename, encoding='utf-8') as text_file:
text = text_file.read()
record = {
'text': text,
'country': country,
'timestamp': {
'year': int(timestamp_year),
'month': int(timestamp_month),
'day': int(timestamp_day),
'hh': int(timestamp_hh),
'mm': int(timestamp_mm),
'local': timestamp_local,
'utc': timestamp_utc
},
'filename': filename
}
if is_data_stream:
record['@timestamp'] = timestamp_utc
if self.logger is not None:
self.logger.debug(f"ES Record: id={record_id} content={record}")
try:
res = self.es.index(index=index, id=record_id, body=record, op_type="create" if is_data_stream else "index")
if self.logger is not None:
self.logger.debug(f"Response for es.index(id={record_id}) -> {res}")
except TransportError as te:
if self.logger is not None:
self.logger.info(f"An error has occurred when indexing record with id={record_id}: {te}")
return None
return record_id