Spark SQL Table Join(Python)
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)的更多相关文章
- Spark SQL 之 Join 实现
原文地址:Spark SQL 之 Join 实现 Spark SQL 之 Join 实现 涂小刚 2017-07-19 217标签: spark , 数据库 Join作为SQL中一个重要语法特性,几乎 ...
- 第九篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 cache table
/** Spark SQL源码分析系列文章*/ Spark SQL 可以将数据缓存到内存中,我们可以见到的通过调用cache table tableName即可将一张表缓存到内存中,来极大的提高查询效 ...
- Spark SQL中Not in Subquery为何低效以及如何规避
首先看个Not in Subquery的SQL: // test_partition1 和 test_partition2为Hive外部分区表 select * from test_partition ...
- Spark SQL概念学习系列之Spark SQL 优化策略(五)
查询优化是传统数据库中最为重要的一环,这项技术在传统数据库中已经很成熟.除了查询优化, Spark SQL 在存储上也进行了优化,从以下几点查看 Spark SQL 的一些优化策略. (1)内存列式存 ...
- Adaptive Execution如何让Spark SQL更高效更好用
1 背 景 Spark SQL / Catalyst 和 CBO 的优化,从查询本身与目标数据的特点的角度尽可能保证了最终生成的执行计划的高效性.但是 执行计划一旦生成,便不可更改,即使执行过程中发 ...
- spark sql/hive小文件问题
针对hive on mapreduce 1:我们可以通过一些配置项来使Hive在执行结束后对结果文件进行合并: 参数详细内容可参考官网:https://cwiki.apache.org/conflue ...
- Spark SQL join的三种实现方式
引言 join是SQL中的常用操作,良好的表结构能够将数据分散到不同的表中,使其符合某种规范(mysql三大范式),可以最大程度的减少数据冗余,更新容错等,而建立表和表之间关系的最佳方式就是join操 ...
- Spark SQL如何选择join策略
前言 众所周知,Catalyst Optimizer是Spark SQL的核心,它主要负责将SQL语句转换成最终的物理执行计划,在一定程度上决定了SQL执行的性能. Catalyst在由Optimiz ...
- 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
随机推荐
- 将JDBC ResultSet结果集变成List
private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>() ...
- 循序渐近学docker---笔记
1.安装docker 环境:ubuntu 16.04 sudo apt-get install docker.io root@ld-Lenovo-G470:~# docker -vDocker ver ...
- composer之安装
最近想要学习下yii框架,所以,就看了下官网,看到了貌似比较依赖composer这个东西,然后我就安装了,但是会有问题,安装不上等等问题,不论是windows还是linux命令行安装,都是因为一个问题 ...
- 后台地址报错:Service Unavailable
首先考虑数据库是否打开? 第二重启IIS试试: 重启下iis试试(cmd接着iisreset) 再次访问就正常了,可以借鉴,但不一定就只是这一种原因.
- myeclipse跟eclipse中使用github做版本控制工具
今天早上花了一上午的时间,了解了在myeclipse跟eclipse中使用github. 好吧 说说怎么做的,让大伙少走一点路,我就简单描述下,需要软件的私信我 第一:下载git 第二:靠谱.但是pu ...
- oracle中创建一个用户,只能查看指定的视图,如何授权,创建别名
1.create user A identified by Apassword,创建用户,用户名是A,密码是Apassword2.grant connect to A --授予connect权限3.g ...
- Swift - 22 - 循环结构
//: Playground - noun: a place where people can play import UIKit // for-in for i in -99...99 { i * ...
- JetBrains公司的IDE使用技巧
1.自定义Live Templates: 点击+添加自己的.最后记住要点击,change或default来设置在哪些文件上使用代码片段.
- 解决Eclipse中编辑xml文件的智能提示问题,最简单的是第二种方法。
Eclipse for Android xml 文件代码自动提示功能,介绍Eclipse 编辑器中实现xml 文件代码自动智能提示功能,解决eclipse 代码提示失效.eclipse 不能自动提示. ...
- Java学习----变量是什么
1.变量必须拥有的类型 2.变量必须拥有的名字 变量:具备名字和类型的可以存放类型匹配的数据的量 public class Student { public static void main(Stri ...