Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80855
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: HADOOP

2014-09-03 11:10:43

转自http://www.cnblogs.com/liuling/p/2013-6-17-01.html

点击(此处)折叠或打开

  1. package com.hdfs;

  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;

  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.FSDataOutputStream;
  7. import org.apache.hadoop.fs.FileStatus;
  8. import org.apache.hadoop.fs.FileSystem;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.IOUtils;

  11. public class HdfsTest {
  12.     
  13.     //创建新文件
  14.     public static void createFile(String dst , byte[] contents) throws IOException{
  15.         Configuration conf = new Configuration();
  16.         FileSystem fs = FileSystem.get(conf);
  17.         Path dstPath = new Path(dst); //目标路径
  18.         //打开一个输出流
  19.         FSDataOutputStream outputStream = fs.create(dstPath);
  20.         outputStream.write(contents);
  21.         outputStream.close();
  22.         fs.close();
  23.         System.out.println("文件创建成功!");
  24.     }
  25.     
  26.     //上传本地文件
  27.     public static void uploadFile(String src,String dst) throws IOException{
  28.         Configuration conf = new Configuration();
  29.         FileSystem fs = FileSystem.get(conf);
  30.         Path srcPath = new Path(src); //原路径
  31.         Path dstPath = new Path(dst); //目标路径
  32.         //调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
  33.         fs.copyFromLocalFile(false,srcPath, dstPath);
  34.         
  35.         //打印文件路径
  36.         System.out.println("Upload to "+conf.get("fs.default.name"));
  37.         System.out.println("------------list files------------"+"\n");
  38.         FileStatus [] fileStatus = fs.listStatus(dstPath);
  39.         for (FileStatus file : fileStatus)
  40.         {
  41.             System.out.println(file.getPath());
  42.         }
  43.         fs.close();
  44.     }
  45.     
  46.     //文件重命名
  47.     public static void rename(String oldName,String newName) throws IOException{
  48.         Configuration conf = new Configuration();
  49.         FileSystem fs = FileSystem.get(conf);
  50.         Path oldPath = new Path(oldName);
  51.         Path newPath = new Path(newName);
  52.         boolean isok = fs.rename(oldPath, newPath);
  53.         if(isok){
  54.             System.out.println("rename ok!");
  55.         }else{
  56.             System.out.println("rename failure");
  57.         }
  58.         fs.close();
  59.     }
  60.     //删除文件
  61.     public static void delete(String filePath) throws IOException{
  62.         Configuration conf = new Configuration();
  63.         FileSystem fs = FileSystem.get(conf);
  64.         Path path = new Path(filePath);
  65.         boolean isok = fs.deleteOnExit(path);
  66.         if(isok){
  67.             System.out.println("delete ok!");
  68.         }else{
  69.             System.out.println("delete failure");
  70.         }
  71.         fs.close();
  72.     }
  73.     
  74.     //创建目录
  75.     public static void mkdir(String path) throws IOException{
  76.         Configuration conf = new Configuration();
  77.         FileSystem fs = FileSystem.get(conf);
  78.         Path srcPath = new Path(path);
  79.         boolean isok = fs.mkdirs(srcPath);
  80.         if(isok){
  81.             System.out.println("create dir ok!");
  82.         }else{
  83.             System.out.println("create dir failure");
  84.         }
  85.         fs.close();
  86.     }
  87.     
  88.     //读取文件的内容
  89.     public static void readFile(String filePath) throws IOException{
  90.         Configuration conf = new Configuration();
  91.         FileSystem fs = FileSystem.get(conf);
  92.         Path srcPath = new Path(filePath);
  93.         InputStream in = null;
  94.         try {
  95.             in = fs.open(srcPath);
  96.             IOUtils.copyBytes(in, System.out, 4096, false); //复制到标准输出流
  97.         } finally {
  98.             IOUtils.closeStream(in);
  99.         }
  100.     }
  101.     
  102.     
  103.     public static void main(String[] args) throws IOException {
  104.         //测试上传文件
  105.         //uploadFile("D:\\c.txt", "/user/hadoop/test/");
  106.         //测试创建文件
  107.         /*byte[] contents = "hello world 世界你好\n".getBytes();
  108.         createFile("/user/hadoop/test1/d.txt",contents);*/
  109.         //测试重命名
  110.         //rename("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt");
  111.         //测试删除文件
  112.         //delete("test/dd.txt"); //使用相对路径
  113.         //delete("test1"); //删除目录
  114.         //测试新建目录
  115.         //mkdir("test1");
  116.         //测试读取文件
  117.         readFile("test1/d.txt");
  118.     }

  119. }

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