hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹
hadoop1.2.1中使用MultipleOutputs将结果输出到多个文件或文件夹
使用步骤主要有三步:
1、在reduce或map类中创建MultipleOutputs对象,将结果输出
- class reduceStatistics extends Reducer<Text, IntWritable, Text, IntWritable>{
- //将结果输出到多个文件或多个文件夹
- private MultipleOutputs<Text,IntWritable> mos;
- //创建对象
- protected void setup(Context context) throws IOException,InterruptedException {
- mos = new MultipleOutputs<Text, IntWritable>(context);
- }
- //关闭对象
- protected void cleanup(Context context) throws IOException,InterruptedException {
- mos.close();
- }
- }
2、在map或reduce方法中使用MultipleOutputs对象输出数据,代替congtext.write()
- protected void reduce(Text key, Iterable<IntWritable> values, Context context)
- throws IOException, InterruptedException {
- IntWritable V = new IntWritable();
- int sum = 0;
- for(IntWritable value : values){
- sum = sum + value.get();
- }
- System.out.println("word:" + key.toString() + " sum = " + sum);
- V.set(sum);
- //使用MultipleOutputs对象输出数据
- if(key.toString().equals("hello")){
- mos.write("hello", key, V);
- }else if(key.toString().equals("world")){
- mos.write("world", key, V);
- }else if(key.toString().equals("hadoop")){
- //输出到hadoop/hadoopfile-r-00000文件
- mos.write("hadoopfile", key, V, "hadoop/");
- }
- }
3、在创建job时,定义附加的输出文件,这里的文件名称与第二步设置的文件名相同
- //定义附加的输出文件
- MultipleOutputs.addNamedOutput(job,"hello",TextOutputFormat.class,Text.class,IntWritable.class);
- MultipleOutputs.addNamedOutput(job,"world",TextOutputFormat.class,Text.class,IntWritable.class);
- MultipleOutputs.addNamedOutput(job,"hadoopfile",TextOutputFormat.class,Text.class,IntWritable.class);
完整代码:
- package com.ru.hadoop.wordcount;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.regex.Pattern;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.NullWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapred.RecordWriter;
- import org.apache.hadoop.mapred.lib.MultipleOutputFormat;
- import org.apache.hadoop.mapred.lib.MultipleTextOutputFormat;
- 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.mapreduce.lib.output.MultipleOutputs;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Progressable;
- public class WordCount2 extends Configured{
- public static void main(String[] args) {
- String in = "/home/nange/work/test/word/";
- String out = "hdfs://localhost:9000/hdfs/test/wordcount/out/";
- Job job;
- try {
- //删除hdfs目录
- WordCount2 wc2 = new WordCount2();
- wc2.removeDir(out);
- job = new Job(new Configuration(), "wordcount Job");
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- job.setMapperClass(mapperString.class);
- // job.setCombinerClass(reduceStatistics.class);
- job.setReducerClass(reduceStatistics.class);
- //定义附加的输出文件
- MultipleOutputs.addNamedOutput(job,"hello",TextOutputFormat.class,Text.class,IntWritable.class);
- MultipleOutputs.addNamedOutput(job,"world",TextOutputFormat.class,Text.class,IntWritable.class);
- MultipleOutputs.addNamedOutput(job,"hadoopfile",TextOutputFormat.class,Text.class,IntWritable.class);
- FileInputFormat.addInputPath(job, new Path(in));
- FileOutputFormat.setOutputPath(job, new Path(out));
- job.waitForCompletion(true);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void removeDir(String filePath) throws IOException, URISyntaxException{
- String url = "hdfs://localhost:9000";
- FileSystem fs = FileSystem.get(new URI(url), new Configuration());
- fs.delete(new Path(filePath));
- }
- }
- /**
- * 重写maptask使用的map方法
- * @author nange
- *
- */
- class mapperString extends Mapper<LongWritable, Text, Text, IntWritable>{
- //设置正则表达式的编译表达形式
- public static Pattern PATTERN = Pattern.compile(" ");
- Text K = new Text();
- IntWritable V = new IntWritable(1);
- @Override
- protected void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- String[] words = PATTERN.split(value.toString());
- System.out.println("********" + value.toString());
- for(String word : words){
- K.set(word);
- context.write(K, V);
- }
- }
- }
- /**
- * 对单词做统计
- * @author nange
- *
- */
- class reduceStatistics extends Reducer<Text, IntWritable, Text, IntWritable>{
- //将结果输出到多个文件或多个文件夹
- private MultipleOutputs<Text,IntWritable> mos;
- //创建MultipleOutputs对象
- protected void setup(Context context) throws IOException,InterruptedException {
- mos = new MultipleOutputs<Text, IntWritable>(context);
- }
- @Override
- protected void reduce(Text key, Iterable<IntWritable> values, Context context)
- throws IOException, InterruptedException {
- IntWritable V = new IntWritable();
- int sum = 0;
- for(IntWritable value : values){
- sum = sum + value.get();
- }
- System.out.println("word:" + key.toString() + " sum = " + sum);
- V.set(sum);
- //使用MultipleOutputs对象输出数据
- if(key.toString().equals("hello")){
- mos.write("hello", key, V);
- }else if(key.toString().equals("world")){
- mos.write("world", key, V);
- }else if(key.toString().equals("hadoop")){
- //输出到hadoop/hadoopfile-r-00000文件
- mos.write("hadoopfile", key, V, "hadoop/");
- }
- }
- //关闭MultipleOutputs对象
- protected void cleanup(Context context) throws IOException,InterruptedException {
- mos.close();
- }
- }
hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹的更多相关文章
- java 写一个"HelloJavaWorld你好世界"输出到操作系统文件Hello.txt文件中
package com.beiwo.homework; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...
- Java递归输出指定路径下所有文件及文件夹
package a.ab; import java.io.File; import java.io.IOException; public class AE { public static void ...
- Java基础知识强化之IO流笔记14:递归之输出指定目录下所有java文件绝对路径的案例
1. 需求:输出指定目录下的所以.java结尾文件的绝对路径的案例: 分析: A:封装目录 B:获取该目录下的所有文件和文件夹的File数组 C:遍历这个File数组,得到每一个File对象的 ...
- php遍历目录输出目录及其下的所有图片文件
在做网站的时候,需要给文章内所有的图片添加上logo,如何利用ThinkPHP来实现. ThinkPHP为我们很好的提供了图像处理类,给文章中的所有图片加上水印的思路,上传的图片文件都保存在一个文件夹 ...
- 将Maple输出的LaTex导出到txt文件
将Maple输出的LaTex导出到txt文件 1. 生成LATEX Maple可以把它的表达式转换成LATEX, 使用latex命令即可: > latex(x^2+y^2=z^2); {x}^{ ...
- Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间
遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...
- java代码实现输出指定以.java结尾的文件的绝对路径
package 输出指定文件绝对路径; import java.io.File; /* * 需求:请大家把"E:\\JAVA语言"文件夹下全部的java结尾的文件的绝对路径给输出在 ...
- 如何在屏幕上查看命令的输出以及在Linux中写入文件
在Linux中输出命令可以做很多事情(http://www.nanke0834.com) 您可以将命令的输出分配给变量,将其发送到另一个命令/程序以通过管道进行处理或将其重定向到文件以进行进一步分析. ...
- Linux命令nohup实现命令后台运行并输出到或记录到日志文件
Linux命令nohup实现命令后台运行并输出到或记录到日志文件 导读 我们在调试程序的时候,免不了要去抓一些 log ,然后进行分析.如果 log 量不是很大的话,那很简单,只需简单的复制粘贴就好. ...
随机推荐
- 解题的小问题(C++)
1.判断一个数是否为整数 if(m==(int)m) 2.#include <bits/stdc++.h>using namespace std;int main(){ int n ...
- HttpWatch工具简介及使用技巧(转载)
一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...
- ubuntu如何实现访问实际网络中windows共享文件夹
方法一: 首先在建立一个挂载目录. sudo mkdir /mnt/share 然后就把共享目录持载进去. 服务器:192.168.6.84 共享名:gg 用户名:administrator 密 码: ...
- ReactiveCocoa常用方法
//1 代替kvo [[self.redView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id x) ...
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)
Java1.5泛型指南中文版(Java1.5 Generic Tutorial) 英文版pdf下载链接:http://java.sun.com/j2se/1.5/pdf/generics-tutori ...
- hdu_5748_Bellovin(LIS)
题目链接:hdu_5748_Bellovin 题意: 给你一个数列ai,设f(a1,a2,a3,..an)=(f1,f2,f3,...,fn),其中fi表示以ai结尾的最长递增子序列长度,注意:必须要 ...
- Entity Framework技巧系列之八 - Tip 29 – 34
提示29. 怎样避免延迟加载或Load()阅读器问题 如果你有如下这样的代码: 1 var results = from c in ctx.Customers 2 where c.SalesPerso ...
- how to write a struct to a file directly?
Using write and read system call. Following is an example: blk.h: #include <stdlib.h> #include ...
- flash/flex 编译错误汇总
来源:http://blog.chinaunix.net/uid-366408-id-116463.html 代码 消息 说明 1000 对 %s 的引用不明确. 引用可能指向多项.例如,下面使用 ...
- jni开发中的常见错误
* java.lang.UnsatisfiedLinkError: Native method not found: 本地方法没有找到 * 本地函数名写错 * 忘记加载.so文件 没有调用System ...