[关闭]
@happyfans 2015-10-30T15:12:52.000000Z 字数 5282 阅读 2575

Hadoop大数据平台架构

hadoop


hadoop的安装

  1. # 解压hadoop-1.1.2.tar.gz到/opt目录
  2. tar zxvf hadoop-1.1.2.tar.gz
  3. # 修改4个配置文件
  4. cd /opt/hadoop-1.1.2/conf

hadoop-env.sh
修改JAVA_HOME,我们可以使用echo $JAVA_HOME命令来找到。
core-site.xml

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <!-- Put site-specific property overrides in this file. -->
  4. <configuration>
  5. <property>
  6. <name>dfs.name.dir</name>
  7. <value>/hadoop/name</value>
  8. </property>
  9. <property>
  10. <name>hadoop.tmp.dir</name>
  11. <value>/hadoop</value>
  12. </property>
  13. <property>
  14. <name>fs.default.name</name>
  15. <value>hdfs://test:9000</value>
  16. </property>
  17. </configuration>

hdfs-site.xml

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <!-- Put site-specific property overrides in this file. -->
  4. <configuration>
  5. <property>
  6. <name>dfs.data.dir</name>
  7. <value>/hadoop/data</value>
  8. </property>
  9. </configuration>

mapred-site.xml

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <!-- Put site-specific property overrides in this file. -->
  4. <configuration>
  5. <property>
  6. <name>mapred.job.tracker</name>
  7. <value>test:9001</value>
  8. </property>
  9. </configuration>

编辑/etc/profile设置HADOOP_HOME,并在PATH中追加hadoop下的bin目录。使用source命令生效。

在任意目录输入hadoop可以测试hadoop是否安装成功。

对namenode进行格式化:

  1. hadoop namenode -format

执行start-all.sh,会依次启动namenode、datanode、secondarynamenode、jobtracker、tasktracker,期间需要输入root密码。我们可以使用jps查看hadoop是否正常运行。

HDFS简介

Block:在操作系统中常常是固定大小的逻辑单元。在HDFS中所有的文件分块存储,HDFS中默认的块的大小时64MB,是文件存储和处理的逻辑单元。

HDFS中有2类节点:NameNode和DataNode。其中NameNode是管理节点,存放文件元数据:
1. 文件与数据块的映射表
2. 数据块和数据节点之间的映射表
hdfs架构图

DataNode是HDFS的管理节点,存放数据块

HDFS数据管理和容错

数据块有多个冗余,DataNode会定期向NameNode发送心跳消息,称之为心跳检测。二级NameNode定期同步元数据映像文件和修复日志,当NameNode故障时备胎转正。

HDFS文件的读写操作

从HDFS中读取文件
向HDFS中写入文件

HDFS特点

HDFS适用性和局限性

HDFS的使用

  1. # 列出HDFS上的文件
  2. hadoop fs -ls /
  3. # 将os上的文件放到HDFS上
  4. hadoop fs -put redis-3.0.5.tar.gz /hadoop/user/root/input
  5. # 查看刚刚放置的文件
  6. hadoop fs -ls /hadoop/user/root/input
  7. # 创建目录
  8. hadoop fs -mkdir tmp
  9. # 查看hdfs上的文件
  10. hadoop fs -put update.js tmp
  11. hadoop fs -cat /user/root/tmp/update.js
  12. # 从hdfs上下载文件
  13. hadoop fs -get tmp/update.js local.js
  14. # 显示hdfs状态
  15. hadoop dfsadmin -report

MapReduce原理

MapReduce简而言之就是一种分治思想,将一个大的任务分成多个小的子任务(Map)执行,最后合并子任务的结果(Reduce)。

mapreduce原理

例如从100GB的网站日志中找出访问次数最多的IP。我们可以将日志文件进行分割,然后分别统计每个分片ip和访问次数,将每个分片的ip进行规约,根据IP来进行hash映射.

MapReduce的运行流程

一个Job会被拆分成多个Task,Task又被分为MapTask和ReduceTask。

MapReduce原理

MapReduce的执行过程
mapreduce执行过程

MapReduce容错机制

wordcount计数程序

计算出文件中每个单词出现的频数,输入结果按照字母顺序进行排序.

编写wordcount.java:

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  15. public class WordCount {
  16. public static class WordCountMap extends
  17. Mapper<LongWritable, Text, Text, IntWritable> {
  18. private final IntWritable one = new IntWritable(1);
  19. private Text word = new Text();
  20. public void map(LongWritable key, Text value, Context context)
  21. throws IOException, InterruptedException {
  22. String line = value.toString();
  23. StringTokenizer token = new StringTokenizer(line);
  24. while (token.hasMoreTokens()) {
  25. word.set(token.nextToken());
  26. context.write(word, one);
  27. }
  28. }
  29. }
  30. public static class WordCountReduce extends
  31. Reducer<Text, IntWritable, Text, IntWritable> {
  32. public void reduce(Text key, Iterable<IntWritable> values,
  33. Context context) throws IOException, InterruptedException {
  34. int sum = 0;
  35. for (IntWritable val : values) {
  36. sum += val.get();
  37. }
  38. context.write(key, new IntWritable(sum));
  39. }
  40. }
  41. public static void main(String[] args) throws Exception {
  42. Configuration conf = new Configuration();
  43. Job job = new Job(conf);
  44. job.setJarByClass(WordCount.class);
  45. job.setJobName("wordcount");
  46. job.setOutputKeyClass(Text.class);
  47. job.setOutputValueClass(IntWritable.class);
  48. job.setMapperClass(WordCountMap.class);
  49. job.setReducerClass(WordCountReduce.class);
  50. job.setInputFormatClass(TextInputFormat.class);
  51. job.setOutputFormatClass(TextOutputFormat.class);
  52. FileInputFormat.addInputPath(job, new Path(args[0]));
  53. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  54. job.waitForCompletion(true);
  55. }
  56. }

在linux中进行编译:

  1. mkdir word_count_class
  2. javac -classpath /opt/hadoop-1.1.2/hadoop-core-1.1.2.jar:/opt/hadoop-1.1.2/lib/commons-cli-1.2.jar -d word_count_class WordCount.java
  3. # 进入编译输出的class目录,发现以下的3个文件
  4. WordCount.class WordCount$WordCountMap.class WordCount$WordCountReduce.class
  5. # 将以上的3个class文件打包
  6. jar -cvf wordcount.jar *.class

在本地新建一个文件~/input/file1,内容是:

  1. hello world
  2. hello hadoop
  3. hadoop file system
  4. hadoop java api
  5. hello java

再新建一个文件~/input/file2,内容是:

  1. new file
  2. hadoop file
  3. hadoop new world
  4. hadoop free home
  5. hadoop free school

将以上的2个文件提交给hdfs

  1. hadoop fs -mkdir input_wordcount && hadoop fs -put ~/input/* input_wordcount
  2. # 查看是否已经成功上传
  3. hadoop fs -ls input_wordcount

提交并查看任务

  1. hadoop jar word_count_class/wordcount.jar WordCount input_wordcount output_wordcount
  2. # 查看结果
  3. hadoop fs -ls output_wordcount
  4. # 成功将两个文件中的单词个数统计了出来并按照字典序进行了排列
  5. hadoop fs -cat output_wordcount/part-r-00000

使用MapReduce进行数据排序

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注