matteo rulli
matteo rulli

Reputation: 1493

cassandra uuid in SELECT statements with Spark SQL

I have the following table in cassandra (v. 2.2.3)

cqlsh> DESCRIBE TABLE historian.timelines;

CREATE TABLE historian.timelines (
    assetid uuid,
    tslice int,
    ...
    value map<text, text>,
    PRIMARY KEY ((assetid, tslice), ...)
) WITH CLUSTERING ORDER BY (deviceid ASC, paramid ASC, fts DESC) 
...
    ;

And I want to extract the data through Apache Spark (v. 1.5.0) via the following java snippet (using the cassandra spark connector v. 1.5.0 and cassandra driver core v. 2.2.0 RC3):

// Initialize Spark SQL Context
CassandraSQLContext sqlContext = new CassandraSQLContext(jsc.sc());
sqlContext.setKeyspace(keyspace);
DataFrame df = sqlContext.sql("SELECT * FROM " + tableName + 
    " WHERE assetid = '085eb9c6-8a16-11e5-af63-feff819cdc9f' LIMIT 2");
df.show();

At this point I get the following error accessing show method above:

cannot resolve '(assetid = cast(085eb9c6-8a16-11e5-af63-feff819cdc9f as double))' due to data type mismatch: 
differing types in '(assetid = cast(085eb9c6-8a16-11e5-af63-feff819cdc9f as double))' (uuid and double).;

So it seems that Spark SQL is not interpreting the assetid input as an UUID. What I could do to handle cassandra UUID type in Spark SQL queries?

Thanks!

Upvotes: 3

Views: 1185

Answers (1)

Will
Will

Reputation: 2217

Indeed your query parameter is a String not a UUID, simply convert the query param like this :

import java.util.UUID;

DataFrame df = sqlContext.sql("SELECT * FROM " + tableName + 
" WHERE assetid = "+ UUID.fromString("085eb9c6-8a16-11e5-af63-feff819cdc9f") +" LIMIT 2");

Upvotes: 0

Related Questions