Created
April 23, 2024 14:18
-
-
Save ensean/bf4f2fe4cc840d349daa7d05f4f63754 to your computer and use it in GitHub Desktop.
AOS-anomaly-detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from opensearchpy import OpenSearch, helpers | |
host = 'vpc-xxxx-cyz5i7xdfs3dffsdgerwerw.ap-northeast-1.es.amazonaws.com' | |
port = 443 | |
auth = ('admin', 'password?') # For testing only. Don't store credentials in code. | |
# Create the client with SSL/TLS enabled, but hostname verification disabled. | |
client = OpenSearch( | |
hosts = [{'host': host, 'port': port}], | |
http_compress = True, # enables gzip compression for request bodies | |
http_auth = auth, | |
use_ssl = True, | |
verify_certs = True, | |
ssl_assert_hostname = False, | |
ssl_show_warn = False, | |
) | |
def create_index(client, index_name): | |
if client.indices.exists(index_name): | |
return | |
index_body = { | |
'settings': { | |
'index': { | |
'number_of_shards': 4 | |
} | |
}, | |
"mappings": { | |
"properties": { | |
"db": { | |
"type": "float" | |
}, | |
"device_id": { | |
"type": "text" | |
}, | |
"event_time": { | |
"type": "date", | |
"format": "yyyy-MM-dd HH:mm:ss", | |
"ignore_malformed": True | |
}, | |
"pm10": { | |
"type": "float" | |
}, | |
"pm25": { | |
"type": "float" | |
}, | |
"rh": { | |
"type": "float" | |
}, | |
"temp": { | |
"type": "float" | |
}, | |
"tsp": { | |
"type": "float" | |
}, | |
} | |
}, | |
} | |
response = client.indices.create(index_name, body=index_body) | |
return response | |
def bulk_ops(client, docs): | |
response = helpers.bulk(client, docs, max_retries=3) | |
print(response) | |
def main(): | |
f_path = sys.argv[1] | |
create_index(client, 'iot_data') | |
docs = [] | |
with open(f_path) as f: | |
lines = f.readlines() | |
for line in lines: | |
vals = line.split(",") | |
docs.append( | |
{ | |
"_index": "iot_data", | |
"device_id": vals[0], | |
"event_time": vals[1], | |
"pm2-5": vals[2], | |
"pm10": vals[3], | |
"tsp": vals[4], | |
"temp": vals[5], | |
"rh": vals[6], | |
"db": vals[7], | |
} | |
) | |
bulk_ops(client, docs[1:]) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
device-id | Timestamp | PM2-5 | PM10 | TSP | temp | rh | dB | |
---|---|---|---|---|---|---|---|---|
50944501 | 2024-04-13 00:00:19 | 41.10 | 50.10 | 75.20 | 25.00 | 70.80 | 52.40 | |
50944501 | 2024-04-13 00:01:19 | 39.60 | 48.80 | 73.20 | 25.00 | 70.80 | 52.10 | |
50944501 | 2024-04-13 00:02:19 | 40.40 | 48.50 | 72.80 | 25.00 | 70.80 | 52.90 | |
50944501 | 2024-04-13 00:03:19 | 41.60 | 50.20 | 75.30 | 25.00 | 70.80 | 51.90 | |
50944501 | 2024-04-13 00:04:19 | 41.20 | 50.00 | 75.00 | 25.00 | 70.80 | 52.50 | |
50944501 | 2024-04-13 00:05:19 | 40.90 | 49.80 | 74.70 | 25.00 | 70.80 | 52.60 | |
50944501 | 2024-04-13 00:06:19 | 41.40 | 50.80 | 76.30 | 25.00 | 70.70 | 51.80 | |
50944501 | 2024-04-13 00:07:19 | 39.90 | 48.50 | 72.80 | 25.00 | 70.70 | 51.60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment