Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从scala spark shell数组中读取字符串[String]_Scala - Fatal编程技术网

如何从scala spark shell数组中读取字符串[String]

如何从scala spark shell数组中读取字符串[String],scala,Scala,我有一个XML文件,我正试图使用Scala通过Spark Shell处理它。我陷入了一个需要使用Scala的 scala> val fileRead = sc.textFile("source_file") fileRead: org.apache.spark.rdd.RDD[String] = source_file MapPartitionsRDD[8] at textFile at <console>:21 scala> val strLines = fileRe

我有一个XML文件,我正试图使用Scala通过Spark Shell处理它。我陷入了一个需要使用Scala的

scala> val fileRead = sc.textFile("source_file")
fileRead: org.apache.spark.rdd.RDD[String] = source_file MapPartitionsRDD[8] at textFile at <console>:21

scala> val strLines = fileRead.map(x => x.toString)
strLines: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[9] at map at <console>:23

scala> val fltrLines = strLines.filter(_.contains("<record column1="))
fltrLines: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[10] at filter at <console>:25

scala> fltrLines.take(5)
res1: Array[String] = Array("<record column1="1" column2="1" column3="5" column4="2010-11-02T18:59:01.140" />", "<record column2=....

然后使用xmlLines.attributes等。

您可以执行
fltrLines.map{scala.xml.xml.loadString}
,它应该用字符串构建
Elem
s。检查,注意这是一个旧的Scaladoc,当Scala std.lib出现时。现在仍然包含XML,它驻留在一个单独的jar文件中。因此,如果您使用的是较新的版本,请确保将正确的jar放在类路径中。

Perfect,我最后这样做了
scala>val element=fltrLines.map{scala.xml.xml.loadString}
。现在我尝试用逗号分隔符连接属性
scala>val elementAttributes=element.map(u.attributes(column1))并连接逗号,然后连接column2等
尝试筛选您提供的文档。非常感谢。我已经接受了这个答案,但显然我没有15个声望来支持你的答案。
"<record column1="1" column2="1" column3="5" column4="2010-11-02T18:59:01.140" />"
val xmlLines = fltrLines.....somehow get the value of the value of Array[String] first index