Cloudera VM环境下使用Spark合并数据
来源:互联网 发布:qq群发推广软件 编辑:程序博客网 时间:2024/05/16 01:39
在前一篇文章中我们使用了hadoop-MapReduce-streaming合并数据。这里我们使用相同的数据文件,但改用在PySparkshell下使用Spark完成这个任务。PySpark的交互性显然比使用MapReduce更好。
首先保证已经安装PySpark。
1. 启动PySpark
PYSPARK_DRIVER_PYTHON=ipythonpyspark
2. 载入数据
a. 载入存储在HDFS上的数据
fileA =sc.textFile("3-spark_join/input/join1_FileA.txt")fileB =sc.textFile("3-spark_join/input/join1_FileB.txt")
b. 检查数据已经载入
<span style="white-space:pre"></span>fileA.collect()fileB.collect()
3. Mapper函数
a. 分别为FileA和FileB编写mapper函数,把读入的数据分割为<key, value>
def split_fileA(line): # split the input line in word and count on the comma key_value = line.split(",") # turn the count to an integer word = key_value[0] count = int(key_value[1]) return (word,count)
def split_fileB(line): # split the input line into word, date and count_string key_value = line.split(",") key_in = key_value[0].split(" ") word = key_in[1] date = key_in[0] count_string = key_value[1] return (word, date + " " +count_string)
b. 可以用一个简单的数据检查函数
<span style="white-space:pre"></span>test_line = "able,991"<span style="white-space:pre"></span>split_fileA(test_line)
c. 现在可以用在FileA和FileB上了
<span style="white-space:pre"></span>fileA_data =fileA.map(split_fileA) fileB_data= fileB.map(split_fileB)d. 检查输出
<span style="white-space:pre"></span>fileA_data.collect()<span style="white-space:pre"></span>fileB_data.collect()
e. 应该得到
<span style="white-space:pre"></span>Out[]: [(u'able', 991), (u'about',11), (u'burger', 15), (u'actor', 22)]
<span style="white-space:pre"></span>Out[]:<span style="white-space:pre"></span>[(u'able', u'Jan-01 5'), <span style="white-space:pre"></span>(u'about', u'Feb-02 3'),<span style="white-space:pre"></span>(u'about', u'Mar-03 8 '), <span style="white-space:pre"></span>(u'able', u'Apr-04 13'),<span style="white-space:pre"></span>(u'actor', u'Feb-22 3'),<span style="white-space:pre"></span>(u'burger', u'Feb-23 5'), <span style="white-space:pre"></span>(u'burger', u'Mar-08 2'), <span style="white-space:pre"></span>(u'able', u'Dec-15 100')]
4. Spark本身有join函数,可以把有相同key的两个数据集合并,如:(K, V)和(K, W)合并为(K,(V, W))
fileB_joined_fileA =fileB_data.join(fileA_data)
5. 检查结果
fileB_joined_fileA.collect()
应该得到:
Out[20]:[(u'about', (u'Feb-02 3', 11)), (u'about', (u'Mar-03 8', 11)), (u'able', (u'Jan-01 5', 991)), (u'able', (u'Apr-04 13', 991)), (u'able', (u'Dec-15 100', 991)), (u'actor', (u'Feb-22 3', 22)), (u'burger', (u'Feb-23 5', 15)), (u'burger', (u'Mar-08 2', 15))]
0 0
- Cloudera VM环境下使用Spark合并数据
- Cloudera VM环境下使用Spark合并数据(2)
- Cloudera Hadoop mapreduce--合并数据
- 使用cloudera-quickstart-vm无配置快速部署Hadoop应用
- 大数据技术大合集:Hadoop家族、Cloudera系列、spark、storm
- 企业大数据cloudera manager安装使用
- cloudera VM环境,jps命令解决Hue安装Hbase报错
- (大数据)转载:windows下spark开发环境配置
- spark云计算环境下的大数据
- spark大数据入门(一)如何在windows下部署spark开发环境
- 如何使用intellij搭建spark开发环境(下)
- Window环境下使用IntelliJ IDEA调试Spark Application
- maven环境下使用java、scala混合开发spark应用
- Spark及HDFS环境下使用python的wordcount实例
- Windows下使用IntelliJ IDEA搭建Spark环境
- VM下搭建UBuntu开发环境笔记
- Vm-Ubuntu下配置Qt开发环境
- 校园网环境下,VM虚拟机上网
- 如何判断链表中是否有环
- 2.请把学生名与考试分数录入到Map中,并按分数显示前三名成绩学员的名字。
- C++ Qt5 TcpSocket网络通讯(传输数据块大小值,防止没有传送完整)
- Android 学习笔记(3)—— ImageView/RadioButton/CheckBox
- TimesTen强大且简单的开发接口支持
- Cloudera VM环境下使用Spark合并数据
- 【杭电】[2076]夹角有多大
- Java总结篇系列:Java泛型
- 很有帮助学习递归:从简单到复杂。
- 第一次写博客
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- ViewRoot和DecorView
- Java基于IBMMQ消息的发布与订阅
- winform自定义控件