How to list all column names in a column family in Cassandra?
Asked Answered
H

3

4

I'm using Cassandra, and I want to make a data browser which shows the contents of any column family. I need to get the column names to configure a UI grid. Is it possible to collect the names of columns in all rows?

Hexane answered 9/7, 2012 at 11:13 Comment(0)
B
8

The best way, if you are using Cassandra 1.1:

SELECT column_name FROM system.schema_columnfamilies WHERE keyspace_name = X AND columnfamily_name = Y

See also http://www.datastax.com/dev/blog/schema-in-cassandra-1-1

Bustup answered 9/7, 2012 at 15:19 Comment(2)
That looks like the most convenient solution. I'm using Cassandra 1.0.9 at this point so I can't try this, though.Hexane
For Cassandra 1.2 (apparently... I am using 2.0.4) you need to query schema_columns SELECT column_name FROM system.schema_columns WHERE keyspace_name = ? AND columnfamily_name = ?Folketing
D
1

There are several parts to this answer.

First, to answer your specific question, you should use a sliceQuery to get all of the columns in a row. Pass an empty ByteBuffer as the start and end values to effectively request all columns:

ByteBuffer empty = ByteBufferUtil.EMPTY_BYTE_BUFFER;       
sliceQuery.setRange(empty,empty,false,100);

Second, you need to make a assumption about the data model being stored in the column family.

If the data model is static (one row per object, one column per attribute) then the column names you get back should be the column names you want to display. If, however, the data is being stored in a dynamic way (one object per column, all objects with the same primary key stored in the same row) - as in a time series, for example - then you need to understand how the columns names (possibly composite) need to be interpreted. Another very common use of the dynamic approach is the way column families are persisted when defined via CQL.

I should add that there is no way to tell how a column family is being used to store objects. There is the schema that you can query, but that only tells you how the data is to be formatted when it's persisted, and not how the data (and the attribute names) are serialized and deserialized.

Dyspepsia answered 9/7, 2012 at 12:5 Comment(3)
Sorry, but before I get all columns in a row, I need to get the row, and I need to know the row key to do that. In my case, I only know the name of the column family and nothing about the rows in it. So what I want to do is to scan all rows and collect the row ids, or at least to scan all values of a particular column. P.S. I come from a relational world and Cassandra's giving me a hard time.Hexane
So your question is really about how to use Hector in general?Dyspepsia
There are some good hector examples at hector-client.github.com/hector/build/html/content/…Tessie
P
0

try this:

select columnfamily_name from system.schema_columnfamilies where keyspace_name=‘mykeyspace';
Putrid answered 1/5, 2014 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.