Legacy Archive (2015): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

Spark Notes Part 1: First Impressions

Original Chinese title: Spark 手册一:初识

  • I first heard about Spark a year ago at an Alibaba tech salon, where Storm was also on the agenda. A year later—after dealing with all sorts of odds and ends, messing around with Hadoop for a long time, and switching over to Python—I finally decided to try using Spark for some actual data analysis. Jotting down a few notes here for future reference.

Getting Started and Basics

Setting Up the Cluster

  1. Hadoop/Spark Cluster Setup
  2. spark1.3.1 clusters
  • You can’t install the JDK with brew. Running jps in zsh and iTerm, though, opens the download page directly. Awesome.
  • I accidentally ran sudo chmod -R 777 /usr and messed up the permissions on the /usr files, which wrecked one of the nodes. Someone else has definitely made this exact mistake before, so of course there is a solution.

You Must Have Scala to Run a Cluster

  • Just download a recent version. Recording some configuration snippets:

    export JAVA_HOME=/usr/java
    export JRE_HOME=/usr/java/jre
    export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
    export CLASSPATH=.:$JAVA_HOME/lib/jt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
    export HADOOP_HOME=/usr/hadoop
    export PATH=$PATH:$HADOOP_HOME/bin
    export HBASE_HOME=/usr/hbase
    export PATH=$PATH:$HBASE_HOME/bin
    export HIVE_HOME=/usr/hive
    export PATH=$PATH:$HIVE_HOME/bin
    export OOZIE_HOME=/usr/oozie/oozie-4.0.0-cdh5.2.0
    export OOZIE_CONFIG=$OOZIE_HOME/conf
    export CLASSPATH=$CLASSPATH:$OOZIE_HOME/bin
    export SPARK_HOME=/usr/spark
    export PATH=$SPARK_HOME/bin:$PATH
    export SCALA_HOME=/usr/scala
    export PATH=$PATH:$SCALA_HOME/bin

Copying Cluster Config with scp

  • Configuration references this article:

    sudo scp /etc/profile slave4:/etc/profile
    
    vi spark-env.sh
    
    export SPARK_MASTER_IP=master
    export SPARK_MASTER_PORT=7077
    export SPARK_WORKER_CORES=1
    export SPARK_WORKER_INSTANCES=1
    export SPARK_WORKER_MEMORY=1g
    export JAVA_HOME=/usr/java

Testing a Small Program

// 从hdfs读文件
scala> val file=sc.textFile("hdfs://172.16.48.202:9000/user/root/input/README.txt")  
// 根据空格分词
scala> val count=file.flatMap(line => line.split(" ")).map(word => (word,1)).reduceByKey(_+_)
//统计输出 
scala> count.collect()

Have fun