ITPub博客

首页 > 大数据 > Hadoop > java操作Hdfs

java操作Hdfs

Hadoop 作者:xidian2002 时间:2014-03-14 11:42:04 0 删除 编辑

package hdfs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


public class App2 {
 public static final String HDFS_PATH="hdfs://192.168.1.115:9000/hello";
 public static final String DIR_PATH="/d00";
 public static final String FILE_PATH="/d00/f300";
 public static void main(String[] args) throws Exception {
 final FileSystem filesystem=FileSystem.get(new URI(HDFS_PATH),new Configuration());

 //创建文件夹
 makeDirectory(filesystem);
 //上传文件
 uploadData(filesystem);
 //下载文件
 downloadData(filesystem);
// //删除文件(夹)
 deletefile(filesystem);
 }
 private static void deletefile(final FileSystem filesystem)
   throws IOException {
  filesystem.delete(new Path(FILE_PATH), true);
 }
 private static void uploadData(final FileSystem filesystem)
   throws IOException, FileNotFoundException {
  final FSDataOutputStream out = filesystem.create(new Path(FILE_PATH));
  FileInputStream in =new FileInputStream("c:/log.txt");
  IOUtils.copyBytes(in, out, 1024,true);
 }
 private static void makeDirectory(final FileSystem filesystem)
   throws IOException {
  filesystem.mkdirs(new Path(DIR_PATH));
 }
 private static void downloadData(final FileSystem filesystem)
   throws IOException {
  final FSDataInputStream in1 =filesystem.open(new Path(FILE_PATH));
  IOUtils.copyBytes(in1, System.out, 1024, true);
 }

}

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7851628/viewspace-1119715/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

最新文章