rdd

Spark计算框架

家住魔仙堡 提交于 2019-12-02 21:23:18
Spark是以MapReduce为基础在其上进行功能扩展的集群计算框架。spark计算面向是RDD(resilient distributed dataset)分区内集合元素可并行操作 RDD是种编程抽象,代表可以跨机器进行分割的只读对象集合,所有对数据操作都需通过RDD处理。   RDD操作:    create :通过hfile 或 scala collection作为数据源    transformation :处理计算转换,map,flatmap,filter    controler :对中间结果可存储在memory 或file供其它RDD数据复用    actions :驱动RDD执行计算 Spark程序是一个惰性计算,通过action调用来驱动代码被分发到集群上,由各个RDD分区上的worker来执行,然后结果会被发送回驱动程序进行聚合处理。 即,驱动程序创建一个或多个RDD,调用transform来转换RDD,然后调用reduce处理被转换后的RDD。在程序处理数据过程中使用的是pipleLine方式。 程序执行流程: Spark Components: 角色组成:   Driver program: 由SparkContext创建,运行在main方法   Cluster manager: 获取集群内资源(模式standalone ,Mesos, YARN

How to create a DataFrame from a text file in Spark

北城以北 提交于 2019-12-02 18:25:13
I have a text file on HDFS and I want to convert it to a Data Frame in Spark. I am using the Spark Context to load the file and then try to generate individual columns from that file. val myFile = sc.textFile("file.txt") val myFile1 = myFile.map(x=>x.split(";")) After doing this, I am trying the following operation. myFile1.toDF() I am getting an issues since the elements in myFile1 RDD are now array type. How can I solve this issue? Update - as of Spark 1.6 , you can simply use the built-in csv data source: spark: SparkSession = // create the Spark Session val df = spark.read.csv("file.txt")

SPARK : groupByKey vs reduceByKey which is better and efficient to combine the Maps?

你离开我真会死。 提交于 2019-12-02 17:43:38
问题 I have a data frame [df] : +------------+-----------+------+ | id | itemName |Value | --------------------------------+ | 1 | TV | 4 | | 1 | Movie | 5 | | 2 | TV | 6 | I am trying to transform it to : {id : 1, itemMap : { "TV" : 4, "Movie" : 5}} {id : 2, itemMap : {"TV" : 6}} I want the final result to be in RDD[(String, String)] with itemMap as the value's name So I am doing : case class Data (itemMap : Map[String, Int]) extends Serializable df.map{ case r => val id = r.getAs[String]("id")

Spark SQL 笔记

老子叫甜甜 提交于 2019-12-02 16:46:39
Spark SQL 简介 SparkSQL 的前身是 Shark, SparkSQL 产生的根本原因是其完全脱离了 Hive 的限制。(Shark 底层依赖于 Hive 的解析器, 查询优化器) SparkSQL 支持查询原生的 RDD。 能够在 scala/java 中写 SQL 语句。 支持简单的 SQL 语法检查, 能够在 Scala 中 写Hive 语句访问 Hive 数据, 并将结果取回作为RDD使用 Spark on Hive 和 Hive on Spark Spark on Hive: Hive 只作为储存角色, Spark负责 sql 解析优化, 执行。 Hive on Spark: Hive 即作为存储又负责 sql 的解析优化, Spark 负责执行。 Dataset 与 DataFrame Dataset 是一个分布式数据容器,与 RDD 类似, 然而 DataSet 更像 传统数据库的二维表格, 除了数据以外, 还掌握的结构信息, 即schema。 同时, 与 Hive 类似, Dataset 也支持嵌套数据类型 (struct、array 和 map)。 从 API 易用性角度上看, DataSet API 提供的是一套高层的关系操作, 比函数式的 RDD API 更加友好, 门槛更低。 Dataset 的底层封装的是RDD, 当 RDD 的泛型是 Row

Spark(二)算子详解

微笑、不失礼 提交于 2019-12-02 15:35:30
目录 Spark(二)算子讲解 一、wordcountcount 二、编程模型 三、RDD数据集和算子的使用 Spark(二)算子讲解 @ 一、wordcountcount 基于上次的wordcount,我们来写一个wordcountcount,来对wc程序进行第二次计数,我们来分析一下性能。 package com.littlepage.wc import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: Array[String]): Unit = { val conf=new SparkConf().setAppName("wc").setMaster("local") val sparkContext=new SparkContext(conf) sparkContext.setLogLevel("error") val fileRDD:RDD[String] = sparkContext.textFile("data/data") val words:RDD[String] = fileRDD.flatMap(_.split(" ")) val pairWord:RDD[(String,Int)] =

大数据之Spark 知识体系完整解读

混江龙づ霸主 提交于 2019-12-02 15:28:56
Spark 简介 Spark 是整个 BDAS 的核心组件,是一个大数据分布式编程框架,不仅实现了MapReduce的算子map 函数和reduce函数及计算模型,还提供更为丰富的算子,如filter、join、groupByKey等。是一个用来实现快速而同用的集群计算的平台。 Spark将分布式数据抽象为弹性分布式数据集(RDD),实现了应用任务调度、RPC、序列化和压缩,并为运行在其上的上层组件提供API。其底层采用Scala这种函数式语言书写而成,并且所提供的API深度借鉴Scala函数式的编程思想,提供与Scala类似的编程接口。 Sparkon Yarn 从用户提交作业到作业运行结束整个运行期间的过程分析。 在这里还是要推荐下我自己建的大数据学习交流群:943791324,群里都是学大数据开发的,如果你正在学习大数据 ,小编欢迎你加入,大家都是软件开发党,不定期分享干货(只有大数据软件开发相关的),包括我自己整理的一份最新的大数据进阶资料和高级开发教程,欢迎进阶中和进想深入大数据的小伙伴加入。 一、客户端进行操作 1.根据yarnConf来初始化yarnClient,并启动yarnClient 2.创建客户端Application,并获取Application的ID,进一步判断集群中的资源是否满足executor和ApplicationMaster申请的资源

Spark中的Transformations和Actions介绍

会有一股神秘感。 提交于 2019-12-02 15:23:26
RDD提供了两种类型的操作: transformation 和 action 所有的 transformation 都是采用的懒策略,如果只是将 transformation 提交是不会执行计算的,计算只有在 action 被提交的时候才被触发。 action操作:action是得到一个值,或者一个结果(直接将RDD cache到内存中) 常见的转换操作有 Transformation 算子 含义 map(func) 对原 RDD 中每个元素运用 func 函数,并生成新的 RDD filter(func) 对原 RDD 中每个元素使用 func 函数进行过滤,并生成新的 RDD flatMap(func) 与 map 类似,但是每一个输入的 item 被映射成 0 个或多个输出的 items( func 返回类型需要为 Seq ) mapPartitions(func) 与 map 类似,但函数单独在 RDD 的每个分区上运行, func 函数的类型为 Iterator<T> => Iterator<U> ,其中 T 是 RDD 的类型,即 RDD[T] mapPartitionsWithIndex(func) 与 mapPartitions 类似,但 func 类型为 (Int, Iterator<T>) => Iterator<U> ,其中第一个参数为分区索引 sample

5. Spark Streaming高级解析

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:43:21
5.1 DStreamGraph对象分析       在Spark Streaming中,DStreamGraph是一个非常重要的组件,主要用来:         1. 通过成员inputStreams持有Spark Streaming输入源及接收数据的方式         2. 通过成员outputStreams持有Streaming app的output操作,并记录DStream依赖关系         3. 生成每个batch对应的jobs       下面,通过分析一个简单的例子,结合源码分析来说明DStreamGraph是如何发挥作用的。案例如下: val sparkConf = new SparkConf().setAppName("HdfsWordCount") val ssc = new StreamingContext(sparkConf, Seconds(2)) val lines = ssc.textFileStream(args(0)) val words = lines.flatMap(_.split(" ")) val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _) wordCounts.print() ssc.start() ssc.awaitTermination()      

Is DAG created when we perform operations over dataframes?

血红的双手。 提交于 2019-12-02 13:33:19
I have seen DAG getting generated whenever we perform any operation on RDD but what happens when we perform operations on our dataframe? When executing multiple operations on dataframe, Are those lazily evaluated just like RDD? When the catalyst optimizer comes into the picture? I am sort of confused between these. If anyone can throw some light on these topics, it would be really of great help. Thanks Every operation on a Dataset , continuous processing mode notwithstanding, is translated into a sequence of operations on internal RDDs . Therefore concept of DAG is by all means applicable. By

Spark rdd write in global list

百般思念 提交于 2019-12-02 13:03:27
How to write in global list with rdd? Li = [] Fn(list): If list.value == 4: Li.append(1) rdd.mapValues(lambda x:fn(x)) When I try to print Li the result is: [] What I'm trying to do is to transform another global liste Li1 while transforming the rdd object. However, when I do this I have always an empty list in the end. Li1 is never transformed. vvladymyrov The reason why you get Li value set to [] after executing mapValue s - is because Spark serializes Fn function (and all global variables that it references - it is called closure) and sends to an another machine - worker. But there is no