I process messages from Kafka with the following JSON structure:
{"unix_time": 1557678233, "category_id": 1000, "ip": "172.10.34.17", "type": "view"}
I want to print out what I'm receiving. Here is a code snippet of what I've done already:
JavaSparkContext sc = createJavaSparkContext();
JavaStreamingContext streamingContext =
new JavaStreamingContext(sc, Durations.seconds(BATCH_DURATION_IN_SECONDS));
SparkSession sparkSession = SparkSession
.builder()
.config(new SparkConf())
.getOrCreate();
Dataset<Row> df = sparkSession
.readStream()
.format("kafka")
.option("kafka.bootstrap.servers", CommonUtils.KAFKA_HOST_PORT)
.option("subscribe", KAFKA_TOPIC)
.load();
StreamingQuery query = df.selectExpr("CAST(value AS STRING)")
.select(from_json(new Column("value"), getSchema())).as("data").
select("data.category_id").writeStream().foreach(new ForeachWriter<Row>() {
@Override
public void process(Row value) {
System.out.println(value);
}
@Override
public void close(Throwable errorOrNull) {
}
@Override
public boolean open(long partitionId, long version) {
return true;
}
})
.start();
query.awaitTermination();
Schema method:
private static StructType getSchema() {
return new StructType(new StructField[]{
new StructField(UNIX_TIME, DataTypes.TimestampType, false, Metadata.empty()),
new StructField(CATEGORY_ID, DataTypes.IntegerType, false, Metadata.empty()),
new StructField(IP, DataTypes.StringType, false, Metadata.empty()),
new StructField(TYPE, DataTypes.StringType, false, Metadata.empty()),
});
}
The problem is that I'm constantly receiving error while writing from Spark:
Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve '
data.category_id' given input columns: [jsontostruct(value)];; 'Project ['data.category_id] +- SubqueryAlias data +- Project [jsontostruct(StructField(unix_time,TimestampType,false), StructField(category_id,IntegerType,false), StructField(ip,StringType,false), StructField(type,StringType,false), value#15) AS jsontostruct(value)#18]
How to overcome this issue? Any suggestions on that?