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?

Yelis913
  • 43
  • 1
  • 4

3 Answers3

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

jbellis
  • 19,347
  • 2
  • 38
  • 47
  • That looks like the most convenient solution. I'm using Cassandra 1.0.9 at this point so I can't try this, though. – Yelis913 Jul 10 '12 at 08:01
  • 1
    For Cassandra 1.2 (apparently... I am using 2.0.4) you need to query [schema_columns](http://www.datastax.com/documentation/cql/cql_using/use_query_system_c.html?pagename=docs&version=1.2&file=cql_cli/using/query_system_tables) `SELECT column_name FROM system.schema_columns WHERE keyspace_name = ? AND columnfamily_name = ?` – Andrew Charneski Jan 29 '14 at 20:45
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.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
  • 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. – Yelis913 Jul 09 '12 at 12:55
  • So your question is really about how to use Hector in general? – Chris Gerken Jul 09 '12 at 13:32
  • There are some good hector examples at http://hector-client.github.com/hector/build/html/content/getting_started.html#read – pd40 Jul 10 '12 at 00:17
0

try this:

select columnfamily_name from system.schema_columnfamilies where keyspace_name=‘mykeyspace';
DàChún
  • 4,751
  • 1
  • 36
  • 39