hadoop的三种运行模式

转载:http://www.2cto.com/kf/201607/522848.html

Hadoop的运行模式分为3种:本地运行模式,伪分布运行模式,集群运行模式,相应概念如下:

1、独立模式即本地运行模式(standalone或local mode)
无需运行任何守护进程(daemon),所有程序都在单个JVM上执行。由于在本机模式下测试和调试MapReduce程序较为方便,因此,这种模式适宜用在开发阶段。
2、伪分布运行模式
伪分布:如果Hadoop对应的Java进程都运行在一个物理机器上,称为伪分布运行模式,如下图所示:

?
1
2
3
4
5
6
7
<codeclass="hljs ruby">[root@hadoop20dir2]# jps
8993Jps
7409SecondaryNameNode
7142NameNode
7260DataNode
8685NodeManager
8590ResourceManager</code>

3、集群模式
如果Hadoop对应的Java进程运行在多台物理机器上,称为集群模式.[集群就是有主有从] ,如下图所示:

?
1
2
3
4
<codeclass="hljs perl">[root@hadoop11local]# jps
18046NameNode
30927Jps
18225SecondaryNameNode</code>
?
1
2
3
<codeclass="hljs ruby">[root@hadoop22~]# jps
9741ResourceManager
16569Jps</code>
?
1
2
3
4
<codeclass="hljs ruby">[root@hadoop33~]# jps
12775DataNode
20189Jps
12653NodeManager</code>
?
1
2
3
4
<codeclass="hljs ruby">[root@hadoop44~]# jps
10111DataNode
17519Jps
9988NodeManager</code>
?
1
2
3
4
<codeclass="hljs ruby">[root@hadoop55~]# jps
11563NodeManager
11686DataNode
19078Jps</code>
?
1
2
3
4
<codeclass="hljs ruby">[root@hadoop66~]# jps
10682DataNode
10560NodeManager
18085Jps</code>

注意:伪分布模式就是在一台服务器上面模拟集群环境,但仅仅是机器数量少,其通信机制与运行过程与真正的集群模式是一样的,hadoop的伪分布运行模式可以看做是集群运行模式的特殊情况。
为了方便文章的后续说明,先介绍一下hadoop的体系结构:
这里写图片描述从Hadoop的体系结构可以看出,HDFS与MapReduce分别是Hadoop的标配文件系统与标配计算框架,但是呢?–我们完全可以选择别的文件系统(如Windows的NTFS,Linux的ext4)与别的计算框架(如spark、storm等)为Hadoop所服务,这恰恰说明了hadoop的松耦合性。在hadoop的配置文件中,我们是通过core-site.xml这个配置文件指定所用的文件系统的。

?
1
2
3
4
<codeclass="hljs xml"><property>
    <name>fs.defaultFS</name>
    <value>hdfs://hadoop11:9000</value>
</property></code>

