一、单表关联

给出child-parent(孩子——父母)表,要求输出grandchild-grandparent(孙子——祖父母)表

二、maven设置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mk</groupId><artifactId>spark-test</artifactId><version>1.0</version><name>spark-test</name><url>http://spark.mk.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><scala.version>2.11.1</scala.version><spark.version>2.4.4</spark.version><hadoop.version>2.6.0</hadoop.version></properties><dependencies><!-- scala依赖--><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>${scala.version}</version></dependency><!-- spark依赖--><dependency><groupId>org.apache.spark</groupId><artifactId>spark-core_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-sql_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin></plugins></pluginManagement></build>
</project>

三、编程代码

public class SingleTableJoinApp implements SparkConfInfo {public static void main(String[] args) {String filePath = "E:\\spark\\childParent.txt";SparkSession sparkSession = new SingleTableJoinApp().getSparkConf("childParent");JavaPairRDD<String, String> childParent = sparkSession.sparkContext().textFile(filePath, 4).toJavaRDD().flatMap(v -> Arrays.asList(v.split("\n")).iterator()).mapToPair(v -> {if(v.matches("\\s+child\\s+parent\\s+")){return null;}String[] data = v.split("\\s+");if (data.length != 2) {return null;}return new Tuple2<>(data[0],data[1]);}).filter(v -> v != null).cache();JavaPairRDD<String, String> parentChild = childParent.mapToPair(v->new Tuple2(v._2, v._1));JavaPairRDD<String, Tuple2<String, String> >  joinRdd = parentChild.join(childParent);List<Tuple2<String, String>> childGrand = joinRdd.mapToPair(v->new Tuple2<>(v._2._1, v._2._2)).sortByKey(true).collect();System.out.println("child\t\tgrand");childGrand.forEach(v -> System.out.println(v._1 + "\t\t" + v._2));sparkSession.stop();}
}public interface SparkConfInfo {default SparkSession getSparkConf(String appName){SparkConf sparkConf = new SparkConf();if(System.getProperty("os.name").toLowerCase().contains("win")) {sparkConf.setMaster("local[4]");System.out.println("使用本地模拟是spark");}else{sparkConf.setMaster("spark://hadoop01:7077,hadoop02:7077,hadoop03:7077");sparkConf.set("spark.driver.host","192.168.150.1");//本地ip,必须与spark集群能够相互访问,如:同一个局域网sparkConf.setJars(new String[] {".\\out\\artifacts\\spark_test\\spark-test.jar"});//项目构建生成的路径}SparkSession session = SparkSession.builder().appName(appName).config(sparkConf).config(sparkConf).getOrCreate();return session;}
}

childParent.txt文件内容

child        parent
Tom        Lucy
Tom        Jack
Jone        Lucy
Jone        Jack
Lucy        Mary
Lucy        Ben
Jack        Alice
Jack        Jesse
Terry        Alice
Terry        Jesse
Philip        Terry
Philip        Alma
Mark        Terry
Mark        Alma

输出

child        grand
Jone        Mary
Jone        Ben
Jone        Alice
Jone        Jesse
Mark        Alice
Mark        Jesse
Philip      Alice
Philip      Jesse
Tom     Mary
Tom     Ben
Tom     Alice
Tom     Jesse

四、join方法

<W> JavaPairRDD<K, Tuple2<V, W>> join(JavaPairRDD<K, W> other)

关联表返回相同可以的键值对

Spark入门(十七)之单表关联相关推荐

  1. Hadoop入门(十六)Mapreduce的单表关联程序

    "单表关联"要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘 1 实例描述 给出child-parent(孩子--父母)表,要求输出grandchild-gran ...

  2. MapReduce实例----单表关联

    1.源数据: Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy Ben Jack Alice Jack Jesse Terry Alice Te ...

  3. MapReduce编程(五) 单表关联

    一.问题描述 下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格. 输入文件内容如下: child parent Steven Lucy Steven Jack ...

  4. MapRedece(单表关联)

    源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...

  5. hibernate入门二之单表操作

    友情提醒:以下所有的操作都是在hibernate入门一的基础上操作的,参考链接:https://www.cnblogs.com/lindaiyu/p/10990525.html 单一主键 assign ...

  6. mysql 单表关联_MySQL 基础之 单表、多表联查

    使用和不使用not null 的区别: 不使用: 查询时用'name is null' 作为条件 mysql>create table t8( -> id int auto_increme ...

  7. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parent Tom    Lucy Tom    Jack Jone    Lucy Jone    Jack Lucy    Mary Lucy  ...

  8. Oracle入门--水表项目(单表查询,链接查询,左右外连接,子查询,分页查询)(3)

    水表项目建表 1.1.1 分析 1.1.2 SQL实现 表结构 -- <自来水公司收费系统> -- 1 业主类型表 create table t_ownertype(id number p ...

  9. Hadoop案例之单表关联输出祖孙关系

    1.案例描述 实例中给出child-parent(孩子--父母)表,要求输出grandchild-grandparent(孙子--爷奶)表. 样例输入如下所示. file: child       p ...

最新文章

  1. HDU4587(割点+最大连通分量)
  2. postfix 554-5.7.0 Reject
  3. 小看--发布-订阅(观察者)模式
  4. firefox浏览器 插件--【维基百科+谷歌翻译】高级应用之 带图翻译
  5. cpci检索为什么那么慢_索引原理与慢查询优化
  6. XML解组基准:JAXB,STAx,Woodstox
  7. JS - Object.create(prototype)方法
  8. 计算机基础函数运用,计算机应用基础第十二讲:EXCEL中函数的实际运用.doc
  9. 2017年苹果企业开发者账号申请完整指南
  10. Python fullstack系列【2】Python数据类型
  11. Mysql(二)Mysql SQL练习题
  12. javaweb框架学习开端
  13. 大数据应用场景有哪些?一篇文章告诉你
  14. XZ_iOS 之开发者账号续费 你的支付授权失败 请核对你的信息并重试 或尝试其他支付方式 请联系你的银行了解更多信息
  15. 测试底妆的软件,你最想看的粉底液测评,我一次性测了12款!!
  16. 线性回归和卡方分布与方差分析
  17. [有机化学] 简单有机物的英文命名法
  18. java 小球抛物线_小球抛物线运动
  19. 利用字典实现大侠个人信息查询程序
  20. 移动IM开源框架对比

热门文章

  1. 乔安监控云存储_智能运维丨全栈监控,护航云上业务
  2. 数据结构——从叶子结点到根节点的全部路径
  3. 循环遍历多层json_面试官:JSON.stringify() 实现深拷贝有什么问题
  4. linux 启动程序 绑定id,linux如何根据进程ID查找启动程序的路径
  5. 添加库路经 linux,linux下的静态库与动态库
  6. 数据结构与算法--数组中的逆序对
  7. 数据结构与算法-- 二叉树中和为某一值的路径
  8. paragon+ntfs+linux,NTFS For Mac 超强兼容性
  9. python如何加密字符串_Python实现对字符串的加密解密方法示例
  10. LeetCode动态规划 分割等和子集