【发布时间】:2019-06-02 22:17:10
【问题描述】:
我的代码正在从 sqlcontext 读取数据。该表中有 2000 万条记录。我想在表中计算 totalCount。
val finalresult = sqlContext.sql(“SELECT movieid,
tagname, occurrence AS eachTagCount, count AS
totalCount FROM result ORDER BY movieid”)
我想在不使用 groupby 的情况下计算一列的总数并将其保存在文本文件中。 .我更改了我的保存文件而无需额外]
>val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
import sqlContext._
case class DataClass(UserId: Int, MovieId:Int, Tag: String)
// Create an RDD of DataClass objects and register it as a table.
val Data = sc.textFile("file:///usr/local/spark/dataset/tagupdate").map(_.split(",")).map(p => DataClass(p(0).trim.toInt, p(1).trim.toInt, p(2).trim)).toDF()
Data.registerTempTable("tag")
val orderedId = sqlContext.sql("SELECT MovieId AS Id,Tag FROM tag ORDER BY MovieId")
orderedId.rdd
.map(_.toSeq.map(_+"").reduce(_+";"+_))
.saveAsTextFile("/usr/local/spark/dataset/algorithm3/output")
// orderedId.write.parquet("ordered.parquet")
val eachTagCount =orderedId.groupBy("Tag").count()
//eachTagCount.show()
eachTagCount.rdd
.map(_.toSeq.map(_+"").reduce(_+";"+_))
.saveAsTextFile("/usr/local/spark/dataset/algorithm3/output2")
错误执行程序:阶段 7.0 (TID 604) 中任务 0.0 中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 at tags$$anonfun$6.apply(tags.scala:46) at tags$$anonfun$6.apply(tags. scala:46) 在 scala.collection.Iterator$$anon$11.next(Iterator.scala:410)
【问题讨论】:
-
您的问题到底是什么?答案很可能在您的帖子中,上面写着
NumberFormatException: For input string: "10]" -
出现此异常的原因及解决方法
-
那么答案肯定在你原来的帖子里。
-
我想在不使用 groupby 的情况下计算一列的总数并将其保存在文本文件中
-
然后,一旦您解决了这个问题,请尝试这样做,如果您遇到困难,请提出另一个问题,显示您编写的代码以及您的特定问题。请参考How to ask a good question
标签: scala apache-spark