下面将基于Linux与Windows两种开发环境详细说明hadoop的本地运行模式,其中核心知识点如下:
Hadoop的本地执行模式:
1、在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行
—-输入输出数据可以放在本地路径下(c:/wc/srcdata/)
—-输入输出数据也可以放在hdfs中(hdfs://hadoop20:9000/dir)

2、在linux的eclipse里面直接运行main方法,但是不要添加yarn相关的配置,也会提交给localjobrunner执行
—-输入输出数据可以放在本地路径下(/usr/local/)
—-输入输出数据也可以放在hdfs中(hdfs://hadoop20:9000/dir)
首先先基于Linux的开发环境进行介绍:
这里写图片描述



以WordCount程序为例,输入输出文件都放在本地路径下,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<codeclass="hljs avrasm">packageMapReduce;
 
importjava.io.IOException;
 
 
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.input.TextInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
importorg.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
importorg.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
 
 
 
 
publicclass WordCount
{
     publicstatic String path1 = "file:///usr/local/word.txt"; //file:///代表本地文件系统中路径的意思
     publicstatic String path2 = "file:///usr/local/dir1";
     publicstatic void main(String[] args) throwsException
     {
         Configuration conf = newConfiguration();
         FileSystem fileSystem = FileSystem.get(conf);
 
         if(fileSystem.exists(newPath(path2)))
         {
             fileSystem.delete(newPath(path2), true);
         }
         Job job = Job.getInstance(conf);
         job.setJarByClass(WordCount.class);
 
         FileInputFormat.setInputPaths(job,newPath(path1));
         job.setInputFormatClass(TextInputFormat.class);
         job.setMapperClass(MyMapper.class);
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(LongWritable.class);
 
         job.setNumReduceTasks(1);
         job.setPartitionerClass(HashPartitioner.class);
 
 
         job.setReducerClass(MyReducer.class);
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(LongWritable.class);
         job.setOutputFormatClass(TextOutputFormat.class);
         FileOutputFormat.setOutputPath(job,newPath(path2));
         job.waitForCompletion(true);
     }   
     public static  class MyMapper extendsMapper<longwritable, longwritable="">
     {
             protectedvoid map(LongWritable k1, Text v1,Context context)throwsIOException, InterruptedException
            {
                 String[] splited = v1.toString().split("\t");
                 for(String string : splited)
                {
                       context.write(newText(string),newLongWritable(1L));
                }
            }    
     }
     public static class MyReducer extendsReducer<text, longwritable="">
     {
        protectedvoid reduce(Text k2, Iterable<longwritable> v2s,Context context)throwsIOException, InterruptedException
        {
                 longsum = 0L;
                 for(LongWritable v2 : v2s)
                {
                    sum += v2.get();
                }
                context.write(k2,newLongWritable(sum));
        }
     }
}
</longwritable></text,></longwritable,></code>

在程序的运行过程中,相应的java进程如下:

?
1
2
3
4
<codeclass="hljs cs">[root@hadoop20local]# jps
7621               //对应的是启动的eclipse
9833Jps
9790WordCount      //对应的是WordCount程序</code>

下面我们在本地查看运行结果:

?
1
2
3
4
5
6
<codeclass="hljs perl">[root@hadoop20dir]# pwd
/usr/local/dir1
[root@hadoop20dir1]# more part-r-00000
hello  2
me     1
you    1</code>

接下来我们将输入路径选择HDFS文件系统中的路径,输出路径还是本地linux文件系统,首先我们在linux上面启动HDFS分布式文件系统。

?
1
2
3
4
5
6
7
8
9
10
11
12
<codeclass="hljs applescript">[root@hadoop20dir]# start-dfs.sh
Starting namenodes on [hadoop20]
hadoop20: starting namenode, logging to /usr/local/hadoop/logs/hadoop-root-namenode-hadoop20.out
hadoop20: starting datanode, logging to /usr/local/hadoop/logs/hadoop-root-datanode-hadoop20.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /usr/local/hadoop/logs/hadoop-root-secondarynamenode-hadoop20.out
[root@hadoop20dir]# jps
10260SecondaryNameNode
7621
10360Jps
9995NameNode
10110DataNode</code>

还是以WordCount程序为例,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<codeclass="hljs avrasm">packageMapReduce;
 
importjava.io.IOException;
 
 
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.input.TextInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
importorg.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
importorg.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
 
 
 
 
publicclass WordCount
{
     publicstatic String path1 = "hdfs://hadoop90:2000/word.txt";//读取HDFS中的测试集
     publicstatic String path2 = "file:///usr/local/dir2";  //输出数据输出到本地文件系统中
     publicstatic void main(String[] args) throwsException
     {
         Configuration conf = newConfiguration();
         FileSystem fileSystem = FileSystem.get(conf);//默认获取的是本地文件系统的FileSystem实例(在这里就是linux文件系统的实例)
 
         if(fileSystem.exists(newPath(path2)))
         {
             fileSystem.delete(newPath(path2), true);
         }
         Job job = Job.getInstance(conf);
         job.setJarByClass(WordCount.class);
 
         FileInputFormat.setInputPaths(job,newPath(path1));
         job.setInputFormatClass(TextInputFormat.class);
         job.setMapperClass(MyMapper.class);
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(LongWritable.class);
 
         job.setNumReduceTasks(1);
         job.setPartitionerClass(HashPartitioner.class);
 
 
         job.setReducerClass(MyReducer.class);
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(LongWritable.class);
         job.setOutputFormatClass(TextOutputFormat.class);
         FileOutputFormat.setOutputPath(job,newPath(path2));
         job.waitForCompletion(true);
     }   
     public static  class MyMapper extendsMapper<longwritable, longwritable="">
     {
             protectedvoid map(LongWritable k1, Text v1,Context context)throwsIOException, InterruptedException
            {
                 String[] splited = v1.toString().split("\t");
                 for(String string : splited)
                {
                       context.write(newText(string),newLongWritable(1L));
                }
            }    
     }
     public static class MyReducer extendsReducer<text, longwritable="">
     {
        protectedvoid reduce(Text k2, Iterable<longwritable> v2s,Context context)throwsIOException, InterruptedException
        {
                 longsum = 0L;
                 for(LongWritable v2 : v2s)
                {
                    sum += v2.get();
                }
                context.write(k2,newLongWritable(sum));
        }
     }
}
</longwritable></text,></longwritable,></code>

运行结果如下:

?
1
2
3
4
5
6
<codeclass="hljs perl">[root@hadoop20dir2]# more part-r-00000
hello  2
me     1
you    1
[root@hadoop20dir2]# pwd
/usr/local/dir2</code>

接下来我们将输入输出路径都换成HDFS中的路径:
代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<codeclass="hljs avrasm">packageMapReduce;
 
importjava.io.IOException;
 
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.input.TextInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
importorg.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
importorg.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
 
 
publicclass WordCount
{
     publicstatic String path1 = "hdfs://hadoop20:9000/word.txt";//读取HDFS中的测试集
     publicstatic String path2 = "hdfs://hadoop20:9000/dir3";
     publicstatic void main(String[] args) throwsException
     {
         Configuration conf = newConfiguration();
         FileSystem fileSystem = FileSystem.get(conf);
 
         if(fileSystem.exists(newPath(path2)))
         {
             fileSystem.delete(newPath(path2), true);
         }
         Job job = Job.getInstance(conf);
         job.setJarByClass(WordCount.class);
 
         FileInputFormat.setInputPaths(job,newPath(path1));
         job.setInputFormatClass(TextInputFormat.class);
         job.setMapperClass(MyMapper.class);
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(LongWritable.class);
 
         job.setNumReduceTasks(1);
         job.setPartitionerClass(HashPartitioner.class);
 
 
         job.setReducerClass(MyReducer.class);
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(LongWritable.class);
         job.setOutputFormatClass(TextOutputFormat.class);
         FileOutputFormat.setOutputPath(job,newPath(path2));
         job.waitForCompletion(true);
     }   
     public static  class MyMapper extendsMapper<longwritable, longwritable="">
     {
             protectedvoid map(LongWritable k1, Text v1,Context context)throwsIOException, InterruptedException
            {
                 String[] splited = v1.toString().split("\t");
                 for(String string : splited)
                {
                       context.write(newText(string),newLongWritable(1L));
                }
            }    
     }
     public static class MyReducer extendsReducer<text, longwritable="">
     {
        protectedvoid reduce(Text k2, Iterable<longwritable> v2s,Context context)throwsIOException, InterruptedException
        {
                 longsum = 0L;
                 for(LongWritable v2 : v2s)
                {
                    sum += v2.get();
                }
                context.write(k2,newLongWritable(sum));
        }
     }
}</longwritable></text,></longwritable,></code>

程序抛出异常:
这里写图片描述
处理措施:

?
1
2
<codeclass="hljs cs">Configuration conf = newConfiguration();
conf.set("fs.defaultFS","hdfs://hadoop20:9000/");//加入此行代码,表示获取HDFS中的FileSystem实例,而不在是默认linux文件系统的FileSystem实例</code>

查看运行结果:

?
1
2
3
4
<codeclass="hljs ruby">[root@hadoop20hadoop]# hadoop fs -cat /dir3/part-r-00000
hello  2
me     1
you    1</code>

好了,从上面的3个例子可以看出,在Linux这种开发环境下,Hadoop的本地运行模式是很简单的,不用配置任何文件,但是在Windows开发环境下,我们却需要配置很多文件。
在这里先说明一下,因为我的电脑是64位,所以我在windows上面安装的jdk1.7、eclipse、hadoop2.4.1都是64位的,下载链接如下:
http://blog.csdn.net/a2011480169/article/details/51814212
在Windows开发环境中实现Hadoop的本地运行模式,详细步骤如下:
1、在本地安装好jdk、hadoop2.4.1,并配置好环境变量:JAVA_HOME、HADOOP_HOME、Path路径(配置好环境变量后最好重启电脑)。
这里写图片描述


这里写图片描述
这里写图片描述
这里写图片描述
2、用hadoop-common-2.2.0-bin-master的bin目录替换本地hadoop2.4.1的bin目录,因为hadoop2.0版本中没有hadoop.dll和winutils.exe这两个文件。
hadoop-common-2.2.0-bin-master的下载链接如下:
http://blog.csdn.net/a2011480169/article/details/51814212
如果缺少hadoop.dll和winutils.exe话,程序将会抛出下面异常:

?
1
<codeclass="hljs lua">java.io.IOException: Could not locate executable D:\hadoop-2.4.1\bin\winutils.exe in the Hadoop binaries.</code>
?
1