【发布时间】:2017-05-04 19:01:09
【问题描述】:
所以我有一个大型数据集,它是 stackoverflow 用户群的样本。该数据集中的一行如下:
<row Id="42" Reputation="11849" CreationDate="2008-08-01T13:00:11.640" DisplayName="Coincoin" LastAccessDate="2014-01-18T20:32:32.443" WebsiteUrl="" Location="Montreal, Canada" AboutMe="A guy with the attention span of a dead goldfish who has been having a blast in the industry for more than 10 years.

Mostly specialized in game and graphics programming, from custom software 3D renderers to accelerated hardware pipeline programming." Views="648" UpVotes="337" DownVotes="40" Age="35" AccountId="33" />
我想从信誉中提取数字,在本例中为“11849”,从年龄中提取数字,在本例中为“35”,我想将它们作为浮点数。
该文件位于 HDFS 中,因此它采用 RDD 格式
val linesWithAge = lines.filter(line => line.contains("Age=")) //This is filtering data which doesnt have age
val repSplit = linesWithAge.flatMap(line => line.split("\"")) //Here I am trying to split the data where there is a "
所以当我用引号将其拆分时,声誉在索引 3 中,年龄在索引 23 中,但是我如何将它们分配给地图或变量,以便我可以将它们用作浮点数。 我还需要它为 RDD 上的每一行执行此操作。
编辑:
val linesWithAge = lines.filter(line => line.contains("Age=")) //transformations from the original input data
val repSplit = linesWithAge.flatMap(line => line.split("\""))
val withIndex = repSplit.zipWithIndex
val indexKey = withIndex.map{case (k,v) => (v,k)}
val b = indexKey.lookup(3)
println(b)
因此,如果向数组添加了一个索引,现在我已经成功地将它分配给一个变量,但我只能对 RDD 中的一个项目执行此操作,有谁知道我如何对所有项目执行此操作?
【问题讨论】:
-
您正在寻找
map函数。快速搜索找到了这个例子:backtobazics.com/big-data/spark/apache-spark-map-example -
映射函数以什么函数为参数?我想为数据集中的每一行在 3 和 23 处收集索引。请你举个例子,因为我已经尝试过使用地图功能。
-
一个函数,它接受一个数组并产生一个由两个数字组成的元组:
f: Array[String] => (Int, Int)也许你可以试试看?如果你还不知道怎么做,这里有很多学习资源。 -
你能检查一下我所做的编辑吗@maasg
-
你检查这些操作的结果了吗?你会得到什么样的结果?
标签: scala hadoop apache-spark