-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP Endpoint - DB Connection(NoSQL ClickhouseDB).py
132 lines (70 loc) · 3 KB
/
HTTP Endpoint - DB Connection(NoSQL ClickhouseDB).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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from clickhouse_driver import Client
from datetime import datetime
from flatten_json import flatten
import re
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import json
client = Client(host='Foobar', user = "Foobar", password = "Foobar", database = "Foobar")
false = False
# In[ ]:
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
s = json.dumps(post_data.decode())
parsed = json.loads(s)
parsed_dict = eval(post_data)
flattened = flatten(parsed_dict)
bids_not_stripped = re.findall('\"price\":.+',parsed)
bids = [x.strip('"price":,') for x in bids_not_stripped]
bid_ids_not_stripped = re.findall('\"id\": \"\S+\"',parsed)
bidders = [x.strip('"id":"') for x in bid_ids_not_stripped[2:]]
auction_id = str(flattened['Auction_id'])
publishers = str(flattened['Auction_publisher'])
page = str(flattened['Auction_site_page'])
impression_id = str(flattened['Auction_impressions_0_id'])
height = str(flattened['Auction_impressions_0_acceptedSizes_0_h'])
width = str(flattened['Auction_impressions_0_acceptedSizes_0_w'])
country = str(flattened['Auction_device_geo_country'])
timestamp = flattened['Statistics_lastAuctionEndTimestamp']
query_1 = "INSERT INTO Gonenc(auction_id,publisher,page,impid,h,w,bidders,bids,country,time) VALUES "
results = str((auction_id,
publishers,page,
impression_id,height,
width,bidders,bids,
country,timestamp))
query = query_1 + results
print(query)
client.execute(query)
self._set_response()
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8085):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
# In[ ]:
# In[ ]:
# In[ ]: