Spark Python 快速体验

Spark是2015年最受热捧大数据开源平台,我们花一点时间来快速体验一下Spark。

Spark 技术栈

如上图所示,Spark的技术栈包括了这些模块:

  • 核心模块 :Spark Core

  • 集群管理 

  • Standalone Scheduler

  • YARN

  • Mesos

  • Spark SQL

  • Spark 流 Streaming

  • Spark 机器学习 MLLib

  • GraphX 图处理模块

安装和启动Spark

Spark Python Shell

> bin/pyspark

Spark Ipython Shell

1> IPYTHON=1 ./bin/pyspark 2> IPYTHON_OPTS="notebook" ./bin/pyspark

Spark 架构

初始化 Spark Context

在使用Spark的功能之前首先要初始化Spark的context,Context包含了Spark的连接和配置信息。

Spark Context,Driver和Worker节点之间的关系如下图:

1from pyspark import SparkConf, SparkContext 2conf = SparkConf().setMaster("local").setAppName("My App") 3sc = SparkContext(conf = conf)

创建 RDD

RDD是Spark的基本数据模型,所有的操作都是基于RDD。RDD是inmutable(不可改变)的。

1lines = sc.textFile("README.md") 2pythonLines = lines.filter(lambda line: "Python" in line)  3pythonLines.first() 4pythonLines.count()

RDD 操作:

下面是一些对RDD的变形操作

RDD Transformation on {1,2,3,4}

两个RDD之间的操作, Transformation on {1,2,3} and {3,4,5}

RDD actions on {1,2,3,3}

Transformation on pair RDD {(1,2),(3,4),(3,6)}

Transform on two pair RDDs {(1,2),(3,4),(3,6)}, {(3,9)}

Spark 流 stream

Spark流基于RDD,可以理解对小的时间片段上的RDD操作。

SparkSQL

Spark SQL可以用于操作和查询结构化和半结构化的数据。包括Hive,JSON, CSV等。

1# Import Spark SQL 2from pyspark.sql import HiveContext, Row 3# Or if you can't include the hive requirements 4from pyspark.sql import SQLContext, Row  5 6input = hiveCtx.jsonFile(inputFile) 7# Register the input schema RDDinput.registerTempTable("tweets") 8# Select tweets based on the retweetCount 9topTweets = hiveCtx.sql("SELECT text, retweetCount FROM tweets ORDER BY retweetCount LIMIT 10")

Spark SQL支持JDBC

SparkML

机器学习的基本流程如下:

  1. 获得数据

  2. 从数据中提取特征

  3. 对数据进行有监督的或者无监督的学习,训练机器学习的模型

  4. 对模型进行评估,找出最佳模型

由于Spark的架构特点,Spark支持的机器学习算法是哪些可以并行的算法。

1from pyspark.mllib.regression import LabeledPoint 2from pyspark.mllib.feature import HashingTF 3from pyspark.mllib.classification import LogisticRegressionWithSGD 4 5spam = sc.textFile("spam.txt")normal = sc.textFile("normal.txt") 6 7# Create a HashingTF instance to map email text to vectors of 10,000 features. 8 9tf = HashingTF(numFeatures = 10000) 10# Each email is split into words, and each word is mapped to one feature. 11spamFeatures = spam.map(lambda email: tf.transform(email.split(" "))) 12normalFeatures = normal.map(lambda email: tf.transform(email.split(" "))) 13 14# Create LabeledPoint datasets for positive (spam) and negative (normal) examples. 15 16positiveExamples = spamFeatures.map(lambda features: LabeledPoint(1, features)) 17negativeExamples = normalFeatures.map(lambda features: LabeledPoint(0, features)) 18trainingData = positiveExamples.union(negativeExamples) 19trainingData.cache() # Cache since Logistic Regression is an iterative algorithm. 20 21# Run Logistic Regression using the SGD algorithm. 22 23model = LogisticRegressionWithSGD.train(trainingData) 24 25# Test on a positive example (spam) and a negative one (normal). We first apply 26# the same HashingTF feature transformation to get vectors, then apply the model. 27posTest = tf.transform("O M G GET cheap stuff by sending money to ...".split(" ")) 28negTest = tf.transform("Hi Dad, I started studying Spark the other ...".split(" ")) 29print "Prediction for positive test example: %g" % model.predict(posTest) 30print "Prediction for negative test example: %g" % model.predict(negTest)
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Spark Python 快速体验 - HelloWorld