hadoop 使用
DESCRIPTION
Hadoop 使用. 聂志 [email protected]. outline. 云计算概念 Hadoop 使用 Mapreduce 详解. 1 云计算概念. 概念 狭义云计算是指 IT 基础设施的交付和使用模式,通过网络以按需、易扩展的方式获得所需的资源(硬件、平台、软件)。 广义云计算是指服务的交付和使用模式,通过网络以按需、易扩展的方式获得所需的服务。这种服务可以是 IT 和软件、互联网相关的,也可以是任意其他的服务。 三层模型 Saas : more Paas : hadoop Iaas : openstack. - PowerPoint PPT PresentationTRANSCRIPT
Hadoop 使用
outline
1. 云计算概念2. Hadoop 使用3. Mapreduce 详解
1 云计算概念• 概念
狭义云计算是指 IT 基础设施的交付和使用模式,通过网络以按需、易扩展的方式获得所需的资源(硬件、平台、软件)。
广义云计算是指服务的交付和使用模式,通过网络以按需、易扩展的方式获得所需的服务。这种服务可以是 IT 和软件、互联网相关的,也可以是任意其他的服务。
• 三层模型Saas : morePaas : hadoopIaas : openstack
23/4/19
google vs hadoop
Google calls it: Hadoop equivalent:
MapReduce Hadoop
GFS HDFS
Bigtable HBase
Chubby Zookeeper
2 Hadoop 使用
• 编写 map 和 reduce 函数• 运行测试– 命令行运行– eclipse 运行
• 查看结果
23/4/19
map 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); // 设置 key value } } }说明: map 的输出 key 、 value 和 reduce 的输入 key 、 value 要一致,见上面红色部分
reduce 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); } }说明: map 的输出 key 、 value 和 reduce 的输入 key 、 value 要一致,见上面红色部分
23/4/19
Job 配置 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> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); //job name 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(otherArgs[0])); //file input FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //file output System.exit(job.waitForCompletion(true) ? 0 : 1); }23/4/19
2 Hadoop 使用
• 编写 map 和 reduce 函数• 运行测试– 命令行运行– eclipse 运行
• 查看结果
命令行运行• 打包 mapreduce 函数, wordcount.jar
设类名 WordCount• 进入 hadoop 安装目录
• $bin/hadoop jar 本地 jar 包目录 类名 hdfs 输入文件目录 hdfs 输入文件目录
• 例子:• $bin/hadoop jar /home/deke/wordcount.jar WordCount hdfs 输入文件目录 hdfs 输入
文件目录
23/4/19
2 Hadoop 使用
• 编写 map 和 reduce 函数• 运行测试– 命令行运行– eclipse 运行
• 查看结果
Eclipse 配置 • 1. 下载 eclipse• 2. 将 hadoop 文件夹下的 contrib/eclipse-plugin/hadoop-*-eclipse- plugin.jar 拷贝到 eclipse 文件夹下的 /plugins 文件夹里• 3. 启动 Eclipse • 4. 设置 Hadoop 安装文件夹的路径 Window->Preferences ,见下一页
Eclipse 配置
2 Hadoop 使用
• 编写 map 和 reduce 函数• 运行测试– 命令行运行– eclipse 运行
• 查看结果• http://10.77.110.161:50030/jobtracker.jsp• http://10.77.110.161:50070/dfshealth.jsp
3Mapreduce 详解• 程序流程例子 1
3Mapreduce 详解• 程序流程例子 2
直接访问 hdfs 文件接口1 程序如果我们只需要访问文件系统,而不需要对文件中的数据进行处理,那么只需要使用下面的访问 hdfs 的接口就行了。而不需要编写 mapreduce 函数String dir = "/user/nz/btc/pvint"; //the input directory
Configuration conf = new Configuration(); //get confFileSystem fs = FileSystem.get(conf); Path path = new Path(dir); //get the directoryFileStatus stat = fs.getFileStatus(path); //get directory FileStatus[] filelist = fs.listStatus(path); //get file list for(FileStatus list: filelist){ String filename = list.getPath().getName(); System.out.println("result:"+filename);}
23/4/19
直接访问 hdfs 文件接口2 命令行上传文件到 hdfs : bin/hadoop fs -copyFromLocal 本地文件 / 目录 hdfs 文件目录下载到本地: bin/hadoop fs -copyToLocal hdfs 文件目录 本地文件 / 目录
23/4/19
参考文献• JefferyDean, Sanjay Ghemawat.: MapReduce:
Simplified data processing on large clusters. OSDI, San Francisco, CA, 2004.
• S. Ghemawat, H. Gobioff, and S.-T. Leung, The Google File System,in Proceedings of the 19th ACM Symposium on Operating System Principles, 2003.
• http://hadoop.apache.org/
23/4/19