-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElastic_Integration.py
More file actions
114 lines (66 loc) · 1.92 KB
/
Elastic_Integration.py
File metadata and controls
114 lines (66 loc) · 1.92 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from elasticsearch import Elasticsearch
import time
from datetime import datetime
# In[2]:
# Found in the 'Manage this deployment' page
CLOUD_ID = "xxx"
# In[3]:
# Found in the 'Management' page under the section 'Security'
API_KEY = "xxx"
#MGlmbTU0NEJ4Zk8wVTEzSUVwWUQ6NUtwZlNXc0NRbTJhaXp4Vmw4LWhtZw==
#https://c548d90314b149f993fa985c277fc9d1.us-central1.gcp.cloud.es.io:443
#https://c548d90314b149f993fa985c277fc9d1.us-central1.gcp.cloud.es.io:443
# In[4]:
# Create the client instance
client = Elasticsearch(
cloud_id=CLOUD_ID,
api_key=API_KEY,
)
# In[5]:
client.info()
# In[6]:
def create_document(data):
event_id = datetime.timestamp(datetime.now())
Transcation = {
"Latitude": data[0],
"Longitude": data[1],
"Altitude": data[2],
"Type": data[3],
"_extract_binary_content": "true",
"_reduce_whitespace": "true",
"_run_ml_inference": "true"
}
doc.append({"index": {"_index": "iotdata", "_id": event_id}})
doc.append(Transcation)
#print(doc)
#print()
return doc
# In[7]:
doc=[]
# In[8]:
def read_file_line_by_line(file_name):
try:
with open(file_name, 'r') as file:
for line in file:
s = line.strip()
#print(s)
data = s.split(',')
#print(data)
data_float = [float(num) for num in data]
#print(data_float)
create_document(data)# .strip() removes leading/trailing whitespace and newline characters
except FileNotFoundError:
print(f"File '{file_name}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage:
file_name = "example.txt"
read_file_line_by_line(file_name)
# In[9]:
doc
# In[10]:
client.bulk(operations=doc, pipeline="ent-search-generic-ingestion")
# In[ ]: