Giter Club home page Giter Club logo

run-hadoop-in-linux's Introduction

How to run Hadoop in Linux

Table of Content

Prerequisites

Setting up a Pseudo-Distributed in Yarn Mode

Goal: Start successful daemon:

  • ResourceMananger
  • NodeManager
  • Namenode
  • SecondaryNamenode
  • Datanode

Install Java JDK 8 ,ssh and pdsh

  $ sudo apt-get install openjdk-8-jdk
  $ sudo apt-get install ssh
  $ sudo apt-get install pdsh

Setup passphraseless ssh

  $ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  $ chmod 0600 ~/.ssh/authorized_keys

Note:

  • pdsh uses rsh by default, not ssh. So, add a line to the end of file ~/.bashrc:
export PDSH_RCMD_TYPE=ssh

Edit environment variables

  1. Set variable in ~/.bashrc:
export JAVA_HOME=/usr/java/latest
export HADOOP_HOME=/path/to/hadoop
export PATH=${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin:${PATH}
  1. Set variable in ${HADOOP_HOME}/etc/hadoop/hadoop-env.sh:

Uncomment JAVA_HOME and set:

export JAVA_HOME=/usr/java/latest

Note:

  • /usr/java/latest can be usr/liv/jvm/java-1.8.0-openjdk-amd64

  • All terminal command is executed in directory $HADOOP_HOME

Configuration

Edit files:

  • ${HADOOP_HOME}/etc/hadoop/core-site.xml
<configuration>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>

    <property>
        <name>hadoop.tmp.dir</name>
        <value>/home/username/tmp</value>
    </property>

</configuration>

Note: hadoop.tmp.dir value is where Hadoop namenode, datanode and namenode secondary store its data, by default is /tmp/ dir, it is deleted after restart

  • ${HADOOP_HOME}/etc/hadoop/hdfs-site.xml
<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
</configuration>
  • ${HADOOP_HOME}/etc/hadoop/mapreduce-site.xml
<configuration>
    <property>
        <name>mapreduce.framework.name</name>
        <value>yarn</value>
    </property>
    <property>
        <name>mapreduce.application.classpath</name>
        <value>$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/*:$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/lib/*</value>
    </property>
</configuration>
  • ${HADOOP_HOME}/etc/hadoop/yarn-site.xml
<configuration>
    <property>
        <name>yarn.nodemanager.aux-services</name>
        <value>mapreduce_shuffle</value>
    </property>
    <property>
        <name>yarn.nodemanager.env-whitelist</name>
        <value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_HOME,PATH,LANG,TZ,HADOOP_MAPRED_HOME</value>
    </property>
</configuration>

Execution

  1. Format the filesystem for first time:
  $ hdfs namenode -format
  1. Start ResourceManager daemon and NodeManager daemon:
 $ start-yarn.sh
  1. Start NameNode daemon and DataNode daemon:
 $ start-dfs.sh

Note: You can use command start-all.sh to start all daemons

Check for working:

jps

Run a MapReduce job

Create WordCount Job to sumbit

Create a WordCount.java file:

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

To compile WordCount.java file above, we need set classpath to hadoop classpath, by following step:

  1. Get all hadoop classpath:
hadoop classpath
  1. Copy all hadoop classpath, edit ~/.bashrc file:
export CLASSPATH='content of hadoop classpath' 
  1. Apply the change:
source ~/.bashrc

Complile WordCount.java:

javac WordCount.java

After compile, it generates 3 classes file: WordCount.class, WordCount$TokenizerMapper.class, WordCount$IntSumReducer.class.

Create a jar file from these classes:

jar cf wc.jar WordCount*.class

Note: Các command được thực hiện trên working directory: $HADOOP_HOME

  • Create HDFS directories:
$ bin/hdfs dfs -mkdir -p /user/
  • Copy files or directories from your local file system to Hadoop's HDFS
bin/hdfs dfs -put from_local to_hdfs 

Prepare data in HDFS

Create txt file in local and add some content:

touch wc_data.txt

nano wc_data.txt

# Add some content and close file

Upload data from local to HDFS:

hadoop fs -mkdir -p /user/username/

hadoop fs -put wc_data.txt /user/username/wc_data.txt

Run a MapReduce job locally

hadoop jar wc.jar WordCount /user/username/wc_data.txt /user/username/output

Run a MapReduce job on YARN

yarn jar wc.jar WordCount /user/username/wc_data.txt /user/username/output

When you’re done, stop the daemons with:

stop-yarn.sh

stop-dfs.sh

Web interface

You can see the namenode, datanode and mapreduce job status in web interface

run-hadoop-in-linux's People

Contributors

quanbkit1 avatar

Stargazers

Serhii Biruk avatar dyuMV11 avatar  avatar

Watchers

 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.