导入pyspark
# coding:utf-8
#使用pyspark
from pyspark import SparkConf, SparkContext
import pyspark
from pyspark import SparkContext as sc
from pyspark import SparkConf
conf=SparkConf().setAppName("miniProject").setMaster("local[*]")
sc=SparkContext.getOrCreate(conf)
lines = sc.textFile("data_sql.txt")
链接hive:
#链接hive
from pyspark.sql import HiveContext
hive_context = HiveContext(sc)
hive_context.sql('use default')
#读取josn
tweets = hive_context.jsonFile(json文件)
result = hive_context.sql('select * from table')
基础RDD操作:
line.map(func) #func可以是lambad,也可以是定义的函数
rdd.mapValues(func)
rdd.flatMapValues(func) #只针对值,不改变键
line.flatmap(func) #flatmap先对每个元素执行func,再重新变成一个数组,实际上就是执行完类似map的操作后打平,变成一个数组
line1.join(line2)# 保留line1中的key,把line1和line2中key相同的value放到一个元组里,[(key,(value1,value2),....]
leftOuterJoin(),rightOuterJoin() #左右链接
rdd.filter(lambda x:x != 1) #过滤出符合条件的值
rdd.keys(),rdd.values()#返回键、values的RDD
rdd1.union(rdd2) # 所有rdd1和rdd2中的item组合'
rdd1.intersection(rdd2)#'rdd1 和 rdd2的交集'
rdd1.substract(rdd2