最近开始接触大数据方面的学习,准备做一个系列笔记来介绍笔者的学习过程。文章简单介绍Hadoop的集群搭建、Mapreduce编程的主要流程及如何提交到Linux服务器中的yarn运行MapReduce程序。针对一些常见问题也作了简单的说明。
HADOOP集群搭建
HADOOP是利用服务器集群,根据用户的业务逻辑,对海量数据进行分布式处理的大数据框架。主要核心组件有分布式文件系统(HDFS)、运算资源调度系统(YARN)、分布式运算框架(MAPREDUCE)。HADOOP集群搭建就是在N台Linux服务器中搭建HDFS集群和YARN集群。
1.HADOOP是用JAVA开发的框架,因此需要依赖JDK,关于JDK在Linux中的配置本文就不再说明,网上有很多可以参考,安装过程并不难。
2.在官网上下载HADOOP编译文件,即hadoop-xxx-tar.gz文件上到Linux服务器中解压(注意版本要与Linux版本配合,本文用的是Hadoop-2.6.4版本,Linux版本是Centos6.5);
3.配置好集群中的各服务器的IP、主机名、hosts文件等、配置ssh免登陆,关闭防火墙、配置Hadoop的环境变量/etc/profile
ssh免登陆配置:假如主机A 要ssh登陆主机 B,在主机A上操作:首先生成密钥对
ssh-keygen
(提示时,直接回车即可),再将A自己的公钥拷贝并追加到B的授权列表文件authorized_keys中ssh-copy-id B
,其中A、B分别为两台Linux的hostname
4.配置hodoop /etc/hodoop/
下的各文件
hadoop-env.sh添加内容
export JAVA_HOME=/home/hadoop/apps/jdk1.7.0_51
core-site.xml文件添加内容
hdfs-site.xml添加内容
mapred-site.xml添加内容
yarn-site.xml添加内容
salves (指定集群节点)
hdp-node-01
hdp-node-02
hdp-node-03
5.启动集群
#初始化
bin/hadoop namenode -format
#启动HDFS
sbin/start-dfs.sh
#启动yarn
sbin/start-yarn.sh
测试集群
通过hadoop fs -ls /
测试hadoop是否成功,也可通过jps
命令查看java进程是否有NameNode、ResourceManager、DataNode、NodeMagnager进程
- 通过JAVA API上传文件
其中192.168.10.121为其中一个节点IP地址,最好在Linux中搭建Eclipse开发环境。
public class HdfsClient {
private FileSystem fs;
@Before
public void init() throws Exception {
Configuration conf = new Configuration();
conf.set("dfs.replication", "2");
fs = FileSystem.get(new URI("hdfs://192.168.10.121:9000"), conf, "jeang");
}
@Test
public void testAddHdfsFile() throws Exception {
Path src = new Path("E:/testHdfs.txt");
Path dst = new Path("/wordcount/data.txt");
fs.copyFromLocalFile(src, dst);
fs.close(); //关闭资源
}
}
hadoop fs -ls /wordcount
查看是否上传成功,如下图表示上传成功
MapReduce程序
主要包括Map、Reduce类、方法实现以及yarn任务提交main类
- Map类
public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable>{
/**
* 复写方法
*/
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
//拿到第一行数据并转为String
String line = value.toString();
String[] words = line.split(" ");
//遍历单词
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}
}
- Reduce类
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
/**
* 每map后进来一个KV,就执行reduce方法一次
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
//计数器
int wordCount = 0;
for (IntWritable value : values) {
wordCount += value.get();
}
context.write(key, new IntWritable(wordCount));
}
}
- yarn任务提交类
public class YarnRunner {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
/*conf.set("mapreduce.framework.name", "yarn");
conf.set("yarn.resourcemanager.hostname", "server1");*/
Job job = Job.getInstance(conf);
job.setJarByClass(YarnRunner.class);
job.setMapperClass(WordCountMap.class);
job.setReducerClass(WordCountReduce.class);
//设置业务逻辑:Map\Reduce的输入输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//指定位置
FileInputFormat.setInputPaths(job, new Path("hdfs://server1:9000/wordcount/data.txt"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://server1:9000/wordcount/result"));
//提交任务,打印信息
boolean completion = job.waitForCompletion(true);
System.exit(completion ? 0 : 1);
}
}
导出jar包,上传到集群中,运行jar包,指定运行类
一个常见的问题是运行jar包时,出现拒绝连接的情况,这一般是Linux环境配置导致的,编辑
/etc/hosts
文件,将IP与主机名称映射,示例如下,其中server1~server4是集群中的节点IP
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost localhost6.localdomain6
192.168.10.121 server1
192.168.10.122 server2
192.168.10.123 server3
192.168.10.124 server4查看结果