Scala:GroupBy如何对String值求和?

6 json scala apache-spark rdd spark-dataframe

我有RDD [Row]:

  |---itemId----|----Country-------|---Type----------|
  |     11      |     US           |      Movie      | 
  |     11      |     US           |      TV         | 
  |     101     |     France       |      Movie      |     
Run Code Online (Sandbox Code Playgroud)

如何组合GroupBy itemId以便我可以将结果保存为json的List,其中每行是单独的json对象(RDD中的每一行):

{"itemId" : 11, 
"Country": {"US" :2 },"Type": {"Movie" :1 , "TV" : 1} },
{"itemId" : 101, 
"Country": {"France" :1 },"Type": {"Movie" :1} }
Run Code Online (Sandbox Code Playgroud)

RDD:

我试过了 :

import com.mapping.data.model.MappingUtils
import com.mapping.data.model.CountryInfo


val mappingPath = "s3://.../"    
val input = sc.textFile(mappingPath)
Run Code Online (Sandbox Code Playgroud)

输入是jsons的列表,其中每一行是json,我使用MappingUtils映射到POJO类CountryInfo,它负责JSON解析和转换:

val MappingsList = input.map(x=> {
                    val countryInfo = MappingUtils.getCountryInfoString(x);
                    (countryInfo.getItemId(), countryInfo)
                 }).collectAsMap

MappingsList: scala.collection.Map[String,com.mapping.data.model.CountryInfo] 


def showCountryInfo(x: Option[CountryInfo]) = x match {
      case Some(s) => s
   }


val events = sqlContext.sql( "select itemId  EventList")

val itemList =  events.map(row => {
    val itemId = row.getAs[String](1);
    val çountryInfo =  showTitleInfo(MappingsList.get(itemId));
    val country = if (countryInfo.getCountry() == 'unknown)' "US" else countryInfo.getCountry()
    val type = countryInfo.getType()

    Row(itemId, country, type)
      })
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何实现这一目标?

谢谢!

Syn*_*sso 3

我无法承担额外的时间来完成此任务,但可以为您提供一个开始。

这个想法是将其聚合RDD[Row]到一个代表 JSON 结构的 Map 中。聚合是需要两个函数参数的折叠:

  1. seqOp如何将元素集合折叠为目标类型
  2. combOp如何合并两种目标类型。

棘手的部分在合并时出现combOp,因为您需要累积seqOp. 我把这个作为练习,因为我要赶飞机!如果您遇到麻烦,希望其他人可以填补空白。

  case class Row(id: Int, country: String, tpe: String)

  def foo: Unit = {

    val rows: RDD[Row] = ???

    def seqOp(acc: Map[Int, (Map[String, Int], Map[String, Int])], r: Row) = {
      acc.get(r.id) match {
        case None => acc.updated(r.id, (Map(r.country, 1), Map(r.tpe, 1)))
        case Some((countries, types)) =>
          val countries_ = countries.updated(r.country, countries.getOrElse(r.country, 0) + 1)
          val types_ = types.updated(r.tpe, types.getOrElse(r.tpe, 0) + 1)
          acc.updated(r.id, (countries_, types_))
      }
    }

    val z = Map.empty[Int, (Map[String, Int], Map[String, Int])]

    def combOp(l: Map[Int, (Map[String, Int], Map[String, Int])], r: Map[Int, (Map[String, Int], Map[String, Int])]) = {
      l.foldLeft(z) { case (acc, (id, (countries, types))) =>
          r.get(id) match {
            case None => acc.updated(id, (countries, types))
            case Some(otherCountries, otherTypes) => 
              // todo - continue by merging countries with otherCountries
              // and types with otherTypes, then update acc
          }
      }
    }

    val summaryMap = rows.aggregate(z) { seqOp, combOp }
Run Code Online (Sandbox Code Playgroud)