java.lang.IllegalArgumentException:未指定'path'// Spark Consumer Issue

时间:2019-01-08 09:55:01

标签: scala apache-spark spark-structured-streaming

我正在尝试创建SparkConsumer,以便在这种情况下可以通过Spark Streaming将CSV文件发送到Kafka。但是我有一个错误,未指定“路径”。 请参阅下面的代码

我的代码如下:

import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.execution.streaming.FileStreamSource.Timestamp
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.functions.from_json
import org.apache.spark.sql.streaming.OutputMode

object sparkConsumer extends App {

  val conf = new SparkConf().setMaster("local").setAppName("Name")
  val sc = new SparkContext(conf)

  val rootLogger = Logger.getRootLogger()
  rootLogger.setLevel(Level.ERROR)

  val spark = SparkSession
    .builder()
    .appName("Spark-Kafka-Integration")
    .master("local")
    .getOrCreate()

  val schema = StructType(Array(
    StructField("InvoiceNo", StringType, nullable = true),
    StructField("StockCode", StringType, nullable = true),
    StructField("Description", StringType, nullable = true),
    StructField("Quantity", StringType, nullable = true)
  ))

  val streamingDataFrame = spark.readStream.schema(schema).csv("C:/Users/me/Desktop/Tasks/Tasks1/test.csv")

  streamingDataFrame.selectExpr("CAST(InvoiceNo AS STRING) AS key", "to_json(struct(*)) AS value").
    writeStream
    .format("csv")
    .option("topic", "topic_test")
    .option("kafka.bootstrap.servers", "localhost:9092")
    .option("checkpointLocation", "C:/Users/me/IdeaProjects/SparkStreaming/checkpointLocation/")
    .start()

  import spark.implicits._
  val df = spark
    .readStream
    .format("kafka")
    .option("kafka.bootstrap.servers", "localhost:9092")
    .option("subscribe", "topic_test")
    .load()

  val df1 = df.selectExpr("CAST(value AS STRING)", "CAST(timestamp AS TIMESTAMP)").as[(String, Timestamp)]
    .select(from_json($"value", schema).as("data"), $"timestamp")
    .select("data.*", "timestamp")

  df1.writeStream
    .format("console")
    .option("truncate","false")
    .outputMode(OutputMode.Append)
    .start()
    .awaitTermination()

}

我出现以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: 'path' is not specified

有人知道我想念什么吗?

1 个答案:

答案 0 :(得分:1)

在您的代码的这一部分似乎可能是一个问题:

  streamingDataFrame.selectExpr("CAST(InvoiceNo AS STRING) AS key", "to_json(struct(*)) AS value").
    writeStream
    .format("csv")
    .option("topic", "topic_test")
    .option("kafka.bootstrap.servers", "localhost:9092")
    .option("checkpointLocation", "C:/Users/me/IdeaProjects/SparkStreaming/checkpointLocation/")
    .start()

因为您使用的是“ csv”格式,但未设置所需的文件位置。相反,您可以配置Kafka属性以将kafka主题用作接收器。因此,如果您将格式更改为“ kafka”,则应该可以使用。

您可以尝试使用csv作为源的另一个问题是您的路径应该是目录而不是文件。就您而言,如果您创建目录并移动csv文件,它将起作用。

仅出于测试目的,请创建一个名为C:/Users/me/Desktop/Tasks/Tasks1/test.csv的目录,并创建一个内部名为part-0000.csv的文件。然后将您的csv内容包含在这个新文件中,然后重新开始该过程。