Hdfs-java-api 使用
package org.amples;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.URI;
import org.f.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileUtil;
public class FileOperation{
public static void FileSystemCat(String arg1) throws Exception //显示文件内容
{
    String uri =arg1;
        Configuration conf =new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri),conf);
        InputStream in =null;
        try{
            in =fs.open(new Path(uri));
            IOUtils.copyBytes(in,System.out,4096, false);//IOUtils 类,可以在输入流和输出流之间复制数据(这里的inSystem.out4096指复制的缓冲区大小,false指复制后选择自行关闭输入流
        }finally{
            IOUtils.closeStream(in);
        }
}
public static void CopyToHdfs(String srcFile, String dstFile) throws Exception //从本地文件系统复制文件至Hdfs
{
    Configuration config = new Configuration();
    FileSystem hdfs = FileSystem.get(URI.create(dstFile),config);
    Path srcPath = new Path(srcFile);
    Path dstPath = new Path(dstFile);
    pyFromLocalFile(srcPath, dstPath);
}
public static void FileCopyWithProgress(String LocalFile,String HdfsFile) throws Except
ion{
    InputStream in = new BufferedInputStream(new FileInputStream(LocalFile));
    Configuration conf = new Configuration();
    FileSystem fs =FileSystem.get(URI.create(HdfsFile),conf);
    OutputStream out = fs.create(new Path(HdfsFile),new Progressable()
            {
        public void progress()
        {
            System.out.print(".");
        }
            });
    IOUtils.copyBytes(in,out,4096,true);
}
public static void CreateHdfsFile(String FileName) throws Exception //Hdfs创建新文件
{
    Configuration conf =new Configuration();
    FileSystem fs =FileSystem.get(URI.create(FileName),conf);
    Path path =new Path(FileName);
   
    FSDataOutputStream outputStream = fs.create(path);
   
}
public static boolean RenameHdfsFile(String FromFileName,String ToFileName) throws Exception//重命名Hdfs的文件
{
    Configuration conf =new Configuration();
    FileSystem fs = FileSystem.get(URI.create(FromFileName),conf);
      Path fromPath = new Path(FromFileName);
      Path toPath = new Path(ToFileName);
      boolean isRenamed = fs.rename(fromPath, toPath);
      return isRenamed;
}
public static boolean DeleteFile(String fileName) throws Exception //Hdfs删除文件
{
    Configuration config = new Configuration();
    FileSystem hdfs =FileSystem.get(URI.create(fileName),config);
      Path path = new Path(fileName);
      boolean isDeleted = hdfs.delete(path, true);
      return isDeleted;
}
public static boolean IfFileExits(String FileName)throws Exception //判断文件是否存在于Hdfs
{
    Configuration conf = new Configuration();
      FileSystem fs = FileSystem.get(URI.create(FileName),conf);
      Path path = new Path(FileName);
      boolean isExists = fs.exists(path);
      return isExists;
}
public static void GetLocation(String FileName)throws Exception//查看想看的文件存放在哪几个节点当中,未完成
{
    Configuration conf = new Configuration();
      FileSystem fs = FileSystem.get(URI.create(FileName),conf);
      Path path = new Path(FileName);
      FileStatus fileStatus = fs.getFileStatus(path);
   
      BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus , 0, Len());
       
      int blkCount = blkLocations.length;
      for (int i=0; i < blkCount; i++) {
        String[] hosts = blkLocations[i].getHosts();
        //System.out.print(blkLocations[i].getHosts());
        System.out.print(hosts);
        // Do something with the block hosts
      }
}
public static void ListStatus(String args[]) throws Exception //显示Hdfs一组路径的文件信息
{
    String uri = args[0];
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri),conf);
    Path[] paths = new Path[args.length];
            for(int i=0;i<paths.length;i++)
            {
                paths[i] = new Path(args[i]);
            }
    FileStatus[] status =fs.listStatus(paths);java switch case string
    Path[] listedPaths = FileUtil.stat2Paths(status);
    for(Path p: listedPaths){
        System.out.println(p);
    }
   
}
    public static void main(String[] args) throws Exception {
        boolean flag=true;
        while(true)
        {
       
        System.out.println("Please input your choise");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。