1、Spark Streaming是什么?

a、Spark Streaming是什么?
  Spark Streaming类似于Apache Storm,用于流式数据的处理。根据其官方文档介绍,Spark Streaming有高吞吐量和容错能力强等特点。Spark Streaming支持的数据输入源很多,例如:Kafka、Flume、Twitter、ZeroMQ和简单的TCP套接字等等。数据输入后可以用Spark的高度抽象原语如:map、reduce、join、window等进行运算。而结果也能保存在很多地方,如HDFS,数据库等。另外Spark Streaming也能和MLlib(机器学习)以及Graphx完美融合。
b、Spark Streaming的特点?
  易用、容错、易整合到Spark体系、

2、Spark与Storm的对比

  a、Spark开发语言:Scala、Storm的开发语言:Clojure。

  b、Spark编程模型:DStream、Storm编程模型:Spout/Bolt。

  c、Spark和Storm的对比介绍:

Spark:

Storm:

3、什么是DStream?

3.1、Discretized Stream是Spark Streaming的基础抽象,代表持续性的数据流和经过各种Spark原语操作后的结果数据流。在内部实现上,DStream是一系列连续的RDD来表示。每个RDD含有一段时间间隔内的数据,如下图:

对数据的操作也是按照RDD为单位来进行的:

计算过程由Spark engine来完成

3.2、DStream相关操作:
  DStream上的原语与RDD的类似,分为Transformations(转换)和Output Operations(输出)两种,此外转换操作中还有一些比较特殊的原语,如:updateStateByKey()、transform()以及各种Window相关的原语。

  a、Transformations on DStream:

Transformation

Meaning

map(func)

Return a new DStream by passing each element of the source DStream through a function func.

flatMap(func)

Similar to map, but each input item can be mapped to 0 or more output items.

filter(func)

Return a new DStream by selecting only the records of the source DStream on which func returns true.

repartition(numPartitions)

Changes the level of parallelism in this DStream by creating more or fewer partitions.

union(otherStream)

Return a new DStream that contains the union of the elements in the source DStream and otherDStream.

count()

Return a new DStream of single-element RDDs by counting the number of elements in each RDD of the source DStream.

reduce(func)

Return a new DStream of single-element RDDs by aggregating the elements in each RDD of the source DStream using a function func (which takes two arguments and returns one). The function should be associative so that it can be computed in parallel.

countByValue()

When called on a DStream of elements of type K, return a new DStream of (K, Long) pairs where the value of each key is its frequency in each RDD of the source DStream.

reduceByKey(func, [numTasks])

When called on a DStream of (K, V) pairs, return a new DStream of (K, V) pairs where the values for each key are aggregated using the given reduce function. Note: By default, this uses Spark's default number of parallel tasks (2 for local mode, and in cluster mode the number is determined by the config property spark.default.parallelism) to do the grouping. You can pass an optional numTasks argument to set a different number of tasks.

join(otherStream, [numTasks])

When called on two DStreams of (K, V) and (K, W) pairs, return a new DStream of (K, (V, W)) pairs with all pairs of elements for each key.

cogroup(otherStream, [numTasks])

When called on a DStream of (K, V) and (K, W) pairs, return a new DStream of (K, Seq[V], Seq[W]) tuples.

transform(func)

Return a new DStream by applying a RDD-to-RDD function to every RDD of the source DStream. This can be used to do arbitrary RDD operations on the DStream.

updateStateByKey(func)

Return a new "state" DStream where the state for each key is updated by applying the given function on the previous state of the key and the new values for the key. This can be used to maintain arbitrary state data for each key.

特殊的Transformations

.UpdateStateByKey Operation
UpdateStateByKey原语用于记录历史记录,上文中Word Count示例中就用到了该特性。若不用UpdateStateByKey来更新状态,那么每次数据进来后分析完成后,结果输出后将不在保存 .Transform Operation
Transform原语允许DStream上执行任意的RDD-to-RDD函数。通过该函数可以方便的扩展Spark API。此外,MLlib(机器学习)以及Graphx也是通过本函数来进行结合的。 .Window Operations
Window Operations有点类似于Storm中的State,可以设置窗口的大小和滑动窗口的间隔来动态的获取当前Steaming的允许状态

  b、Output Operations on DStreams:

    Output Operations可以将DStream的数据输出到外部的数据库或文件系统,当某个Output Operations原语被调用时(与RDD的Action相同),streaming程序才会开始真正的计算过程。

Output Operation

Meaning

print()

Prints the first ten elements of every batch of data in a DStream on the driver node running the streaming application. This is useful for development and debugging.

