0

for the following piece of code I am getting an InvalidTypeException whenever I am using the row.getToken("fieldname").

Record RowToRecord(Row rw) {

    ColumnDefinitions cd = rw.getColumnDefinitions();
    Record rec = new Record();
    int i;
    for(i = 0; i < cd.size(); i++) {
        rec.fields.add(cd.getName(i));
        System.out.println(cd.getName(i));
        //System.out.println((rw.getToken(cd.getName(i))).getValue());
        Token tk = rw.getToken(cd.getName(i));    //// InvalidTypeException on this line.
        //System.out.println(tk.getValue()+" "+tk.getType().toString());
        rec.values.add(tk.getValue());
        rec.types.add(tk.getType().toString());
        //Token tk = new Token();

    }
    return rec;
}
Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23

1 Answers1

1

getToken is meant to be called on a column that contains a Cassandra token. In 99% of cases, that will be the result of a call to the token() CQL function, for example the first column in this query:

select token(id), col1 from my_table where id = ...

Your code is calling it for all columns, which will fail as soon as you have a column that doesn't match the CQL type for tokens.

That CQL type depends on the partitioner used in your cluster:

  • murmur3 partitioner (the default): token(...) will return a BIGINT
  • random partitioner: VARINT
  • ordered partitioner: BLOB

In theory you can call getToken on any column with this type (although in practice it probably only makes sense for columns that are the result of a token() call, as explained above).

Olivier Michallat
  • 2,302
  • 11
  • 13
  • I wanted to convert cassandra Row object to Record object, basically extract the fields, values and type from Row object and put it into record.field ( list(string type) which contains name of fields), record.values ( list(Object type) which contains values of fields). But I am unable to extract field values without knowing there types(using row.get----("fieldName")). Is there any way I can achieve that?. – Anirban Bhowmik May 13 '15 at 09:12
  • Starting with 2.0.10 there is a `getObject` method, it will be in 2.1.6 as well. – Olivier Michallat May 13 '15 at 12:07
  • And actually there is a workaround before that: `cd.getType(i).deserialize(rw.getBytesUnsafe(i))` – Olivier Michallat May 13 '15 at 13:23