- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 RDD[Row] :
|---itemId----|----Country-------|---Type----------|
| 11 | US | Movie |
| 11 | US | TV |
| 101 | France | Movie |
{"itemId" : 11,
"Country": {"US" :2 },"Type": {"Movie" :1 , "TV" : 1} },
{"itemId" : 101,
"Country": {"France" :1 },"Type": {"Movie" :1} }
import com.mapping.data.model.MappingUtils
import com.mapping.data.model.CountryInfo
val mappingPath = "s3://.../"
val input = sc.textFile(mappingPath)
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)
})
最佳答案
我负担不起额外的时间来完成这个,但可以给你一个开始。
这个想法是你聚合 RDD[Row]
分解成一个代表您的 JSON 结构的 Map。聚合是一个折叠,需要两个函数参数:
seqOp
如何将元素集合折叠成目标类型 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 }
关于json - 斯卡拉 : How to do GroupBy sum for String values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454277/
scala 中是否有任何等效于 C# 部分类的东西?我想用这样的对象离开我的功能: // file 1: object MainClass { def addValue(value: AnyR
你能定义一组变量供以后使用吗? 这里有一些伪代码突出了我的意图: def coordinates = x1, y1, x2, y2 log("Drawing from (%4.1f, %4.1f) t
在我的应用程序中,我有很多地方需要获取元组列表,按元组的第一个元素对其进行分组,然后将其从其余元素中删除。例如,我有元组 (1, "Joe", "Account"), (1, "Tom", "Empl
我在 Scala 中的类声明遇到了麻烦: class Class2[ A, B class Foo extends Class3[String, Foo] define
给定以下场景 val items = List("a", "b", "c", 1, 2, 3, false, true) def intItems = items.collect {case i :
我有一个我想忽略的日期列表: private val excludeDates = List( new DateTime("2015-07-17"),
我想创建一个方法,它将一个选项数组和一个默认值作为参数,并返回第一个非空选项,否则返回默认值: def customGetOrElse[T](options : Array[Option[T]], d
试图生成一个显示素因数多重性的元组列表......这个想法是将排序列表中的每个整数与元组中的第一个值相匹配,使用第二个值进行计数。使用 takeWhile 可能更容易做到这一点,但是嗯。不幸的是,我的
我是 Scala 新手,但有一些 Java 背景。 在编写 Scala 代码时,以这种方式处理 Option 参数很有用: val text = Option("Text") val length =
这个问题已经有答案了: Use of def, val, and var in scala (6 个回答) 已关闭 9 年前。 我正在寻找一种方法来解决 Scala 中的以下编译错误。我正在尝试更新变
我有一种情况,我想作为 future 并发执行多个任务,这样如果其中一个任务失败,其他任务仍然会执行。如果失败,我想记录它的错误。我希望我的父线程能够判断每个线程是否成功,然后根据它执行一些操作。例如
我被教导了 formal systems在大学时,但我很失望他们似乎并没有被真正使用。 我喜欢能够知道某些代码(对象、函数等)是否有效的想法,而不是通过测试,而是通过证明。 我相信我们都熟悉物理工程和
wiki 上的示例似乎工作得很好,但是我的问题更多是关于如何实现此结果以及如何使用 Eclipsify util 最终将项目(带有子项目)导入 Eclipse。 https://github.com/
我玩了一下占位符,发现了一个奇怪的情况: val integers = Seq(1, 2) val f = (x:Int) => x + 1 integers.map((_, f(_))) 返回 Se
使用 Slick,您可以执行以下操作以从表中生成结果流: val q = for (e println(s"Event: $s") } 这将打印 events 中的所有事件表并在最后一行之后终止。
我想在我的 Scala 摆动应用程序中使用一棵树,但该组件在 API 中不可用。 是否包装了 JTree存在吗? 如果没有,你对制作有什么建议吗? 谢谢 最佳答案 即使您可以在 Scala 程序中直接
我一直在试图了解莫纳德州。虽然使用起来并不总是那么容易,但是它的用法并不多。但是,我发现有关Monad州的每一次讨论都具有基本相同的信息,而且总会有一些我不理解的地方。 以this帖子为例。作者具有以
拜托,对不起我的英语:(让我们通过例子来解释我的问题。我们有一个数组a: var a = Array(1,1,1,1,2) 我们可以: 过滤a: a.filter( _ a.coun
为什么queue.get( ) 返回空列表? class MyQueue{ var queue=List[Int](3,5,7) def get(){ this.queue.head
Scala 2.10引入了value classes。它们对于编写类型安全代码非常有用。此外,还有一些限制,其中一些将被编译器检测到,而某些则需要在运行时分配。 我想使用case class语法创建值
我是一名优秀的程序员,十分优秀!