Chinaunix首页 | 论坛 | 博客
  • 博客访问: 458451
  • 博文数量: 711
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 4200
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-28 14:18
文章分类

全部博文(711)

文章存档

2011年(1)

2008年(710)

我的朋友

分类:

2008-10-28 14:19:42

  在使用hadoop集群时,经常会要用到一个全局性的文件,或是一个hash文件或是一个词表等等。
 
  HadoopDfsReadWriteExample
 
  其实在DFS文件系统中读写文件与其它文件系统本质上是一样的。以下的这个例子就是从一个DFS中读出再写入到另一个DFS中。
 
  Hadoop  FileSystem API这个类就是用来做这个读写用的。
 
  首选要用一个配置对象来产生一个 FileSystem 的对象。
 
  
 
  Configuration conf = new Configuration();
 
  FileSystem fs = FileSystem.get(conf);
 
  Path inFile = new Path(argv[0]);
 
  Path outFile = new Path(argv[1]);
 
  Configuration conf = new Configuration();
 
  FileSystem fs = FileSystem.get(conf);
 
  Path inFile = new Path(argv[0]);
 
  Path outFile = new Path(argv[1]);
 
  先检验一下if (!fs.exists(inFile))       printAndExit("Input file not found");     if (!fs.isFile(inFile))       printAndExit("Input should be a file");     if (fs.exists(outFile))       printAndExit("Output already exists");  if (!fs.exists(inFile))
 
  printAndExit("Input file not found");
 
  if (!fs.isFile(inFile))
 
  printAndExit("Input should be a file");
 
  if (fs.exists(outFile))
 
  printAndExit("Output already exists");
 
  首先是打开一个文件流:FSDataInputStream in = fs.open(inFile);再打开一个输出的文件流:FSDataOutputStream out = fs.create(outFile);从输入流中读入并输出到输出到流中while ((bytesRead = in.read(buffer)) > 0) {       out.write(buffer, 0, bytesRead);     }  while ((bytesRead = in.read(buffer)) > 0) {
 
  out.write(buffer, 0, bytesRead);
 
  }
 
  再把两个流都关闭in.close();     out.close();
 
【责编:landy】

--------------------next---------------------

阅读(353) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~