zoukankan      html  css  js  c++  java
  • RDD的基础知识

    以下的这些分析都是基于spark2.1进行的

    (一)什么是RDD

    A Resilient Distributed Dataset (RDD), the basic abstraction in Spark. Represents an immutable, partitioned collection of elements that can be operated on in parallel.

    RDD是spark最基本的抽象概念,spark中的所有数据均通过RDD的形式进行组织。RDD是弹性的,自动容错的,分区的,只读的记录集合。

    (二)RDD的适用范围

    RDD尤其适用于迭代式的数据处理,如机器学习等。但它不适合那些异步更新共享状态的应用,例如web爬虫。

    (三)一些特性

    1、在部分分区数据丢失时,spark可以通过依赖关系重新计算丢失的分区数据,而不是对RDD的所有分区进行重算。 
    2、用户可以在创建RDD时指定RDD的分区数量,如果没有指定,那么就会采用默认值,即程序分区到的CPU core数目。对于HDFS,每个block会分配一个分区。对于由父RDD生成的子RDD,其分区数量与父RDD相同,或者在transformation中显式指定。

    (四)RDD的创建

    RDD有2种创建方式

    1、由一个已经存在的scala集合创建

    val rdd = sc.paralellize(List(1,2,3,4))

    一般只在试验性代码中使用,生产环境不大可能用到。

    2、由外部存储系统的数据创建

    比如本地文件,HDFS, Hbase等,常用textFile方法

    val rdd = sc.textFile("hdfs:///tmp/myfile.txt")

    (五)RDD的操作

    RDD有2种操作:transformation 与 action

    二、RDD的缓存

    对于一个经常被使用的RDD或者计算代价较大的RDD,将其缓存下来,会大大的提高处理速度。

    (一)缓存方式

    persist()是标准的缓存方法 
    cache()是其简化方法,当只使用内存作缓存时使用。

    (二)缓存级别

    MEMORY_ONLY Store RDD as deserialized Java objects in the JVM. If the RDD does not fit in memory, some partitions will not be cached and will be recomputed on the fly each time they’re needed. This is the default level. 
    MEMORY_AND_DISK Store RDD as deserialized Java objects in the JVM. If the RDD does not fit in memory, store the partitions that don’t fit on disk, and read them from there when they’re needed. 
    MEMORY_ONLY_SER Store RDD as serialized Java objects (one byte array per partition). This is generally more space-efficient than deserialized objects, especially when using a fast serializer, but more CPU-intensive to read. 
    MEMORY_AND_DISK_SER Similar to MEMORY_ONLY_SER, but spill partitions that don’t fit in memory to disk instead of recomputing them on the fly each time they’re needed. 
    DISK_ONLY Store the RDD partitions only on disk. 
    MEMORY_ONLY_2, MEMORY_AND_DISK_2, etc. Same as the levels above, but replicate each partition on two cluster nodes. 
    OFF_HEAP Store RDD in serialized format in Tachyon. Compared to MEMORY_ONLY_SER, OFF_HEAP reduces garbage collection overhead and allows executors to be smaller and to share a pool of memory, making it attractive in environments with large heaps or multiple concurrent applications. Furthermore, as the RDDs reside in Tachyon, the crash of an executor does not lead to losing the in-memory cache. In this mode, the memory in Tachyon is discardable. Thus, Tachyon does not attempt to reconstruct a block that it evicts from memory. If you plan to use Tachyon as the off heap store, Spark is compatible with Tachyon out-of-the-box. Please refer to this page for the suggested version pairings.

    (三)序列化

    缓存数据时可以选择是否同时进行序列化。序列化后占用的空间会减少,但有序列化/反序列化的成本。 
    如果确定需要使用序列化,则同时应该设置序列化的方式,默认是使用java自带的序列化机制,可以通过kyro等框架优化序列化效率。

    即使完全无其它属性,一个java对象都要占据8个字节的内存,包括:锁标志位、经历了几次gc、类的类信息等,因此序列化可节省此部分的空间。

  • 相关阅读:
    Java
    Spring
    Q&A
    Q&A
    Q&A
    Spring
    Elasticsearch部署及基本概念
    rust(二) 变量及类型
    rust(一) 一些命令
    vim笔记
  • 原文地址:https://www.cnblogs.com/itboys/p/6673290.html
Copyright © 2011-2022 走看看