简体   繁体   中英

Mapping UUID in Spark Cassandra connector

I have the following code to save RDD to cassandra:

 JavaRDD<UserByID> mapped = ......

CassandraJavaUtil.javaFunctions(mapped)
.writerBuilder("mykeyspace", "user_by_id", mapToRow(UserByID.class)).saveToCassandra();

And UserByID is a normal serializable POJO with the following variable with getters and setters

private UUID userid;

Cassandra table has exactly the same names of the class UserByID variables, and userid is of type uuid in Cassandra table, I am loading data successfully from the table using the same class mapping.

CassandraJavaRDD<UserByID> UserByIDRDD = javaFunctions(spark)
 .cassandraTable("mykeyspace", "user_by_id", mapRowTo(UserByID.class));

however, when I call saveToCassandra function above, I get the following exception:

org.apache.spark.SparkException: Job aborted due to stage failure: Task
0 in stage 227.0 failed 1 times, most recent failure: Lost task 0.0
in stage 227.0 (TID 12721, localhost, executor driver): 
java.lang.IllegalArgumentException: 
The value (4e22e71a-a387-4de8-baf1-0ef6e65fe33e) of the type 
(java.util.UUID) cannot be converted to 
struct<leastSignificantBits:bigint,mostSignificantBits:bigint> 

To solve the problem I have registered UUID codec, but that didn't help, I am using spark-cassandra-connector_2.11 version 2.4.0 and the same version for spark-core_2.11 any suggestion?

my reference is here but it has no Java UUID example, your help is appreciated.

That's really strange error - this just works fine with connector 2.4.0 & Spark 2.2.1 with following example:

Table definition:

CREATE TABLE test.utest (
    id int PRIMARY KEY,
    u uuid
);

POJO class :

public class UUIDData {
    private UUID u;
    private int id;
    ...
    // getters/setters
};

Spark job :

public static void main(String[] args) {
    SparkSession spark = SparkSession
            .builder()
            .appName("UUIDTest")
            .getOrCreate();

    CassandraJavaRDD<UUIDData> uuids = javaFunctions(spark.sparkContext())
            .cassandraTable("test", "utest", mapRowTo(UUIDData.class));

    JavaRDD<UUIDData> uuids2 = uuids.map(x -> new UUIDData(x.getId() + 10, x.getU()));

    CassandraJavaUtil.javaFunctions(uuids2)
            .writerBuilder("test", "utest", mapToRow(UUIDData.class))
            .saveToCassandra();
}

I've noticed that in your code you're using functions mapRowTo and mapToRow without calling the .class on POJO - are you sure that your code compiled and you don't run any old version of the code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM