Giter Site home page Giter Site logo

hadoop-spark-rdd's Introduction

hadoop-Spark RDD

This post will cover the following points:

  • what is RDD
  • how to manipulate RDD using pyspark API (mapreduce example)
  • Some RDD actions
  • Caching data
  • Shared Variables
  • Accumulator

What is RDD (Resilient Distributed Dataset)
RDD is the data container, the way in Spark to store data like variable or object in programming. Data can be read from local text file, HDFS or other source.

Caracteristiques:

  • Immutable: you cannot change just a section of RDD. But every time you transform one RDD, you create a new one.
  • Distributed: partition of data are divided accross machine
  • Resilient: for every point in your calculations, Spark knows which are the partitions needed to recreate the partition in case it gets lost. And if that happens, then spark automatically figures out where it can start from to recompute what's the minimum amount of processing needed to recover the lost partition

RDD manipulation
Go to your shell console and tape this command

pyspark

Create RDD

# Create RDD in 3 partitions
integer_RDD = sc.parallelize(range(10), 3)

# Check
integer_RDD.collect()

# Check partitions
integer_RDD.glom().collect()

# Read data form txt file
text_RDD = sc.textFile("file:///home/cloudera/testfile1")

# Read data from HDFS
text_RDD = sc.textFile("/user/cloudera/input/testfile1")

# Outputs the first line
text_RDD.take(1)

Word count in spark

# map functions
def split_words(line):
    return line.split()
    
def create_pair(word):
    return (word, 1)

pairs_RDD=text_RDD.flatMap(split_words).map(create_pair)


# reduce functions
def sum_counts(a, b):
    return a + b

wordcounts_RDD = pairs_RDD.reduceByKey(sum_counts)

# Show result
wordcounts_RDD.collect()

Some RDD actions

  • rdd_name.copy: copy all elements to the driver
  • rdd_name.take(n): copy first n elements
  • rdd_name.reduce(func) - aggregate elements with func (takes 2 elements, returns 1)
  • rdd_name.saveAsTextFile(filename) - save to local file or HDFS

Caching data
Once you have prepared your data for your iterative algorithm, for example, a machine learning algorithm, it's a good idea to cache that data. So that each iteration of your algorithm can read data from memory and be very fast.

rdd_name.cache()

Shared Variables

config = sc.broadcast({"order":3, "filter":True})
config.value

Accumulator

accum = sc.accumulator(0)

def test_accum(x):
    accum.add(x)
    
sc.parallelize([1, 2, 3, 4]).foreach(test_accum)
accum.value

hadoop-spark-rdd's People

Contributors

gamboabdoulraoufou avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.