示例
 
Spark SQL注册“临时表”执行“Join”(Inner Join、Left Outer Join、Right Outer Join、Full Outer Join)
 
代码
 
from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext, Row conf = SparkConf().setAppName("spark_sql_table_join") sc = SparkContext(conf=conf) sqlCtx = SQLContext(sc) line1 = sc.parallelize(["name1 a", "name3 c", "name4 d"]) line2 = sc.parallelize(["name1 1", "name2 2", "name3 3"]) word1 = line1.map(lambda line: line.split(" ")) word2 = line2.map(lambda line: line.split(" ")) table1 = word1.map(lambda words: Row(name=words[0], title=words[1])) table2 = word2.map(lambda words: Row(name=words[0], fraction=words[1])) tableSchema1 = sqlCtx.inferSchema(table1) tableSchema2 = sqlCtx.inferSchema(table2) tableSchema1.registerTempTable("table1") tableSchema2.registerTempTable("table2") def printRows(rows):
if rows:
for row in rows:
print row # inner join
rows = sqlCtx.sql(
"select table1.name, table1.title, table2.fraction from table1 join table2 on table1.name = table2.name").collect() printRows(rows) print "=============================================" # left outer join
rows = sqlCtx.sql(
"select table1.name, table1.title, table2.fraction from table1 left outer join table2 on table1.name = table2.name").collect() printRows(rows) # right outer join
rows = sqlCtx.sql(
"select table1.name, table1.title, table2.fraction from table1 right outer join table2 on table1.name = table2.name").collect() print "=============================================" printRows(rows) # full outer join
rows = sqlCtx.sql(
"select table1.name, table1.title, table2.fraction from table1 full outer join table2 on table1.name = table2.name").collect() print "=============================================" printRows(rows) """
Row(name=u'name1', title=u'a', fraction=u'1')
Row(name=u'name3', title=u'c', fraction=u'3')
=============================================
Row(name=u'name1', title=u'a', fraction=u'1')
Row(name=u'name3', title=u'c', fraction=u'3')
Row(name=u'name4', title=u'd', fraction=None)
=============================================
Row(name=u'name1', title=u'a', fraction=u'1')
Row(name=None, title=None, fraction=u'2')
Row(name=u'name3', title=u'c', fraction=u'3')
=============================================
Row(name=u'name1', title=u'a', fraction=u'1')
Row(name=None, title=None, fraction=u'2')
Row(name=u'name3', title=u'c', fraction=u'3')
Row(name=u'name4', title=u'd', fraction=None)
""" sc.stop()
 

Spark SQL Table Join(Python)的更多相关文章

  1. Spark SQL 之 Join 实现

    原文地址:Spark SQL 之 Join 实现 Spark SQL 之 Join 实现 涂小刚 2017-07-19 217标签: spark , 数据库 Join作为SQL中一个重要语法特性,几乎 ...

  2. 第九篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 cache table

    /** Spark SQL源码分析系列文章*/ Spark SQL 可以将数据缓存到内存中,我们可以见到的通过调用cache table tableName即可将一张表缓存到内存中,来极大的提高查询效 ...

  3. Spark SQL中Not in Subquery为何低效以及如何规避

    首先看个Not in Subquery的SQL: // test_partition1 和 test_partition2为Hive外部分区表 select * from test_partition ...

  4. Spark SQL概念学习系列之Spark SQL 优化策略(五)

    查询优化是传统数据库中最为重要的一环,这项技术在传统数据库中已经很成熟.除了查询优化, Spark SQL 在存储上也进行了优化,从以下几点查看 Spark SQL 的一些优化策略. (1)内存列式存 ...

  5. Adaptive Execution如何让Spark SQL更高效更好用

    1 背  景 Spark SQL / Catalyst 和 CBO 的优化,从查询本身与目标数据的特点的角度尽可能保证了最终生成的执行计划的高效性.但是 执行计划一旦生成,便不可更改,即使执行过程中发 ...

  6. spark sql/hive小文件问题

    针对hive on mapreduce 1:我们可以通过一些配置项来使Hive在执行结束后对结果文件进行合并: 参数详细内容可参考官网:https://cwiki.apache.org/conflue ...

  7. Spark SQL join的三种实现方式

    引言 join是SQL中的常用操作,良好的表结构能够将数据分散到不同的表中,使其符合某种规范(mysql三大范式),可以最大程度的减少数据冗余,更新容错等,而建立表和表之间关系的最佳方式就是join操 ...

  8. Spark SQL如何选择join策略

    前言 众所周知,Catalyst Optimizer是Spark SQL的核心,它主要负责将SQL语句转换成最终的物理执行计划,在一定程度上决定了SQL执行的性能. Catalyst在由Optimiz ...

  9. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

随机推荐

  1. IOS Dictionary和Model相互转换

    // // HYBJSONModel.h // Json2ModelDemo // // Created by huangyibiao on 14-9-15. // Copyright (c) 201 ...

  2. IOS中类的扩展(协议,分类)

    IOS中类的扩展(协议,分类) 扩展类,我们可以使用协议和分类这两种方法,下面我们来分别实现这两种方法: 参考网址:http://www.cnblogs.com/wendingding/p/37095 ...

  3. 中国剩余定理模板poj1006

    #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...

  4. 原生js方法document.getElementsByClassName在ie8及其以下的兼容性问题

    document.getElementsByClassName在ie8及其以下浏览器的兼容性问题,在ie8及其以下浏览器中不能使用,针对这个问题,下面给出详细的解决方法,感兴趣的朋友可以参考下     ...

  5. protocol buffer使用简介

    之前在工作中用到了protocol buffer(此处简称PB)(主要对数据进行序列化与反序列化,方便网络传输中的编解码),之后发现这是一个好东西,在此稍微记录下该工具如何使用,方便以后查阅 官网地址 ...

  6. eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

    在做arcgis android开发的时候,突然遇到这种错误,The method onClick(View) of type new View.OnClickListener(){} must ov ...

  7. cxf webservice异步调用

    http://blog.csdn.net/changpingchen/article/details/9048347 http://www.oschina.net/question/780719_12 ...

  8. WebSocket协议

    websocket 简介 (2013-04-09 15:39:28) 转载▼   分类: websocket 一 WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例 ...

  9. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  10. Swift进阶

    概述 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反射 扩展—KVO 内存管理 循 ...