- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Spark (v2.1.1) 中有一个包含分层数据的 3 列(如下所示)的数据集。
- My target objective is to assign incremental numbering to each row based on the parent-child hierarchy. Graphically it can be said that the hierarchical data is a collection of trees.
- As per below table, I already have the rows grouped based on 'Global_ID'. Now I would like to generate the 'Value' column in an incremental order but based on the hierarchy of data from 'Parent' and 'Child' columns.
表格表示(值是所需的输出):
+-----------+--------+-------+ +-----------+--------+-------+-------+
| Current Dataset | | Desired Dataset (Output) |
+-----------+--------+-------+ +-----------+--------+-------+-------+
| Global_ID | Parent | Child | | Global_ID | Parent | Child | Value |
+-----------+--------+-------+ +-----------+--------+-------+-------+
| 111 | 111 | 123 | | 111 | 111 | 111 | 1 |
| 111 | 135 | 246 | | 111 | 111 | 123 | 2 |
| 111 | 123 | 456 | | 111 | 123 | 789 | 3 |
| 111 | 123 | 789 | | 111 | 123 | 456 | 4 |
| 111 | 111 | 111 | | 111 | 111 | 135 | 5 |
| 111 | 135 | 468 | | 111 | 135 | 246 | 6 |
| 111 | 135 | 268 | | 111 | 135 | 468 | 7 |
| 111 | 268 | 321 | | 111 | 135 | 268 | 8 |
| 111 | 138 | 139 | | 111 | 268 | 321 | 9 |
| 111 | 111 | 135 | | 111 | 111 | 138 | 10 |
| 111 | 111 | 138 | | 111 | 138 | 139 | 11 |
| 222 | 222 | 654 | | 222 | 222 | 222 | 12 |
| 222 | 654 | 721 | | 222 | 222 | 987 | 13 |
| 222 | 222 | 222 | | 222 | 222 | 654 | 14 |
| 222 | 721 | 127 | | 222 | 654 | 721 | 15 |
| 222 | 222 | 987 | | 222 | 721 | 127 | 16 |
| 333 | 333 | 398 | | 333 | 333 | 333 | 17 |
| 333 | 333 | 498 | | 333 | 333 | 398 | 18 |
| 333 | 333 | 333 | | 333 | 333 | 498 | 19 |
| 333 | 333 | 598 | | 333 | 333 | 598 | 20 |
+-----------+--------+-------+ +-----------+--------+-------+-------+
树表示(期望值在每个节点旁边表示):
+-----+ +-----+
1 | 111 | 17 | 333 |
+--+--+ +--+--+
| |
+---------------+--------+-----------------+ +----------+----------+
| | | | | |
+--v--+ +--v--+ +--v--+ +--v--+ +--v--+ +--v--+
2 | 123 | 5 | 135 | 10 | 138 | | 398 | | 498 | | 598 |
+--+--+ +--+--+ +--+--+ +--+--+ +--+--+ +--+--+
+-----+-----+ +--------+--------+ | 18 19 20
| | | | | |
+--v--+ +--v--+ +--v--+ +--v--+ +--v--+ +--v--+
| 789 | | 456 | | 246 | | 468 | | 268 | | 139 | +-----+
+-----+ +-----+ +-----+ +-----+ +--+--+ +-----+ 12 | 222 |
3 4 6 7 8 | 11 +--+--+
+--v--+ |
| 321 | +------+-------+
+--+--+ | |
9 +--v--+ +--v--+
13 | 987 | 14 | 654 |
+--+--+ +--+--+
|
+--v--+
15 | 721 |
+--+--+
|
+--v--+
16 | 127 |
+--+--+
代码片段:
Dataset<Row> myDataset = spark
.sql("select Global_ID, Parent, Child from RECORDS");
JavaPairRDD<Row,Long> finalDataset = myDataset.groupBy(new Column("Global_ID"))
.agg(functions.sort_array(functions.collect_list(new Column("Parent").as("parent_col"))),
functions.sort_array(functions.collect_list(new Column("Child").as("child_col"))))
.orderBy(new Column("Global_ID"))
.withColumn("vars", functions.explode(<Spark UDF>)
.select(new Column("vars"),new Column("parent_col"),new Column("child_col"))
.javaRDD().zipWithIndex();
// Sample UDF (TODO: Actual Implementation)
spark.udf().register("computeValue",
(<Column Names>) -> <functionality & implementation>,
DataTypes.<xxx>);
经过大量研究并引用了博客中的许多建议,我尝试了以下方法,但无法实现我的方案的结果。
Tech Stack :
Apache Spark (v2.1.1)
Java 8
AWS EMR Cluster (Spark App Deployment)
Data Volume:
- Approximately ~20 million rows in the Dataset
Approaches Tried:
Spark GraphX + GraphFrames:
- Using this combination, I could only achieve the relation between vertices and edges but it doesn't fit for my use case.
Reference: https://graphframes.github.io/user-guide.htmlSpark GraphX Pregel API:
- This is the closest I could get to achieving the expected result but unfortunately I could not find a Java code snippet for the same. The example provided in one of the blogs is in Scala which I am not well versed with.
Reference: https://dzone.com/articles/processing-hierarchical-data-using-spark-graphx-pr
对当前方法的替代(或)修改的任何建议都会非常有帮助,因为我完全迷失了为这个用例找出解决方案。
感谢您的帮助!谢谢!
最佳答案
注意:以下解决方案是在 scala spark 中。您可以轻松地转换为 Java 代码。
检查一下。我尝试使用 Spark Sql 来做,你可以得到一个想法。基本上的想法是在聚合和分组时对 child 、 parent 和全局对象进行排序。一旦按 globalid 分组和排序,展开其余部分。您将获得有序的结果表,稍后您可以zipWithIndex
添加排名(值)
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.functions.udf
val sqlContext = new SQLContext(sc)
import sqlContext.implicits._
val t = Seq((111,111,123), (111,111,111), (111,123,789), (111,268,321), (222,222,654), (222,222,222), (222,721,127), (333,333,398), (333,333,333), (333,333,598))
val ddd = sc.parallelize(t).toDF
val zip = udf((xs: Seq[Int], ys: Seq[Int]) => xs zip ys)
val dd1 = ddd
.groupBy($"_1")
.agg(sort_array(collect_list($"_2")).as("v"),
sort_array(collect_list($"_3")).as("w"))
.orderBy(asc("_1"))
.withColumn("vars", explode(zip($"v", $"w")))
.select($"_1", $"vars._1", $"vars._2").rdd.zipWithIndex
dd1.collect
输出
res24: Array[(org.apache.spark.sql.Row, Long)] = Array(([111,111,111],0), ([111,111,123],1), ([111,123,321],2),
([111,268,789],3), ([222,222,127],4), ([222,222,222],5), ([222,721,654],6),([333,333,333],7), ([333,333,398],8), ([333,333,598],9))
关于java - Apache Spark 中的分层数据操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47996055/
在 Django 中如何处理分层 URL?有什么最佳做法吗?例如。如果我有一个像 /blog/category1/category2/myblogentry 这样的 URL(使用例如 django-m
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
有没有办法在 R 中创建这样的图表? 以下是图表中显示的数据的摘录: df % group_by(Animal) %>% unite(col=Type, Animal:Name, sep =
我一直在努力处理一些时间戳数据(直到现在才需要处理日期,并且它表明)。希望您能帮忙。 我正在处理来自网站的数据,该数据显示每个客户 (ID) 各自的访问以及这些访问的时间戳。它的分组是指一个客户可能有
我一直在努力处理一些时间戳数据(直到现在才需要处理日期,并且它表明)。希望您能帮忙。 我正在处理来自网站的数据,该数据显示每个客户 (ID) 各自的访问以及这些访问的时间戳。它的分组是指一个客户可能有
我正在尝试完成这段代码: ORDER BY IF(j.groups IS NULL OR j.groups = '', IF(j.title IS NULL, i.title), j.groups)
我有一个非常抽象的问题,因为我不确定如何提出它。我的其中一个 View 上有一个 UIImageView。我想让 ImageView 看起来“压入 super View ”。我不确定技术术语是什么,但
我希望 100% 宽的包含图像的 div 位于我的页面下方。在这些 div 之上,我想要一个 1210 像素宽的 div,我可以在其中放置我的内容。 例子: http://mudchallenger.
我目前正在做一个类似于 http://www.beoplay.com/Products/BeoplayA9#under-the-hood 的元素使用 Javascript、HTML5 和 CSS3。我
我想像上面那样创建图像缩略图..为此,我在下面创建了 XML activity_main.xml
我想知道是否可以定义一个分层 MapReduce 作业?。换句话说,我想要一个 map-reduce 作业,在 mapper 阶段将调用不同的 MapReduce 作业。可能吗?您对如何操作有什么建议
程序设计: A 类,实现较低级别的数据处理 类 B-E,为 A 提供更高级别的接口(interface)以执行各种功能 F 类,它是根据用户输入与 B-E 交互的 UI 对象 在任何给定时间只能有一个
CTE 对我来说有点新,所以我希望有人可以帮助我编写的以下内容将采用类别表并从中构建层次结构以进行显示。我知道这种事情一直被问到,但我认为我的排序情况使它有点独特。 我希望有一些使用 Hierarch
我有关于 的问题群 在聚类分析(层次聚类)中。例如,这是 的完全链式的树状图。虹膜数据集 . 我使用后 > table(cutree(hc, 3), iris$Species) 这是输出 : se
数据 我有以下(简化的)数据集,我们称之为 df从现在开始: species rank value 1
Delphi 2009 中的分层窗口和系统菜单存在问题。也就是说,我们的分层窗口(没有边框)没有系统菜单。当我说系统菜单时,我指的是单击应用程序的图标、右键单击其标题栏或(在 Windows 7 中,
我正在制作一个 pototype HMTL5 Canvas 动画,该动画将导出到 Quicktime。 我有一个动态生成的背景,上面有动态屏蔽的元素。 我可以获取要制作的背景,并将其作为逐帧动画(pn
好吧,我有一个打印棋盘的类和另一个打印国际象棋的类 如何使用 LayeredPane 将它们合并在一起,如上面的示例图片所示?我一整天都在尝试,但似乎没有任何效果。我正在使用 JFrame 打印图片。
这是我的场景。我有两个类(class) ClassA 和 ClassB。 B类继承A类。 我在它们两个上使用@Component注释来使它们成为Spring bean。 @Component publ
这不是一道问题题,而是一道使用工具——leiningen——的题。 在一个主项目下创建分层的 lein 项目是否有优势,如果有,优势是什么? 如果我使用 lein new bene-cmp 创建一个项
我是一名优秀的程序员,十分优秀!