hadoop第一个程序PutMerge

前一阵子没有继续学习hadoop了,有些生疏,今天重新开始学习,跟着《Hadoop in Action》书上敲下了第一个程序PutMerge。该PutMerge程序旨在将本地文件合并后放入HDFS系统。

其基本步骤为:

  1. 根据用户定义的参数设置本地目录和HDFS的目标文件
     Configuration conf = new Configuration();
     FileSystem hdfs =  FileSystem.get(conf);//HDFS接口的FileSystem对象
     FileSystem local = FileSystem.getLocal(conf);//本地文件系统的FileSystem对象
  2. 提取本地输入目录中的每个文件信息
     Path inputDir = new Path(args[0]);//上传目录
     Path hdfsFile = new Path(args[1]);//目的目录
     FileStatus[] inputFiles = local.listStatus(inputDir);//本地文件列表
  3. 创建输出流写入到HDFS文件系统
    FSDataOutputStream out = hdfs.create(hdfsFile);//HDFS的输出流
  4. 遍历本地目录的每个文件打开输入流读取该文件。
    FSDataInputStream in = local.open(inputFiles[i].getPath());//本地输入流
    byte buffer[] = new  byte[256];
    int bytesRead = 0;
    while((bytesRead = in.read(buffer)) > 0){
      out.write(buffer,0,bytesRead);
    }
代码如下:
  1 import java.io.IOException;
  2 import org.apache.hadoop.conf.Configuration;
  3 import org.apache.hadoop.fs.*;
  4 
  5 public class PutMerge{
  6     public static void main(String[] args)throws IOException{
  7         Configuration conf = new Configuration();
  8         FileSystem hdfs =  FileSystem.get(conf);
  9         FileSystem local = FileSystem.getLocal(conf);
 10 
 11         Path inputDir = new Path(args[0]);
 12         Path hdfsFile = new Path(args[1]);
 13 
 14         try{
 15             FileStatus[] inputFiles = local.listStatus(inputDir);
 16             FSDataOutputStream out = hdfs.create(hdfsFile);
 17 
 18             for(int i=0; i<inputFiles.length; i++)
 19             {
 20                 System.out.println(inputFiles[i].getPath().getName());
 21                 FSDataInputStream in = local.open(inputFiles[i].getPath());
 22                 byte buffer[] = new  byte[256];
 23                 int bytesRead = 0;
 24                 while((bytesRead = in.read(buffer)) > 0){
 25                     out.write(buffer,0,bytesRead);
 26                 }
 27                 in.close();
 28             }
 29             out.close();
 30         }catch(IOException e){
 31             e.printStackTrace();
 32         }
 33     }
 34 }
编译:
javac -classpath hadoop-0.20.2-core.jar:lib/commons-cli-1.2.jar -d playground/classes playground/src/PutMerge.java
查看生成的文件:
ls playground/classes/
打包:
jar -cvf PutMerge.jar PutMerge.class 
HDFS文件系统上建立putmerge文件夹:
bin/hadoop fs -mkdir putmerge
上传本地test文件夹下的a.txt~c.txt到putmerge文件夹下的sum.txt:
 bin/hadoop jar playground/classes/PutMerge.jar PutMerge test/ putmerge/sum.txt
查看结果:
bin/hadoop fs -cat putmerge/sum.txt
查看结果可知本地文件夹确实已经合并并上传了。实验成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop第一个WordCount程序Hadoop的经典入门程序,它可以让你快速了解Hadoop的基本操作。下面是一个简单的WordCount程序的示例代码: Mapper类: ```java public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable ONE = new IntWritable(1); private Text word = new Text(); @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, ONE); } } } ``` Reducer类: ```java public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); @Override 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); } } ``` Driver类: ```java public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在实现这个程序之前,你需要先安装并配置好Hadoop环境。然后,你可以将这个程序保存为一个Java文件并使用Hadoop的命令行工具来运行它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值