Question

I'm using Astyanax version 1.56.26 with Cassandra version 1.2.2

Ok, a little situational overview:

  1. I have created a very simple column family using cqlsh like so:

    CREATE TABLE users ( name text PRIMARY KEY, age int, weight int );

  2. I populated the column family (no empty columns)

  3. Querying users via cqlsh yields expected results

  4. Now I want to programmatically query users, so I try something like:

ColumnFamily<String, String> users =  
  new ColumnFamily<String, String>("users", StringSerializer.get(), StringSerializer.get());
OperationResult<ColumnList<String>> result = ks.prepareQuery(users)
  .getRow("bobbydigital") // valid rowkey
  .execute();
ColumnList<String> columns = result.getResult();
int weight = columns.getColumnByName("weight").getIntegerValue();
  1. During the assignment of weight a NPE is thrown! :(

My understanding is that the result should have contained all the columns associated with the row containing "bobbydigital" as its row key. I then tried to assign the value in the column named "weight" to the integer variable weight. I know that the variable columns is getting assigned because when I add some debug code right after the assignment, like so:

System.out.println("Column names = " + columns.getColumnNames());

I get the following output:

Column names = [, age, weight]

So why the null pointer? Can someone tell me where I went wrong? Also, why is there blank column name?

UPDATE:
Also if I try querying in a different manner, like so:

Column<String> result = ks.prepareQuery(users)
  .getKey("bobbydigital")
  .getColumn("weight")
  .execute().getResult();
int x = result.getIntegerValue();

I get the following exception:
InvalidRequestException(why:Not enough bytes to read value of component 0)

Thanks in advance for any help you can provide!

Was it helpful?

Solution

I figured out what I was doing incorrectly. The style of querying I was attempting is not valid with CQL tables. To query CQL tables with Astyanax you need to chain the .withCQL method to your prepareQuery method; passing a CQL statement as the argument.

Information specific to using CQL with Astyanax can be found here.

OTHER TIPS

I got this fixed by adding setCqlVersion

this.astyanaxContext = new AstyanaxContext.Builder() .forCluster("ClusterName") .forKeyspace(keyspace) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl().setCqlVersion( "3.0.0").setDiscoveryType(

And adding WITH COMPACT STORAGE while creating the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top