【发布时间】:2020-08-16 04:39:52
【问题描述】:
我正在尝试通过读取 csv 在 Spark 中创建数据框,问题是如果我不做任何特别的事情,数据框的每个列类型都会作为字符串:
root
|-- ticker: string (nullable = true)
|-- open: string (nullable = true)
|-- close: string (nullable = true)
|-- adj_close: string (nullable = true)
|-- low: string (nullable = true)
|-- high: string (nullable = true)
|-- volume: string (nullable = true)
|-- date: string (nullable = true)
为了解决这个问题,我将选项“inferSchema”添加为 true,如下所示:
val spark = SparkSession.builder
.appName("Job One")
.master("local")
.config("spark.eventLog.enabled", "true")
.config("spark.eventLog.dir", spark_events)
.getOrCreate()
import spark.implicits._
val df = spark.read
.format("csv")
.option("inferSchema", "true")
.option("header", "true")
.option("mode", "DROPMALFORMED")
.load(historicalStockPrices)
df.printSchema()
通过这种方式我得到了这个:
root
|-- ticker: string (nullable = true)
|-- open: double (nullable = true)
|-- close: double (nullable = true)
|-- adj_close: double (nullable = true)
|-- low: double (nullable = true)
|-- high: double (nullable = true)
|-- volume: long (nullable = true)
|-- date: string (nullable = true)
这正是我想要的,但是添加选项 inferSchema 使得当我不添加它时,这项工作需要 1.4 分钟而不是 6 秒。 另一种获取我想要的类型的列的方法是使用 withColumn,如下所示:
val df2 = df
.withColumn("open",df("open").cast("Float"))
.withColumn("close",df("close").cast("Float"))
.withColumn("adj_close",df("adj_close").cast("Float"))
.withColumn("low",df("low").cast("Float"))
.withColumn("high",df("high").cast("Float"))
.withColumn("volume",df("volume").cast("Long"))
df2.printSchema()
这次整个操作的结果又是6秒而已。 什么给了?
【问题讨论】:
标签: scala dataframe apache-spark apache-spark-sql