Java 阅读来自卡夫卡主题的消息并将其转储到HDFS中

Java 阅读来自卡夫卡主题的消息并将其转储到HDFS中,java,scala,apache-spark,apache-kafka,hdfs,Java,Scala,Apache Spark,Apache Kafka,Hdfs,我试图使用Kafka主题中的数据,将其加载到Dataset,然后在加载到Hdfs之前执行筛选 我能够从卡夫卡主题中消费,将其加载到数据集中,并在HDFS中另存为拼花地板文件,但无法执行过滤条件。您能分享一下在保存到hdfs之前执行过滤的方法吗? 我正在使用Java和Spark来使用卡夫卡主题。 我的部分代码如下所示: DataframeDeserializer dataframe = new DataframeDeserializer(dataset); ds = dataframe.fro

我试图使用Kafka主题中的数据,将其加载到Dataset,然后在加载到Hdfs之前执行筛选

我能够从卡夫卡主题中消费,将其加载到数据集中,并在HDFS中另存为拼花地板文件,但无法执行过滤条件。您能分享一下在保存到hdfs之前执行过滤的方法吗? 我正在使用Java和Spark来使用卡夫卡主题。 我的部分代码如下所示:

DataframeDeserializer dataframe = new DataframeDeserializer(dataset);

 ds = dataframe.fromConfluentAvro("value", <your schema path>, <yourmap>, RETAIN_SELECTED_COLUMN_ONLY$.MODULE$);

StreamingQuery query = ds.coalesce(10)
                .writeStream()
                .format("parquet")
                .option("path", path.toString())
                .option("checkpointLocation", "<your path>")
                .trigger(Trigger.Once())
                .start();
DataframeDeserializer dataframe=新的DataframeDeserializer(数据集);
ds=dataframe.fromConfluentAvro(“值”;
StreamingQuery查询=ds.coalesce(10)
.writeStream()
.格式(“拼花地板”)
.option(“path”,path.toString())
.option(“检查点位置”,“”)
.trigger(trigger.Once())
.start();

coalesce
之前写入过滤器逻辑,即
ds.filter().coalesce()


DataframeDeserializer dataframe=新的DataframeDeserializer(数据集);
ds=dataframe.fromConfluentAvro(“值”;
StreamingQuery查询=
ds
.filter(…)//在此处写入您的筛选条件
.联合(10)
.writeStream()
.格式(“拼花地板”)
.option(“path”,path.toString())
.option(“检查点位置”,“”)
.trigger(trigger.Once())
.start();

我强烈建议不要重新发明轮子。 您只需要HDFS接收器连接器,它将数据从卡夫卡主题复制到HDFS

  • 对于HDFS2.x文件,您可以使用
  • 对于HDFS3.x文件,请使用

来自@Srinivas的答案很好。我想我们可以学习如何工作或使用这个库。假设我有这样的过滤条件:Country='USA'和State='Texas',然后是这样的查询?:StreamingQuery query=ds.filter(“Country=='USA'和State='Texas')。coalesce(10)。writeStream.format(“parquet”).option(“path”,path.toString()).option(“checkpointLocation”,”).trigger(trigger.Once()).start();当然,我会根据你的建议修改代码,并分享结果:)过滤条件按预期工作。谢谢你的帮助。

DataframeDeserializer dataframe = new DataframeDeserializer(dataset);

 ds = dataframe.fromConfluentAvro("value", <your schema path>, <yourmap>, RETAIN_SELECTED_COLUMN_ONLY$.MODULE$);

StreamingQuery query = 
                ds
                .filter(...) // Write your filter condition here
                .coalesce(10)
                .writeStream()
                .format("parquet")
                .option("path", path.toString())
                .option("checkpointLocation", "<your path>")
                .trigger(Trigger.Once())
                .start();