使用java api操作Hadoop文件 Robbin

使用java api操作Hadoop文件 Robbin
  1 package cn.hadoop.fs;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.net.URI;
  6 import java.net.URISyntaxException;
  7 
  8 import org.apache.hadoop.conf.Configuration;
  9 import org.apache.hadoop.fs.FSDataOutputStream;
 10 import org.apache.hadoop.fs.FileStatus;
 11 import org.apache.hadoop.fs.FileSystem;
 12 import org.apache.hadoop.fs.Path;
 13 import org.apache.hadoop.io.IOUtils;
 14 import org.junit.Test;
 15 /**
 16  * javaAPI操作hadoop文件
 17  * 
 18  * @author robbin
 19  *
 20  */
 21 public class HdfsTest {
 22 
 23      //创建文件
 24      public static  void createFile(String dst,byte[]contents) throws IOException, InterruptedException, URISyntaxException{
 25          Configuration conf = new Configuration();
 26          FileSystem fs = FileSystem.get(new URI("hdfs://robbin:9000"),conf, "root");
 27          Path dstPath = new Path(dst);
 28          //打开输入流
 29          FSDataOutputStream outputStream = fs.create(dstPath);
 30          outputStream.write(contents);
 31          outputStream.close();
 32          System.out.println("文件创建成功 ");
 33      }
 34      //上传本地文件
 35      public static void uploadFile(String src,String dst) throws IOException, InterruptedException, URISyntaxException{
 36          Configuration conf = new Configuration();
 37          FileSystem fs = FileSystem.get(new URI("hdfs://robbin:9000"),conf, "root");
 38          Path srcPath = new Path(src);
 39          Path dstPath  = new Path(dst);
 40          fs.copyFromLocalFile(false, srcPath, dstPath);
 41          //打印文件路径
 42          System.out.println("Upload to "+conf.get("fs.default.name"));
 43          FileStatus[] listStatus = fs.listStatus(dstPath);
 44          for (FileStatus fileStatus : listStatus)
 45          {
 46             System.out.println(fileStatus.getPath());
 47         }
 48          
 49      }
 50      //删除文件
 51      public static void delete(String filePath) throws IOException{
 52          Configuration conf = new Configuration();
 53          FileSystem fs = FileSystem.get(conf);
 54          Path srcPath = new Path(filePath);
 55          boolean isOk = fs.deleteOnExit(srcPath);
 56          if(isOk){
 57              System.out.println("删除成功");
 58          }else{
 59              System.out.println("删除失败");
 60          }
 61          
 62      }
 63      //创建目录
 64      public static void mkdir(String path) throws IOException, InterruptedException, URISyntaxException{
 65          Configuration conf = new Configuration();
 66          FileSystem fs = FileSystem.get(new URI("hdfs://robbin:9000"),conf, "root");
 67          Path srcPath = new Path(path);
 68          boolean isOk = fs.mkdirs(srcPath);
 69          if(isOk){
 70              System.out.println("创建目录成功");
 71          }else{
 72              System.out.println("创建目录失败");
 73          }
 74      }
 75      //读取文件
 76      public static void readFile(String filePath) throws IOException, InterruptedException, URISyntaxException{
 77          FileSystem fs = FileSystem.get(new URI("hdfs://robbin:9000"), new Configuration(), "root");
 78          Path srcPath = new Path(filePath);
 79          InputStream is = null;
 80          is =fs.open(srcPath);
 81          IOUtils.copyBytes(is,System.out,5096,false);
 82      }
 83      //重命名文件
 84      public static void renameFile(String oldName,String newName) throws IOException, InterruptedException, URISyntaxException{
 85          
 86          FileSystem fs = FileSystem.get(new URI("hdfs://robbin:9000"), new Configuration(), "root");
 87          Path oldPath = new Path(oldName);
 88          Path newPath  = new Path(newName);
 89          boolean isOk = fs.rename(oldPath, newPath);
 90          if(isOk){
 91              System.out.println("修改名字成功");
 92          }else{
 93              System.out.println("修改名字失败");
 94          }
 95          
 96      }
 97      public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
 98           //重命名文件
 99           //renameFile("/robbin/upload.txt","/robbin/robbin2.txt");
100           //测试上传文件
101           //uploadFile("D:\\upload.txt", "/user/");
102           //测试创建文件
103           //byte[] contents =  "hello world 世界你好\n".getBytes();
104           //createFile("/user/robbin3.txt",contents);
105           //测试删除文件
106           //delete("/user/robbin.txt"); //使用相对路径
107           //delete("/user");    //删除目录
108           //测试新建目录
109           //mkdir("user");
110           //测试读取文件
111           readFile("/user/upload.txt");
112               
113      }
114      
115 
116 }
使用java api操作Hadoop文件 Robbin

 

使用java api操作Hadoop文件 Robbin,布布扣,bubuko.com

使用java api操作Hadoop文件 Robbin

上一篇:FreeSWITCH视频会议“标准”解决方案(转载)


下一篇:Javascript实现两张图片的延迟加载