0

I have built a table using CQL2, data looks fine from cqlsh as well as hive command prompt. But when I am reading the data from java client it shows up binary value for 2 columns, as shown below

"hr1":"\u0000\u0000\u0000\u000e","hr2":"\u0000\u0000\u0000)"

After creating table have created index on column "col1" from CQL prompt.

My java code looks like :

   public void read() {
        if (null != cluster && null != keySpace) {
            CqlQuery<String, String, String> cqlQuery = new CqlQuery<String, String, String>(
                    keySpace, StringSerializer.get(), StringSerializer.get(),
                    StringSerializer.get());
            cqlQuery.setQuery("select * from myTable where col1 = 'HR100'");
            QueryResult<CqlRows<String, String, String>> result = cqlQuery
                    .execute();
            CqlRows rows = result.get();
            for (int i = 0; i < rows.getCount(); i++) {
                RowImpl<String, String, String> row = (RowImpl<String, String, String>) rows
                        .getList().get(i);
                System.out.println("Row key = " + row.getKey());
                for (HColumn<String, String> column : row.getColumnSlice()
                        .getColumns()) {
                    System.out.println("Column name = "
                            + column.getName().toString());
                    System.out.println("Colmn value = "
                            + column.getValue().toString());
                }
            }

        }
    }
Aaron
  • 55,518
  • 11
  • 116
  • 132
Madhup Srivastava
  • 446
  • 1
  • 6
  • 18
  • Can you post your schema? – Aaron Jun 26 '14 at 02:08
  • Following is schema definition - CREATE TABLE myTable ( col1 varchar PRIMARY KEY, col2 varchar, col3 varchar, hr1 int, hr2 int, hr3 varchar ); – Madhup Srivastava Jun 26 '14 at 02:31
  • I am providing schema as part of 4-5 comments as post doesn't allow me. ColumnFamily: myTable Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type Default column value validator: org.apache.cassandra.db.marshal.UTF8Type Cells sorted by: org.apache.cassandra.db.marshal.UTF8Type GC grace seconds: 864000 Compaction min/max thresholds: 4/32 Read repair chance: 0.1 DC Local Read repair chance: 0.0 Populate IO Cache on flush: false Replicate on write: true Caching: KEYS_ONLY Bloom Filter FP chance: default – Madhup Srivastava Jun 26 '14 at 02:58
  • Built indexes: [myTable.col3, myTable.hr1, myTable.hr2] Column Metadata: Column Name: col2 Validation Class: org.apache.cassandra.db.marshal.UTF8Type Column Name: hr2 Validation Class: org.apache.cassandra.db.marshal.Int32Type Column Name: hr1 Validation Class: org.apache.cassandra.db.marshal.Int32Type Index Name: ahr Index Type: KEYS Index Options: {} – Madhup Srivastava Jun 26 '14 at 02:59
  • Column Name: col3 Validation Class: org.apache.cassandra.db.marshal.UTF8Type Index Name: adt Index Type: KEYS Index Options: {} Column Name: col1 Validation Class: org.apache.cassandra.db.marshal.UTF8Type Index Name: snm Index Type: KEYS Index Options: {} Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy Compression Options: sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor – Madhup Srivastava Jun 26 '14 at 03:01
  • @BryceAtNetwork23 do you need any further information, please let me know. – Madhup Srivastava Jun 26 '14 at 19:31

1 Answers1

1

Here the problem is you are trying to read every value as Strings. That approach works for string values. But when it comes to other types, you must have to specify the appropriate Serialiser before reading. You can use column metadata to know the data types for a columnfamily.

So iteration is impossible in such situation and you must have to read individual columns as follows.

ColumnSlice<String, Integer> slice = row.getColumnSlice();
HColumn<String,Integer> col = slice.getColumnByName("column_name");

Here I have used an Integer type as value since I'm expecting an integer.

Please find more details in How to use cql queries to get different datatypes out of cassandra with java client hector

Community
  • 1
  • 1
Dunith Dhanushka
  • 4,139
  • 6
  • 26
  • 29