博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spark sql中进行sechema合并
阅读量:6690 次
发布时间:2019-06-25

本文共 1552 字,大约阅读时间需要 5 分钟。

spark sql中支持sechema合并的操作。

直接上官方的代码吧。

val sqlContext = new org.apache.spark.sql.SQLContext(sc)// sqlContext from the previous example is used in this example.// This is used to implicitly convert an RDD to a DataFrame.import sqlContext.implicits._// Create a simple DataFrame, stored into a partition directoryval df1 = sparkContext.makeRDD(1 to 5).map(i => (i, i * 2)).toDF("single", "double")df1.saveAsParquetFile("data/test_table/key=1")// Create another DataFrame in a new partition directory,// adding a new column and dropping an existing columnval df2 = sparkContext.makeRDD(6 to 10).map(i => (i, i * 3)).toDF("single", "triple")df2.saveAsParquetFile("data/test_table/key=2")// Read the partitioned tableval df3 = sqlContext.parquetFile("data/test_table")df3.printSchema()// The final schema consists of all 3 columns in the Parquet files together// with the partiioning column appeared in the partition directory paths.// root// |-- single: int (nullable = true)// |-- double: int (nullable = true)// |-- triple: int (nullable = true)// |-- key : int (nullable = true)

也就是说df1和df2都保存在data/test_table目录下了。 

df1列名分别为single,double,key 

df2列名分别为single,triple,key。

然后df3直接读取test_table后,会将df1,df2的列都加在一起,那么dfs的列分别就是single,double,triple,key

然后将df3.show。结果就 是:

single double triple key3      6      null   1  4      8      null   1  5      10     null   1  1      2      null   1  2      4      null   1  8      null   24     2  9      null   27     2  10     null   30     2  6      null   18     2  7      null   21     2

 

大家看,是不是df1和df2合起来的集成呢(不需要做关联)

转载地址:http://myhao.baihongyu.com/

你可能感兴趣的文章
[JS] - level8 kata
查看>>
JS和css实现检测移动设备方向的变化并判断横竖屏幕
查看>>
jQuery的deferred对象实战应用(附:Exchar动态多条数据展示并在topic展示详细数据)...
查看>>
python中all函数得用法
查看>>
js数组操作大全
查看>>
JAVAWEB 一一 Spirng(AOP面向切面)
查看>>
CentOS下yum安装VNCserver
查看>>
HttpServletResponse函數
查看>>
Linux基础学习(3)--初学注意
查看>>
php总结笔记[转]
查看>>
jni开发中的常见错误
查看>>
【ZeroClipboard is not defined】的解决方法
查看>>
【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法
查看>>
iOS 一个ViewController上显示2个tableView的方法
查看>>
VSCode插件整理
查看>>
【六】tf和cgi进行联合试验,完成日志服务器
查看>>
面试题解(2):loop相关
查看>>
《当当网系统分级与海量信息动态发布实践》读后感
查看>>
完整的学习C++的读书路线图
查看>>
Android获取屏幕宽度、高度的4种方法
查看>>