Hadoop的多目录输出 -2

来源:互联网 发布:付费的炒股软件 编辑:程序博客网 时间:2024/06/16 01:01

两个主要的方法:







代码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package mapreduce.baozi;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.conf.Configured;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.LongWritable;  
  10. import org.apache.hadoop.io.Text;  
  11. import org.apache.hadoop.mapreduce.Job;  
  12. import org.apache.hadoop.mapreduce.Mapper;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;  
  16. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  17.   
  18. public class TestwithMultipleOutputs extends Configured{  
  19.   
  20.     public static class MapClass extends Mapper<LongWritable, Text, Text, IntWritable> {  
  21.   
  22.         private MultipleOutputs<Text, Text> mos;  
  23.   
  24.         @Override  
  25.         protected void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)throws IOException, InterruptedException {  
  26.             mos = new MultipleOutputs(context);  
  27.         }  
  28.   
  29.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{  
  30.             String line = value.toString();  
  31.             String[] tokens = line.split("\t");  
  32.             if(tokens[0].equals("hadoop")){  
  33.                 mos.write("hadoop"new Text(tokens[0]),new Text(tokens[1]));  
  34.             }else if(tokens[0].equals("hive")){  
  35.                 mos.write("hive"new Text(tokens[0]),new Text(tokens[1]));  
  36.             }else if(tokens[0].equals("hbase")){  
  37.                 mos.write("hbase"new Text(tokens[0]),new Text(tokens[1]));  
  38.             }else if(tokens[0].equals("spark")){  
  39.                 mos.write("spark"new Text(tokens[0]),new Text(tokens[1]));  
  40.             }  
  41.               
  42.         }  
  43.               
  44.         protected void cleanup(Context context) throws IOException,InterruptedException {  
  45.             mos.close();  
  46.         }  
  47.   
  48.     }  
  49.   
  50.     public static void main(String[] args) throws Exception {  
  51.         Configuration conf = new  Configuration();  
  52.         Job job=Job.getInstance(conf, "MultipleOutput");  
  53.         job.setJarByClass(TestwithMultipleOutputs.class);  
  54.         Path in = new Path(args[0]);  
  55.         Path out = new Path(args[1]);  
  56.         FileInputFormat.setInputPaths(job, in);  
  57.         FileOutputFormat.setOutputPath(job, out);  
  58.         job.setMapperClass(MapClass.class);  
  59.         job.setNumReduceTasks(0);  
  60.         MultipleOutputs.addNamedOutput(job,"hadoop",TextOutputFormat.class,Text.class,Text.class);  
  61.         MultipleOutputs.addNamedOutput(job,"hive",TextOutputFormat.class,Text.class,Text.class);  
  62.         MultipleOutputs.addNamedOutput(job,"hbase",TextOutputFormat.class,Text.class,Text.class);  
  63.         MultipleOutputs.addNamedOutput(job,"spark",TextOutputFormat.class,Text.class,Text.class);  
  64.         System.exit(job.waitForCompletion(true)?0:1);  
  65.     }  
  66. }  



[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. 输入数据:
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. more aa.txt   
  2. hadoop  hadoops  
  3. hive    21312q  
  4. hbase   dwfsdf  
  5. spark   sdfsdf  
  6. hbase   werwer  
  7. spark   wefg  
  8. hive    thhdf  
  9. hive    jtyj  
  10. hadoop  trjuh  
  11. hbase   sdfsf  


运行结果目录:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. -rw-r--r--   2 jiuqian supergroup        123 2015-10-13 09:21 libin/input/aa.txt  
  2. Found 6 items  
  3. -rw-r--r--   2 jiuqian supergroup          0 2015-10-13 09:25 libin/input/mul1/_SUCCESS  
  4. -rw-r--r--   2 jiuqian supergroup         28 2015-10-13 09:25 libin/input/mul1/hadoop-m-00000  
  5. -rw-r--r--   2 jiuqian supergroup         38 2015-10-13 09:25 libin/input/mul1/hbase-m-00000  
  6. -rw-r--r--   2 jiuqian supergroup         33 2015-10-13 09:25 libin/input/mul1/hive-m-00000  
  7. -rw-r--r--   2 jiuqian supergroup          0 2015-10-13 09:25 libin/input/mul1/part-m-00000  
  8. -rw-r--r--   2 jiuqian supergroup         24 2015-10-13 09:25 libin/input/mul1/spark-m-00000  
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. </pre><pre name="code" class="java">hdfs dfs -text libin/input/mul1/hadoop-m-00000  
  2. hadoop  hadoops  
  3. hadoop  trjuh  


[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. 15/10/13 09:25:01 INFO client.RMProxy: Connecting to ResourceManager at sh-rslog1/27.115.29.102:8032  
  2. 15/10/13 09:25:02 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.  
  3. 15/10/13 09:25:03 INFO input.FileInputFormat: Total input paths to process : 1  
  4. 15/10/13 09:25:03 INFO mapreduce.JobSubmitter: number of splits:1  
  5. 15/10/13 09:25:03 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1435558921826_10644  
  6. 15/10/13 09:25:03 INFO impl.YarnClientImpl: Submitted application application_1435558921826_10644  
  7. 15/10/13 09:25:03 INFO mapreduce.Job: The url to track the job: http://sh-rslog1:8088/proxy/application_1435558921826_10644/  
  8. 15/10/13 09:25:03 INFO mapreduce.Job: Running job: job_1435558921826_10644  
  9. 15/10/13 09:25:11 INFO mapreduce.Job: Job job_1435558921826_10644 running in uber mode : false  
  10. 15/10/13 09:25:11 INFO mapreduce.Job:  map 0% reduce 0%  
  11. 15/10/13 09:25:18 INFO mapreduce.Job:  map 100% reduce 0%  
  12. 15/10/13 09:25:18 INFO mapreduce.Job: Job job_1435558921826_10644 completed successfully  
  13. 15/10/13 09:25:18 INFO mapreduce.Job: Counters: 30  
  14.         File System Counters  
  15.                 FILE: Number of bytes read=0  
  16.                 FILE: Number of bytes written=107447  
  17.                 FILE: Number of read operations=0  
  18.                 FILE: Number of large read operations=0  
  19.                 FILE: Number of write operations=0  
  20.                 HDFS: Number of bytes read=241  
  21.                 HDFS: Number of bytes written=123  
  22.                 HDFS: Number of read operations=5  
  23.                 HDFS: Number of large read operations=0  
  24.                 HDFS: Number of write operations=6  
  25.         Job Counters   
  26.                 Launched map tasks=1  
  27.                 Data-local map tasks=1  
  28.                 Total time spent by all maps in occupied slots (ms)=4262  
  29.                 Total time spent by all reduces in occupied slots (ms)=0  
  30.                 Total time spent by all map tasks (ms)=4262  
  31.                 Total vcore-seconds taken by all map tasks=4262  
  32.                 Total megabyte-seconds taken by all map tasks=6546432  
  33.         Map-Reduce Framework  
  34.                 Map input records=10  
  35.                 Map output records=0  
  36.                 Input split bytes=118  
  37.                 Spilled Records=0  
  38.                 Failed Shuffles=0  
  39.                 Merged Map outputs=0  
  40.                 GC time elapsed (ms)=41  
  41.                 CPU time spent (ms)=1350  
  42.                 Physical memory (bytes) snapshot=307478528  
  43.                 Virtual memory (bytes) snapshot=1981685760  
  44.                 Total committed heap usage (bytes)=1011351552  
  45.         File Input Format Counters   
  46.                 Bytes Read=123  
  47.         File Output Format Counters   
  48.                 Bytes Written=0 
0 0