【问题标题】:How can I read a dataframe using spark streaming with it's schema that I specify如何使用我指定的模式使用火花流读取数据帧
【发布时间】:2019-11-13 04:37:08
【问题描述】:

我正在尝试使用 Spark 流将 csv 文件从 AWS S3 读取到数据帧中,但是数据没有存储在所需的列中,而是仅输入 1 列,其他列为空。 需要一种方法如何将 csv 文件作为格式的输入。

我已尝试添加架构。 删除架构并尝试推断架构状态必须指定架构。

var schema = StructType(
  StructField("date", StringType, true) ::
    StructField("close",StringType, true) ::
    StructField("volume", StringType, true) ::
    StructField("open", StringType, true) ::
    StructField("high",StringType,true) ::
    StructField("low", StringType,true) :: Nil)

val ds = spark
  .readStream
  .option("sep", ";")
  .format("csv")
  .option("thousands",",")
  .schema(schema)
  .option("header",true)
  .load(path)

val df = ds.select("*")

df.writeStream.outputMode("append")
  .format("console")
  .trigger(Trigger.ProcessingTime("5 seconds"))
  .start("/home/admin1/IdeaProjects/StockPricePrediction/src/main/output/")
  .awaitTermination()

我希望数据框在每列中都有数据,但它显示如下:

Batch: 0
-------------------------------------------
19/07/02 18:53:46 INFO CodeGenerator: Code generated in 20.170544 ms
+--------------------+-----+------+----+----+----+
|                date|close|volume|open|high| low|
+--------------------+-----+------+----+----+----+
|0,2019/06/28,1080...| null|  null|null|null|null|
|1,2019/06/27,1076...| null|  null|null|null|null|
|2,2019/06/26,1079...| null|  null|null|null|null|
|3,2019/06/25,1086...| null|  null|null|null|null|
|4,2019/06/24,1115...| null|  null|null|null|null|
+--------------------+-----+------+----+----+----+

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你的输入数据是什么样子的?
  • 看起来你的数据在你提到的地方分开;作为分隔符。

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


【解决方案1】:

您的分隔符似乎设置不正确。 由于所有数据似乎都聚集在日期列中。

.option("delimiter", ",")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 2019-02-14
    • 2023-03-18
    • 2016-12-16
    • 2018-10-27
    • 2019-05-12
    • 2018-12-30
    • 2019-06-01
    相关资源
    最近更新 更多