saveAsTextFiles(prefix, [suffix])

Save this DStream's contents as text files. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]".

saveAsObjectFiles(prefix, [suffix])

Save this DStream's contents as SequenceFiles of serialized Java objects. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]".

saveAsHadoopFiles(prefix, [suffix])

Save this DStream's contents as Hadoop files. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]".

foreachRDD(func)

The most generic output operator that applies a function, func, to each RDD generated from the stream. This function should push the data in each RDD to an external system, such as saving the RDD to files, or writing it over the network to a database. Note that the function func is executed in the driver process running the streaming application, and will usually have RDD actions in it that will force the computation of the streaming RDDs.

4、 Spark Streaming的练习使用:

从Socket实时读取数据,进行实时处理,首先测试是否安装nc:

然后检查是否安装:[root@slaver1 hadoop]# which nc

  然后安装nc:[root@slaver1 hadoop]# yum install -y nc(此种方法安装出现错误,不建议使用)

  [root@slaver1 hadoop]# wget   http://vault.centos.org/6.6/os/x86_64/Packages/nc-1.84-22.el6.x86_64.rpm
  [root@slaver1 hadoop]# rpm   -iUv    nc-1.84-22.el6.x86_64.rpm

  然后在窗口执行如下命令:[root@slaver1 hadoop]#  nc -lk 9999(输入消息)。

  然后复制这个窗口,执行如下命令:[root@slaver1 hadoop]# nc slaver1 9999(可以接受输入的消息)。

5、开始测试:

[hadoop@slaver1 ~]$ nc -lk 9999

[hadoop@slaver1 spark-1.5.1-bin-hadoop2.4]$ ./bin/run-example streaming.NetworkWordCount 192.168.19.131 9999

然后在第一行的窗口输入例如:hello world hello world hadoop world spark world flume world hello world

看第二行的窗口是否进行计数计算;


1、Spark SQL and DataFrame

a、什么是Spark SQL?
  Spark SQL是Spark用来处理结构化数据的一个模块,它提供了一个编程抽象叫做DataFrame并且作为分布式SQL查询引擎的作用。
b、为什么要学习Spark SQL?
  我们已经学习了Hive,它是将Hive SQL转换成MapReduce然后提交到集群上执行,大大简化了编写MapReduce的程序的复杂性,由于MapReduce这种计算模型执行效率比较慢。所有Spark SQL的应运而生,它是将Spark SQL转换成RDD,然后提交到集群执行,执行效率非常快!
c、Spark的特点:
  易整合、统一的数据访问方式、兼容Hive、标准的数据连接。
d、什么是DataFrames?
  与RDD类似,DataFrame也是一个分布式数据容器。然而DataFrame更像传统数据库的二维表格,除了数据以外,还记录数据的结构信息,即schema。同时,与Hive类似,DataFrame也支持嵌套数据类型(struct、array和map)。从API易用性的角度上 看,DataFrame API提供的是一套高层的关系操作,比函数式的RDD API要更加友好,门槛更低。由于与R和Pandas的DataFrame类似,Spark DataFrame很好地继承了传统单机数据分析的开发体验。

2、创建DataFrames?

在Spark SQL中SQLContext是创建DataFrames和执行SQL的入口,在spark-1.5.2中已经内置了一个sqlContext:

.在本地创建一个文件,有三列,分别是id、name、age,用空格分隔,然后上传到hdfs上
hdfs dfs -put person.txt / .在spark shell执行下面命令,读取数据,将每一行的数据使用列分隔符分割
val lineRDD = sc.textFile("hdfs://node1.itcast.cn:9000/person.txt").map(_.split(" ")) .定义case class(相当于表的schema)
case class Person(id:Int, name:String, age:Int) .将RDD和case class关联
val personRDD = lineRDD.map(x => Person(x().toInt, x(), x().toInt)) .将RDD转换成DataFrame
val personDF = personRDD.toDF .对DataFrame进行处理
personDF.show

3、DataFrame常用操作:

DSL风格语法
//查看DataFrame中的内容
personDF.show //查看DataFrame部分列中的内容
personDF.select(personDF.col("name")).show
personDF.select(col("name"), col("age")).show
personDF.select("name").show //打印DataFrame的Schema信息
personDF.printSchema //查询所有的name和age,并将age+1
personDF.select(col("id"), col("name"), col("age") + ).show
personDF.select(personDF("id"), personDF("name"), personDF("age") + ).show //过滤age大于等于18的
personDF.filter(col("age") >= 18).show //按年龄进行分组并统计相同年龄的人数
personDF.groupBy("age").count().show()

4、SQL风格语法:

