HDFS 文件操作工具类

package test;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
public class Util {
private static final String url="hdfs://master:9000/";
private static final Configuration  conf =new Configuration();
  /**
     * 创建文件夹
     *
     * @param folder 文件夹名
* @throws IOException 
     */
  
  public void mkdir(String folder) throws IOException{
  Path path=new Path(folder);
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  if(!fs.exists(path)){
  fs.mkdirs(path);
  System.out.println("success");
  }
  else
  System.out.println("failure");
  fs.close();
  }
  /**
     * 删除文件夹
     *
     * @param folder 文件夹名
* @throws IOException 
     */
  public void delete(String folder) throws IOException{
  Path path =new Path(folder);
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  if(fs.exists(path)){
  fs.delete(path);
  System.out.println("success");
  }
  else
  System.out.println("failure");
  fs.close();
  }
  /**
     * 重命名文件
     * @param src 源文件名
     * @param dst 目标文件名
* @throws IOException 
     * */
  
  public void rename(String src,String dst) throws IOException{
  Path path1=new Path(src);
  Path path2=new Path(dst);
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  fs.rename(path1, path2);
  fs.close();
  }
  
  /**
     * 列出该路径的文件信息
     *
     * @param folder 文件夹名
* @throws IOException 
     */
  public void ls(String folder) throws IOException{
  Path path =new Path(folder);
  FileSystem fs=path.getFileSystem(conf);
  FileStatus[] list=fs.listStatus(path);
  for(FileStatus l:list){
  System.out.println(l.getPath().getName());
  }
  fs.close();
  }
  
  /**
     * 创建文件
     *
     * @param file    文件名
     * @param content 文件内容
* @throws IOException 
     */
  
  public void createFile(String file,String content) throws IOException{
  Path path=new Path(file);
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  byte[] bytes=content.getBytes();
  FSDataOutputStream os=null;
  try{
  os=fs.create(path);
  os.write(bytes,0,bytes.length);
  }finally{
  os.close();
  }
  fs.close();
  }
  
  /**
     * 复制本地文件到hdfs
     *
     * @param local  本地文件路径
     * @param remote hdfs目标路径
* @throws IOException 
     */
  public void copyFile(String local,String remote) throws IOException{
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  fs.copyFromLocalFile(new Path(local), new Path(remote));
  fs.close();
  }
  
  /**
     * 从hdfs下载文件到本地
     *
     * @param remote hdfs文件路径
     * @param local  本地目标路径
* @throws IOException 
     */
  public void download(String remote,String local) throws IOException{
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  fs.copyToLocalFile(new Path(remote), new Path(local));
  fs.close();
  }
  /**
     * append
     *
     * @param local  本地目标路径
     * * @param remote hdfs文件路径
* @throws IOException 
     */
  public void append(String local,String remote) throws IOException{
  FileSystem fs=FileSystem.get(URI.create(url),conf);
  InputStream in=new BufferedInputStream(new  FileInputStream(local));
  //hdfs-site.xml  settings
    conf.setBoolean( "dfs.support.append", true );                                     
conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");    
conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true"); 

  OutputStream os=null;
  try{
  os=fs.append(new Path(remote));
  IOUtils.copyBytes(in, os, 4096, true);
  }finally{
  IOUtils.closeStream(in);
  IOUtils.closeStream(os);
  }
  fs.close();
  }
  
  /**
     * 查看hdfs文件内容
     *
     * @param remoteFile hdfs文件路径
* @throws IOException 
     */
  
  public void cat(String remoteFile) throws IOException{
  Path path=new Path(remoteFile);
  FileSystem fs=path.getFileSystem(conf);
  FSDataInputStream os=null;
  try{
  os=fs.open(path);
  IOUtils.copyBytes(os, System.out, 4096,false);
  }finally{
  IOUtils.closeStream(os);
  }
  fs.close();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值