Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save null Values in Cassandra using DataStax Spark Connector

I try to save stream data into Cassandra using Spark and Cassandra Spark Connector.

I made something like the following:

Create a Model Class:

public class ContentModel {
    String id;

    String available_at; //may be null

  public ContentModel(String id, String available_at){
     this.id=id;
     this.available_at=available_at,
  }
}

Mapping Streaming content to Model:

JavaDStream<ContentModel> contentsToModel = myStream.map(new Function<String, ContentModel>() {
        @Override
        public ContentModel call(String content) throws Exception {

            String[] parts = content.split(",");
            return new ContentModel(parts[0], parts[1]);
        }
    });

Save:

CassandraStreamingJavaUtil.javaFunctions(contentsToModel).writerBuilder("data", "contents", CassandraJavaUtil.mapToRow(ContentModel.class)).saveToCassandra();

If some values are null I get the following error:

com.datastax.spark.connector.types.TypeConversionException: Cannot convert object null to struct.ValueRepr.

Is there a way to store null values using Spark Cassandra Connector ?

like image 420
theShadow89 Avatar asked Oct 07 '15 14:10

theShadow89


1 Answers

In scala you can use Options as well for this.

like image 161
Yatin Goyal Avatar answered Nov 05 '22 17:11

Yatin Goyal