-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
59 lines (45 loc) · 1.69 KB
/
data.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
import ksql
class DataStream:
def __init__(self, ksqldb_url):
self.client = ksql.KSQLAPI(ksqldb_url)
def create_stream(self, stream_name, topic):
"""This method creates a stream in ksqldb
Args:
stream_name (str): Name of the Stream
topic (str): Name of the Kafka Topic
Returns:
response: Response Returned from KSQLDB if any
excpt: Exception if any
"""
try:
response = self.client.ksql(
f"CREATE STREAM {stream_name} (INSTANT BIGINT, FAST BIGINT, ECO BIGINT, AVG_GAS_ESTIMATE BIGINT, BASE_FEE BIGINT, PRICE DOUBLE) WITH (KAFKA_TOPIC='{topic}', KEY_FORMAT='KAFKA', PARTITIONS=2, VALUE_FORMAT='JSON');")
return response, None
except Exception as e:
return None, e
def run_ksql_command(self, command):
"""This method runs a ksqldb specific command.
Args:
command (str): KSQLDB command to be run
Returns:
response: Response Returned from KSQLDB if any
excpt: Exception if any
"""
try:
response = self.client.ksql(command)
return response, None
except Exception as e:
return None, e
def run_ksql_query(self, query):
"""This method runs a ksqldb query command.
Args:
query (str): KSQLDB query command to be run
Returns:
response: Response Returned from KSQLDB if any
excpt: Exception if any
"""
try:
response = self.client.ksql(query)
return response, None
except Exception as e:
return None, e