pyspark 语法总结

这篇博客详细总结了PySpark的语法,包括导入、链接Hive、基础及高阶RDD操作,如action和pair RDD操作,以及如何计算平均值、优化join速度。此外,还介绍了读取文件(如text、json、csv)的方法,累加器和广播变量的使用,以及数值RDD操作和Spark SQL的应用,特别强调了schemaRDD在存储效率上的优势。
摘要由CSDN通过智能技术生成

导入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
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About This Book, Learn why and how you can efficiently use Python to process data and build machine learning models in Apache Spark 2.0Develop and deploy efficient, scalable real-time Spark solutionsTake your understanding of using Spark with Python to the next level with this jump start guide, Who This Book Is For, If you are a Python developer who wants to learn about the Apache Spark 2.0 ecosystem, this book is for you. A firm understanding of Python is expected to get the best out of the book. Familiarity with Spark would be useful, but is not mandatory., What You Will Learn, Learn about Apache Spark and the Spark 2.0 architectureBuild and interact with Spark DataFrames using Spark SQLLearn how to solve graph and deep learning problems using GraphFrames and TensorFrames respectivelyRead, transform, and understand data and use it to train machine learning modelsBuild machine learning models with MLlib and MLLearn how to submit your applications programmatically using spark-submitDeploy locally built applications to a cluster, In Detail, Apache Spark is an open source framework for efficient cluster computing with a strong interface for data parallelism and fault tolerance. This book will show you how to leverage the power of Python and put it to use in the Spark ecosystem. You will start by getting a firm understanding of the Spark 2.0 architecture and how to set up a Python environment for Spark., You will get familiar with the modules available in PySpark. You will learn how to abstract data with RDDs and DataFrames and understand the streaming capabilities of PySpark. Also, you will get a thorough overview of machine learning capabilities of PySpark using ML and MLlib, graph processing using GraphFrames, and polyglot persistence using Blaze. Finally, you will learn how to deploy your applications to the cloud using the spark-submit command., By the end of this book, you will have established a firm understanding of the Spark Python API and how it can be used t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值