hdfs java api操作

/**
     * 创建一个新文件(此程序可以升级putMerge功能)
     * @param filename
     * @param content
     * @throws IOException
     */
    public static void newFile(String filename, byte[] content) throws IOException{
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(conf);
 
        Path filePath = new Path(filename);
        FSDataOutputStream outputStream = fileSystem.create(filePath);
 
        outputStream.write(content);
        outputStream.close();
        fileSystem.close();
        System.out.println("创建文件成功!");
    }
 
    /**
     * 上传文件
     * @param localPath
     * @param hdfsPath
     * @throws IOException
     */
    public static void uploadFile(String localPath, String hdfsPath) throws IOException{
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
 
        Path src = new Path(localPath);
        Path dst = new Path(hdfsPath);
        //fileSystem.copyFromLocalFile(src, dst);
        //第一个false,是否删除原文件;第二个false,是否overwrite。
        fileSystem.copyFromLocalFile(false, false, src, dst);
        fileSystem.close();
    }
 
    /**
     * 重命名
     * @param oldName
     * @param newName
     * @throws IOException
     */
    public static void rename(String oldName,String newName) throws IOException{
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
 
        Path oldPath = new Path(oldName);
        Path newPath = new Path(newName);
 
        boolean isSuccess = fileSystem.rename(oldPath, newPath);
        if (isSuccess) {
            System.err.println("成功");
        }
        else {
            System.err.println("失败");
        }
        fileSystem.close();
    }
 
    /**
     * 删除文件操作
     * @param filePath
     * @throws IOException
     */
    public static void delete(String filePath) throws IOException{
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
        Path hdfspath = new Path(filePath);
        fileSystem.deleteOnExit(hdfspath);
        fileSystem.close();
    }
 
    /**
     * 创建目录
     * @throws IOException
     */
    public static void mkdir(String dirPath) throws IOException{
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
        Path path = new Path(dirPath);
        boolean isSuccess = fileSystem.mkdirs(path);
        if (isSuccess) {
            System.err.println("创建目录成功!");
        }else {
            System.err.println("创建目录失败!");
        }
        fileSystem.close();
    }
 
    public static void readFile(String filePath) throws IOException{
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(configuration);
        Path path = new Path(filePath);
        InputStream in = null;
        in = fs.open(path);
        IOUtils.copyBytes(in, System.out, 4096);
        IOUtils.closeStream(in);
        fs.close();//TODO: ?
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值