· 6 years ago · May 13, 2019, 04:38 PM
1 def create_table_query(self, keyspace, table, primaryKey, additionalColumns):
2 output = """CREATE TABLE IF NOT EXISTS """ + keyspace + """.""" + table + """ ("""
3 for column_name, type in additionalColumns.items():
4 output = output + str(column_name) + ' ' + str(type) + ', '
5 output = output + 'PRIMARY KEY(' + primaryKey + ')'
6 output = output + \
7 """ );"""
8 self.session.execute(output)
9
10 def add_to_table_in_json(self, keyspace, table, data):
11 data = json.dumps(data);
12 self.session.execute("INSERT INTO " + keyspace + "." + table + " JSON '" + data + "';")
13
14if __name__ == "__main__":
15 list = {"user_id" : "int"}
16 CassClient = CassClient()
17 CassClient.create_keyspace()
18 CassClient.create_table_query(KEYSPACE, 'user', 'user_id', list)
19 data = {"user_id": 78}
20 CassClient.add_to_table_in_json(KEYSPACE, 'user', data)