你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop HelloWord Examples -对Hadoop FileSystem进行操作 - 基于Java

[日期:2013-08-27] 来源:Linux社区  作者:tspatial_thunder [字体:大 中 小]

我之前对Hadoop的各种文件操作都是基于命令行的,但是进阶后,经常需要直接从java的代码中对HDFS进行修改。今天来练习下。

一个简单的demo,将hdfs的一个文件的内容拷贝到另外hdfs一个文件

相关阅读:

《Hadoop实战》中文版+英文文字版+源码【PDF】 http://www.linuxidc.com/Linux/2012-10/71901.htm

Hadoop HelloWorld Examples - 单表连接 http://www.linuxidc.com/Linux/2013-08/89374.htm

import java.util.*;
import java.io.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.io.IOUtils;

public class ShortestPath {

 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration();
  conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));


  //The two lines' code below is quite useful when debugging Configuration, see reference[3].
  //System.out.println(conf.getRaw("fs.default.name"));
  //System.out.println(conf.toString());
 
  FileSystem fs = FileSystem.get(conf);
 
  FSDataInputStream in= fs.open(new Path(fs.getWorkingDirectory()+"/input/data"));
 
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
 
  FSDataOutputStream out = fs.create(new Path(fs.getWorkingDirectory() +"/testInput/copyData.txt"));
 
  String str = br.readLine();
  while(str!=null)
  {
   out.writeBytes(str);
   out.writeBytes("\n");
   str = br.readLine();
  }
  out.close();
  br.close();
 }
}

以上的拷贝操作也可以通过IOUtils来完成,例如:

import java.util.*;
import java.io.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.io.IOUtils;

public class ShortestPath {

 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration();
  conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
 
  //System.out.println(conf.getRaw("fs.default.name"));
  //System.out.println(conf.toString());
 
  FileSystem fs = FileSystem.get(conf);
 
  FSDataInputStream in= fs.open(new Path(fs.getWorkingDirectory()+"/input/data"));
 
  FSDataOutputStream out = fs.create(new Path(fs.getWorkingDirectory() +"/testInput/copyData.txt"));
   
  IOUtils.copyBytes(in, out, conf);
 
  in.close();
  out.close(); 
 }
}

上面的

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

这行代码让我挺困惑的,我一直以为Configuration是自己在构造函数的时候就自动载入这些默认的core-site.xml之类,但是看来不是。而且调用Configuration的toString()函数后显示它载入了多个core-site.xml,更加困惑。菜鸟对配置文件不熟悉,知道的兄弟讲讲。

System.out.println(conf.toString());

其他更多的文件操作,比如删除等,可以参考reference[1,2],基本大同小异。

Reference

(1) Hadoop: The Definitive Guide【PDF版】  http://www.linuxidc.com/Linux/2012-01/51182.htm

(2)http://eclipse.sys-con.com/node/1287801/mobile

(3)http://www.opensourceconnections.com/2013/03/24/hdfs-debugging-wrong-fs-expected-file-exception/

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

linux
相关资讯       Hadoop HelloWord 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款