共计 5067 个字符,预计需要花费 13 分钟才能阅读完成。
一、概述
1. 实验使用的 Hadoop 集群为伪分布式模式,Eclipse 相关配置已完成;
2. 软件版本为 hadoop-2.7.3.tar.gz、apache-maven-3.5.0.rar。
二、使用 eclipse 连接 hadoop 集群进行开发
1. 在开发主机上配置 hadoop
①将 hadoop-2.7.3.tar.gz 解压到本地主机上
②使用 windows 版本的 hadoop 中的 bin 替换目标中的 bin 文件夹
③配置 windows 上的 hadoop 环境变量
2. 在 eclipse 上配置 hadoop 集群信息
①在 eclipse 中添加 hadoop 路径
②配置 hadoop 集群访问信息
3. 在 hadoop 集群中取消权限验证
hdfs-site.xml
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
4. 创建一个文件测试连接权限
5. 安装 maven
①将 maven 解压到开发主机上
②在 eclipse 上添加 maven 路径
5. 新建 maven 工程
6. 修改 maven 配置文件(maven/pom.xml)
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
7. 新建一个类用于测试(WordCount)
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;
import org.apache.hadoop.util.GenericOptionsParser;
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();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println(“Usage: wordcount <in> [<in>…] <out>”);
System.exit(2);
}
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);
for (int i = 0; i < otherArgs.length – 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length – 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
8. 配置 WordCount
①将 log4j.properties 移动到 WordCount 类下
②设置 WordCount 的运行自变量
8. 运行测试
三、jar 包的导出与提交执行
1. 导出 WordCount
2. 将导出的 jar 包上传到 hadoop 集群
[hadoop@hadoop ~]$ ls
wc.jar
3. 运行
[hadoop@hadoop ~]$ hadoop jar wc.jar WordCount /user/hadoop/input/* /user/hadoop/output/out
17/09/06 22:36:56 INFO client.RMProxy: Connecting to ResourceManager at hadoop/192.168.100.141:8032
17/09/06 22:36:57 INFO input.FileInputFormat: Total input paths to process : 1
17/09/06 22:36:58 INFO mapreduce.JobSubmitter: number of splits:1
17/09/06 22:36:58 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1504744740212_0001
17/09/06 22:36:59 INFO impl.YarnClientImpl: Submitted application application_1504744740212_0001
17/09/06 22:36:59 INFO mapreduce.Job: The url to track the job: http://hadoop:8088/proxy/application_1504744740212_0001/
17/09/06 22:36:59 INFO mapreduce.Job: Running job: job_1504744740212_0001
17/09/06 22:37:36 INFO mapreduce.Job: Job job_1504744740212_0001 running in uber mode : false
17/09/06 22:37:36 INFO mapreduce.Job: map 0% reduce 0%
17/09/06 22:38:26 INFO mapreduce.Job: map 100% reduce 0%
17/09/06 22:38:42 INFO mapreduce.Job: map 100% reduce 100%
17/09/06 22:38:46 INFO mapreduce.Job: Job job_1504744740212_0001 completed successfully
4. 查看运行结果
[hadoop@hadoop ~]$ hdfs dfs -cat /user/hadoop/output/out/part-r-00000
“AS 1
“GCC 1
“License”); 1
& 1
‘Aalto 1
‘Apache 4
‘ArrayDeque’, 1
‘Bouncy 1
‘Caliper’, 1
‘Compress-LZF’, 1
……
Hadoop2.3-HA 高可用集群环境搭建 http://www.linuxidc.com/Linux/2017-03/142155.htm
Hadoop 项目之基于 CentOS7 的 Cloudera 5.10.1(CDH)的安装部署 http://www.linuxidc.com/Linux/2017-04/143095.htm
Hadoop2.7.2 集群搭建详解(高可用)http://www.linuxidc.com/Linux/2017-03/142052.htm
使用 Ambari 来部署 Hadoop 集群(搭建内网 HDP 源)http://www.linuxidc.com/Linux/2017-03/142136.htm
Ubuntu 14.04 下 Hadoop 集群安装 http://www.linuxidc.com/Linux/2017-02/140783.htm
CentOS 6.7 安装 Hadoop 2.7.2 http://www.linuxidc.com/Linux/2017-08/146232.htm
Ubuntu 16.04 上构建分布式 Hadoop-2.7.3 集群 http://www.linuxidc.com/Linux/2017-07/145503.htm
CentOS 7 下 Hadoop 2.6.4 分布式集群环境搭建 http://www.linuxidc.com/Linux/2017-06/144932.htm
Hadoop2.7.3+Spark2.1.0 完全分布式集群搭建过程 http://www.linuxidc.com/Linux/2017-06/144926.htm
更多 Hadoop 相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147639.htm