首页 > 解决方案 > 从未解析的文本字符串中创建数据框

问题描述

我正在使用 Scala 和 Spark 来分析一些数据。对不起,我在这方面绝对是新手。

我有以下格式的数据(如下)我想创建 RDD 来过滤数据、分组和转换数据。

目前我有未解析字符串列表的rdd,我从rawData创建了它:字符串列表

val rawData  ( this is ListBuffer[String] )
val rdd = sc.parallelize(rawData)

如何创建数据集来操作数据?我想在 Rdd 中有具有命名字段行 ob.name、obj.year 等的对象正确的方法是什么?

我应该为此创建数据框吗?

原始数据字符串如下所示:这是字符串列表,具有空格分隔的值

列含义:“name”、“year”、“month”、“tmax”、“tmin”、“afdays”、“rainmm”、“sunhours”

aberporth    1941  10    ---     ---    ---    106.2     ---
aberporth    1941  11    ---     ---    ---     92.3     ---
aberporth    1941  12    ---     ---    ---     86.5     ---
aberporth    1942   1    5.8     2.1    ---    114.0    58.0
aberporth    1942   2    4.2    -0.6    ---     13.8    80.3
aberporth    1942   3    9.7     3.7    ---     58.0   117.9
aberporth    1942   4   13.1     5.3    ---     42.5   200.1
aberporth    1942   5   14.0     6.9    ---    101.1   215.1
aberporth    1942   6   16.2     9.9    ---      2.3   269.3
aberporth    1942   7   17.4    11.3    12     70.2*   185.0
aberporth    1942   8   18.7    12.3    5-     78.5   141.9
aberporth    1942   9   16.4    10.7    123    146.8   129.1#
aberporth    1942  10   13.1     8.2    125    131.1    82.1l

--- - 表示没有数据,我想我可以把 0 放在这个列上。

70.2* , 129.1# , 82.l - * , # 和 l 这里应该被过滤

请指出我正确的方向。

我在这里找到了一种可能的解决方案: https ://medium.com/@mrpowers/manually-creating-spark-dataframes-b14dae906393

这个例子看起来不错:

val someData = Seq(
  Row(8, "bat"),
  Row(64, "mouse"),
  Row(-27, "horse")
)

val someSchema = List(
  StructField("number", IntegerType, true),
  StructField("word", StringType, true)
)

val someDF = spark.createDataFrame(
  spark.sparkContext.parallelize(someData),
  StructType(someSchema)
)

如何将字符串列表转换为 Seq of Row ?

标签: scalaparsingapache-sparkdataframerdd

解决方案


您可以将数据作为文本文件读取,并替换---0和删除特殊字符或过滤掉。(我在下面的例子中替换了)

创建一个案例类来表示数据

case class Data(
                 name: String, year: String, month: Int, tmax: Double,
                 tmin: Double, afdays: Int, rainmm: Double, sunhours: Double
             )

读取文件

val data = spark.read.textFile("file path")  //read as a text file
  .map(_.replace("---", "0").replaceAll("-|#|\\*", ""))  //replace special charactes 
  .map(_.split("\\s+"))
  .map(x =>  // create Data object for each record
    Data(x(0), x(1), x(2).toInt, x(3).toDouble, x(4).toDouble, x(5).toInt, x(6).toDouble, x(7).replace("l", "").toDouble)
  )

现在你得到一个Dataset[Data]从文本中解析出来的数据集。

输出:

+---------+----+-----+----+----+------+------+--------+
|name     |year|month|tmax|tmin|afdays|rainmm|sunhours|
+---------+----+-----+----+----+------+------+--------+
|aberporth|1941|10   |0.0 |0.0 |0     |106.2 |0.0     |
|aberporth|1941|11   |0.0 |0.0 |0     |92.3  |0.0     |
|aberporth|1941|12   |0.0 |0.0 |0     |86.5  |0.0     |
|aberporth|1942|1    |5.8 |2.1 |0     |114.0 |58.0    |
|aberporth|1942|2    |4.2 |0.6 |0     |13.8  |80.3    |
|aberporth|1942|3    |9.7 |3.7 |0     |58.0  |117.9   |
|aberporth|1942|4    |13.1|5.3 |0     |42.5  |200.1   |
|aberporth|1942|5    |14.0|6.9 |0     |101.1 |215.1   |
|aberporth|1942|6    |16.2|9.9 |0     |2.3   |269.3   |
|aberporth|1942|7    |17.4|11.3|12    |70.2  |185.0   |
|aberporth|1942|8    |18.7|12.3|5     |78.5  |141.9   |
|aberporth|1942|9    |16.4|10.7|123   |146.8 |129.1   |
|aberporth|1942|10   |13.1|8.2 |125   |131.1 |82.1    |
+---------+----+-----+----+----+------+------+--------+

我希望这有帮助!


推荐阅读