Reputation: 37
I am connecting to a Cassandra database and am trying to find out what attributes an entry in a column family has. I am unable to use the cqlsh command line utility. After creating the connection I have:
myCF = ColumnFamily(myConncetion, "user")
for u in myCF.get_range():
u_item = u[1]
name = u_item.get('FIRST_NAME')
print name
This prints the names of all users. I would like to know what other attributes besides "FIRST_NAME" a u_item has. Is there a built in function that can describe its attributes? Thanks in advance.
Upvotes: 1
Views: 823
Reputation: 57748
Which version of Cassandra are you running? If you wanted to go through the table programmatically, you could try querying the schema from the system tables:
If you're on Cassandra >= 3.x+:
SELECT column_name FROM system_schema.columns
WHERE keyspace_name='yourkeyspacename' AND table_name='yourtablename';
If you're on Cassandra <= 2.2.x;
SELECT column_name FROM system.schema_columns
WHERE keyspace_name='yourkeyspacename' AND columnfamily_name ='yourtablename';
How can I execute this Select statement from a python script?
If I'm running on Cassandra 2.2.5, I can take the above code and make it work in a Python script (cassConnect.py
) like this:
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import sys
hostname=sys.argv[1]
username=sys.argv[2]
password=sys.argv[3]
keyspace=sys.argv[4]
table=sys.argv[5]
nodes = []
nodes.append(hostname)
auth_provider = PlainTextAuthProvider(username=username, password=password)
cluster = Cluster(nodes,auth_provider=auth_provider)
session = cluster.connect(keyspace)
pStatement = session.prepare("""
SELECT column_name FROM system.schema_columns WHERE keyspace_name=? AND columnfamily_name=?;
""")
rows = session.execute(pStatement,[keyspace,table])
for row in rows:
print row[0]
$ python cassConnect.py 127.0.0.1 aploetz bacon stackoverflow user
email
first
last
username
Upvotes: 1