我对Scala和Apache都很陌生。我的文本文件包含的条目如下:
[-0.9704851405656525,1.0286638765434661]
[-0.9704851405656525,1.0286638765434661]
[-1.0353873234576638,-0.001849782262230898]
[-0.9704851405656525,1.0286638765434661]
[-0.9704851405656525,1.0286638765434661]
....我想从这里创建数据格式。要使用sql查询,我的代码如下所示,
def processr(str:String) = str.replaceAll("\\[", "").replaceAll("\\]","")
case class Result(a:Double, b:Double)
val filemat = sc.textFile("mat.txt")
val result = filemat.map(s => s.split(',').map(r=>Result(processr(r[0]).toDouble, processr(r[1]).toDouble)).toDF.cache我犯了个错误,
<console>:1: error: identifier expected but integer literal found.
val result = filemat.map(s => s.split(',').map(r=>Result(processr(r[0]).toDouble, processr(r[1]).toDouble)).toDF.cache我不确定,我在代码中犯了什么错误。看来我的分裂方法不对。有人能建议我如何进入数据仓库吗?提前谢谢。
发布于 2015-10-08 10:32:46
你应该使用圆括号,而不是方括号。从Scala中的数组中提取只是一个apply方法调用:
scala> val r = "[-0.9704851405656525,1.0286638765434661]".split(",")
r: Array[String] = Array([-0.9704851405656525, 1.0286638765434661])
scala> r.apply(0)
res4: String = [-0.9704851405656525加上一些语法糖:
scala> r(0)
res5: String = [-0.9704851405656525接下来你的地图看上去不对。当您调用s.split时,您将得到一个Array[String],因此r实际上是一个String,r(0)给出的是-或第一个数字。你可能想要这样的东西:
filemat.map(_.split(',') match {
case Array(s1, s2) => Result(processr(s1).toDouble, processr(s2).toDouble)
})可以通过使用模式匹配和regex来简化它:
val p = "^\\[(-?[0-9]+\\.[0-9]+),(-?[0-9]+\\.[0-9]+)\\]$".r
filemat.map{
case p(s1, s2) => Result(s1.toDouble, s2.toDouble)
}或者使用Row.fromSeq方法:
val schema = StructType(Seq(
StructField("a", DoubleType, false),
StructField("b", DoubleType, false)))
val p1 = "(-?[0-9]+\\.[0-9]+)".r
sqlContext.createDataFrame(filemat.map(s =>
Row.fromSeq(p1.findAllMatchIn(s).map(_.matched.toDouble).toSeq)),
schema)https://stackoverflow.com/questions/33013051
复制相似问题