How to add cassandra table column dynamically?

社会主义新天地 提交于 2020-01-07 05:50:07

问题


I'm trying to add a new columns to cassandra table dynamically. I'm using below version -

cqlsh 5.0.1

I'm using python to interact with Cassandra. I have one python list which I wish to add as a column names to Cassandra table.

Python List -

['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']

Currently, I'm iterating a list and then adding each column one by one to cassandra table like below -

from cassandra.cluster import Cluster
cluster = Cluster(['localhost'])
session = cluster.connect()

session.execute("CREATE KEYSPACE IF NOT EXISTS data WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};")
session.execute("use my_data")
session.execute("CREATE TABLE IF NOT EXISTS data.my_data (pk uuid PRIMARY KEY);")
names = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']

for val in names:
   try:
      session.execute("alter table data.my_data add "+ val +" ascii;")
   except:
      pass

It is working fine but actual problem is, if in my python list more than 1000 entries are available then there should be more than 1000 hits to the cassandra which will be time consuming. Is any different approach available to add a column names to existing table in cassandra?


回答1:


Cassandra internally stores data as rows, each row has a key (Partition key) and dynamic number of columns (clustering key). So, you can use Clustering Key value for your column names, e.g

CREATE TABLE my_data (
     pk text,
     column text,
     value text,
     PRIMARY KEY (pk, column)
); 

Insert new columns and values by a regular INSERT query:

INSERT INTO my_data (pk, column, value) VALUES ('pk1', 'A', 'value A'); 
INSERT INTO my_data (pk, column, value) VALUES ('pk1', 'B', 'value B');
INSERT INTO my_data (pk, column, value) VALUES ('pk1', 'C', 'value C');
...  

Get all columns for pk1:

SELECT * FROM my_data WHERE pk='pk1';

Updated

Assume, you have table my_data as described above and you want to add some columns and data for a specific pk value. In python code perform insert query:

pk = 'pk'
columns_data = {'A':'value for A','B':'value  for B','C': 'value for C'} #dynamic column data
for col_name, col_value in columns_data.iteritems():
   try:
      session.execute("INSERT INTO my_data (pk, column, value) VALUES (%s, %s, %s)", (pk, col_name, col_value))
   except:
      pass

Moreover, you can use asynchronous driver's methods, to achieve more performance of inserting.



来源:https://stackoverflow.com/questions/44408279/how-to-add-cassandra-table-column-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!