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 ?
In scala you can use Options as well for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With