【发布时间】:2017-06-05 23:38:11
【问题描述】:
我有一个 CSV 输入文件。我们使用以下内容阅读了这一点
val rawdata = spark.
read.
format("csv").
option("header", true).
option("inferSchema", true).
load(filename)
这会巧妙地读取数据并构建架构。
下一步是将列拆分为 String 列和 Integer 列。怎么样?
如果以下是我的数据集的架构...
scala> rawdata.printSchema
root
|-- ID: integer (nullable = true)
|-- First Name: string (nullable = true)
|-- Last Name: string (nullable = true)
|-- Age: integer (nullable = true)
|-- DailyRate: integer (nullable = true)
|-- Dept: string (nullable = true)
|-- DistanceFromHome: integer (nullable = true)
我想将其拆分为两个变量(StringCols、IntCols),其中:
- StringCols 应该有 "First Name","Last Name","Dept"
- IntCols 应该有 "ID","Age","DailyRate","DistanceFromHome"
这是我尝试过的:
val names = rawdata.schema.fieldNames
val types = rawdata.schema.fields.map(r => r.dataType)
现在在types 中,我想循环查找所有StringType 并在名称中查找列名,与IntegerType 类似。
【问题讨论】:
标签: scala apache-spark apache-spark-sql