Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spark结构化流读取Kafka Connect JSONConverter消息和模式_Json_Scala_Apache Kafka_Apache Spark Sql_Spark Structured Streaming - Fatal编程技术网

使用Spark结构化流读取Kafka Connect JSONConverter消息和模式

使用Spark结构化流读取Kafka Connect JSONConverter消息和模式,json,scala,apache-kafka,apache-spark-sql,spark-structured-streaming,Json,Scala,Apache Kafka,Apache Spark Sql,Spark Structured Streaming,我试图阅读卡夫卡主题的信息。消息的格式如下(示例格式): {“schema”:{“type”:“struct”,“name”:“emp_table”,“fields”:[{“field”:“emp_id”,“type”:“emp_name”,“type”:“string”},{“field”:“city”,“type”:“string”},{“field”:“emp_-sal”,“type”:“string”},{“field”:“manager_-name”,“type”:“string”},

我试图阅读卡夫卡主题的信息。消息的格式如下(示例格式):

{“schema”:{“type”:“struct”,“name”:“emp_table”,“fields”:[{“field”:“emp_id”,“type”:“emp_name”,“type”:“string”},{“field”:“city”,“type”:“string”},{“field”:“emp_-sal”,“type”:“string”},{“field”:“manager_-name”,“type”:“string”},},“payload”:“emp_-id”:“1”,“emp_-name”:“abc”,“city”:“NYK”,“emp_-sal”,“emp-sal”;“manager”}

另外,请注意,主题包含来自不同表的消息,而不仅仅是一个表

我试图实现的是使用Spark Structured Streaming阅读来自Kafka主题的上述消息,并创建一个具有列名的数据框架,其值都来自JSON消息本身

我不想使用case类或StructType显式定义模式

我试过这个:

val df = spark.readStream.format("kafka").option("kafka.bootstrap.servers", brokers).option("subscribe", "topic1").option("startingOffsets", "earliest").load()

val y=df.select(get_json_object(($"value"), "$.payload").alias("payload")
当我查看Y(这是一个数据帧)时,它作为1列出现,在该列的有效负载下的值为JSON

如何获取数据帧中的单个列?我不能做到这一点


(再次重申,我不能对模式部分使用泛型case类或StructType,因为通过Kafka消息传递的消息来自不同的表,因此我希望在运行时从JSON本身创建更多的动态模式。)

选项1:将Kafka连接源更改为set
value.converter.schemas.enable=false
。这只会给你(打开的有效载荷开始),然后你可以跳到下面的文章

否则,在剥离连接模式后,需要使用来自_json()的
应用模式

val y = df.select(get_json_object($"value", "$.payload").alias("payload"))
val z = df.select(from_json($"payload", schema))
所有字段都是字符串,因此

val schema: StructType = StructType(Seq(
  StructField("emp_id", StringType()),
  StructField("emp_name", StringType()),
  StructField("city", StringType()),
  StructField("emp_sal", StringType()),
  StructField("manager_name", StringType())
))
相关的


Umh,您可能可以通过点表示法访问单个值,payload.schema.type应该返回“struct”作为值我认为您需要将值转换为字符串(从字节数组),然后才能使用
get\u json\u object
Hi@OneCriketeer,如果我的有效负载是这样的“有效负载”:{“数据”:{“名称字段”:“Myname”,“另一个字段”:[{“value”:{“unitField”:“MyUnit”,“valueField”:“27”}}],“timestampField”:“2020-08-01T18:00:00”}“``,我需要定义某种嵌套结构吗?嗯。在这里查找:@Minnie
另一个字段
需要是数组类型,是的,我被卡住了,我将在下面发布我的问题以保持主题的集中。@Minnie请创建一个新帖子,而不是在接受答案后再问问题。@Minnie