java访问hdfs简单demo

代码如下

package com.lijie.javaapivisit;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

public class Test {

    public static void main(String[] args) throws Exception {
        mkdir();
//      delete();
//      copyFromHdfs();
//      showList();
//      getFileLocal();
//      hdfsInfo();
    }


    public static FileSystem getFileSystem() throws IOException, URISyntaxException{
        Configuration conf = new Configuration();
        URI uri = new URI("hdfs://lijie:9000");
        FileSystem fs = FileSystem.get(uri, conf);
//      FileSystem fs = FileSystem.get(conf);
        return fs;
    }

    public static void mkdir() throws Exception{
        FileSystem fs = getFileSystem();

        fs.mkdirs(new Path("/asdasdasd"));

        fs.close();
    }

    public static void delete() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        fs.delete(new Path("/datatest"), true);
        fs.close();
    }

    public static void copyToHdfs() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        Path path = new Path("F:\\aa.txt");
        Path path1 = new Path("/");
        fs.copyFromLocalFile(path, path1);
        fs.close();
    }

    public static void copyFromHdfs() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        Path path1 = new Path("/lijie/demo.txt");
        Path paht2 = new Path("D://");
        fs.copyToLocalFile(path1, paht2);
        fs.close();
    }

    public static void showList() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        FileStatus[] ls = fs.listStatus(new Path("/"));

        Path[] ps = FileUtil.stat2Paths(ls);
        for (Path path : ps) {
            System.out.println(path);
        }
        fs.close();
    }

    public static void getFileLocal() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        Path path = new Path("/lijie/demo.txt");
        FileStatus fis = fs.getFileStatus(path);
        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fis, 0, fis.getLen());
        int i=0;
        for (BlockLocation blockLocation : fileBlockLocations) {
            i++;
            System.out.println(blockLocation.getHosts()[i-1]+"----"+i);
        }
        fs.close();
    }

    public static void hdfsInfo() throws IOException, URISyntaxException{
        FileSystem fs = getFileSystem();
        DistributedFileSystem hdfs = (DistributedFileSystem) fs;
        DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
        for (DatanodeInfo datanodeInfo : dataNodeStats) {
            System.out.println(datanodeInfo.getHostName());
        }
        fs.close();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值