如果想使用SQL风格的语法,需要将DataFrame注册成表
personDF.registerTempTable("t_person") //查询年龄最大的前两名
sqlContext.sql("select * from t_person order by age desc limit 2").show //显示表的Schema信息
sqlContext.sql("desc t_person").show

待续......

Spark的Streaming和Spark的SQL简单入门学习的更多相关文章

  1. Spring.Net 简单入门学习

    Spring.NET IoC容器的用法. 通过简单的例子学习Spring.Net 1.先创建一个控制台程序项目. 2.添加IUserInfoDal 接口. namespace Spring.Net { ...

  2. android的简单入门学习

    话说光配环境就整死我了, 不是说多么难, 是最近google被屏了, 很多sdk里面需要下载的东西都下不下来, 坑爹啊.  最后跟扫拉稀要了一个他配置好的,才运行了. android目录分析: ass ...

  3. SDL 简单入门学习

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 概要 实际学习使用SDL创建窗体,并绘制图形. 前言 今天想要做一个简单的demo ...

  4. MyBatis的简单入门学习

    一个新知识开始是最难学的.(万事开头难) MyBatis大体分三层,接口层,数据处理层,基础支撑层. 其中接口层不是java中的那个interface,而是框架和程序员之间的对接.一个API,程序员可 ...

  5. JSTL简单入门学习实例

    Maven依赖: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</ ...

  6. Python 3 + Selenium 3 简单入门学习示例 126邮箱登录

    这是一个很多基础演示的书上的例子,但是一般按照这些书上的代码可能都不能成功登录.也许是网易修改了126的页面导致的吧,下面给出最新的能够work的版本 from selenium import web ...

  7. [Spark] 06 - What is Spark Streaming

    前言 Ref: 一文读懂 Spark 和 Spark Streaming[简明扼要的概览] 在讲解 "流计算" 之前,先做一个简单的回顾,亲! 一.MapReduce 的问题所在 ...

  8. Spark Structured streaming框架(1)之基本使用

     Spark Struntured Streaming是Spark 2.1.0版本后新增加的流计算引擎,本博将通过几篇博文详细介绍这个框架.这篇是介绍Spark Structured Streamin ...

  9. Spark Structured Streaming框架(1)之基本用法

     Spark Struntured Streaming是Spark 2.1.0版本后新增加的流计算引擎,本博将通过几篇博文详细介绍这个框架.这篇是介绍Spark Structured Streamin ...

随机推荐

  1. python print输出带颜色 总结

    书写格式:      开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由 ...

  2. libevent学习笔记 一、基础知识【转】

    转自:https://blog.csdn.net/majianfei1023/article/details/46485705 欢迎转载,转载请注明原文地址:http://blog.csdn.net/ ...

  3. telnetlib 中各种 read 函数的意义

    基本原理 要明白 telnetlib 中各个 read 函数的意义,首先要了解 telnetlib 的工作原理. telnetlib 首先通过 socket 连接从网络接收数据,把数据存储到自己的 r ...

  4. 题解-CTSC2012 熟悉的文章

    Problem bzoj 题目大意:给定多个标准串和一个文本串,全部为01串,如果一个串长度不少于\(L\)且是任意一个标准串的子串,那么它是"熟悉"的.对于文本串\(A\),把\ ...

  5. localstorage实现网页状态记录比如放音乐功能的实例

    <div class="music"> <a href="javascript:;" onclick="playPause();&q ...

  6. git与eclipse集成之更新特性分支代码到个人特性分支

    1.1. 更新特性分支代码到个人特性分支 在基于特性分支开发的过程中,存在多人向特性分支提交代码的情况,开发者需要关注特性分支代码与个人分支代码保持同步,否则可能导致提交代码冲突. 具体代码同步步骤: ...

  7. codeforces 955F Cowmpany Cowmpensation 树上DP+多项式插值

    给一个树,每个点的权值为正整数,且不能超过自己的父节点,根节点的最高权值不超过D 问一共有多少种分配工资的方式? 题解: A immediate simple observation is that ...

  8. Control算法相关

    Control算法相关 添加新的control算法官方指导教程. 创建一个控制器: 在文件control_config中添加新控制器的配置信息: 注册新控制器. 如何添加新的CAN卡. Apollo中 ...

  9. #ifndef#define#endif的用法(整理)

    [转] #ifndef#define#endif的用法(整理)    原作者:icwk  文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都in ...

  10. ubuntu安装 opencv-3.4.3

    1 .官网(https://opencv.org/releases.html)下载下源码 opencv-3.4.3.zip 2.解压 unzip  opencv-3.4.3.zip 3.cmake c ...