开发者学堂课程【2020版大数据实战项目之 DMP 广告系统(第七阶段):统一识别_图计算】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/682/detail/11840
统一识别_图计算
统一识别的环境对象已经完成了,回到 mainId 方法中
// 格式:mainId, tads
// mainId ->
主ID,按照顺序取得,第一个非空的就是主 ID
// tags -> GDmale:1,A20:1
// 步骤:
①将数据集转换为 Vertex 和Edge 的数据集
②图计算
③结果聚合
①将数据集转换为 Vertex 和Edge 的数据集
val vertex = idsAndTags.map(item => Vertex(item.mainId,
item.ids, item.tags))
// vertex
可以直接通过idsAndTags
进行map, idsAndTags
中的类型是 IdsWithTags,
item 转成 Vertex 对象,
Vertex 对象第一项接收 id, item 有一个mainId, 第二项 item
中有一个 ids, 第三项item 中有一个 tags。
生成之后val edge = idsAndTags.flatMap(item => {
// uuid -> lasdkfakjh mac -> jaksdhfiah1231kjjh
// 一定要注意边的生成,是一对多的,是一个笛卡尔积
val ids: Map[String, String] = item.ids
val result: immutable.Iterable[Edge] = for (id <- ids; otherId
<- ids if id != otherId) yield Edge(id Edge(id._2,otherId._2)
result
})
// 生成一个边的集合 edge , 拿到 idsAndTags.Map(item ,生
成 Edge , Edge 中第一项是一个 source, 第二项是一个
destination, 但这样不能直接创建出 Edge;所以先拿到 ids, 可
通过 item.ids 获取到,ids是一个 map 类型,对应的第一项是 id
的 key 也是类型,第二项是id 的值“value”;
Ids 中可能是 uuid 对应 lasdkfakjh, 第二项 mac 对应
jaksdhfiah1231kjjh
,uuid 要和 mac 地址连接起来。生成全链接,
用 for 循环,拿到 id, 从 ids 中取出一个 id, 它对应的是一个元
组,第一项是key,第二项是“value”,使用分号,嵌套 for 循环,
拿到 otherId <- ids
,判断 if id != otherId
,写一个 yield 生成
Edge, id 和 otherId 取其中的第二项,数据集命名为val result;
展平使用 flatMap,Edge生成了,也生成了多个边。
// ②图计算
Val components = GraphFrame(vertex.toDF(), edge.toDF()).connectedComponents.run()
// GraphFrame
接收的对象为 vertex 和 edge 的数据集,但 GraphFrame 只能接收 dateFrame, 所以需使用 toDF, 进行connectedComponents.run
就获取到了components
。
// ③聚合
}
注意点:
边的生成是一对多的,是一个笛卡尔积