我有一个CSV输入文件。我们阅读使用以下内容如何将列分成两组?
val rawdata = spark.
read.
format("csv").
option("header", true).
option("inferSchema", true).
load(filename)
这整齐地读取数据并构建架构。
下一步是将列拆分为字符串和整数列。怎么样?
如果以下是我的数据集的架构......
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应有“姓”,“姓”,“部门”
- IntCols应该有“ID”,“年龄”,“DailyRate”,“DistanceFromHome”
这是我曾尝试:
val names = rawdata.schema.fieldNames
val types = rawdata.schema.fields.map(r => r.dataType)
现在types
,我想循环并找到所有StringType
和地名查找起来的列名,同样为IntegerType
。
谢谢,这就是我正在看。我做的错误是,不包括sql.types。{IntegerType,StringType} 我在做以下并获得空列表 VAL TST = rawdata.schema.filter(C => c.dataType == “StringType”) 代替 VAL TST = rawdata.schema.filter (c => c.dataType == StringType) 非常感谢。 Regards Bala –