通过FileSystem API对集群操作(第一回)

通过FileSystem API访问BlockLocations块位置信息
-----------------------------------------------------------------------------------------------
    /**
     * 通过FileSystem API访问BlockLocations块位置信息
     * @throws IOException
     */
    @Test
    public void BlockLByFS() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path file = new Path("/spaceQuota/hello.txt");
        FileStatus fst = fs.getFileStatus(file);
        BlockLocation[] blocklocations = fs.getFileBlockLocations(fst, 8, 30);
//        BlockLocation[] blocklocations = fs.getFileBlockLocations(fst, 0, 30);
        for (BlockLocation blockLocation : blocklocations) {
            String[] hosts = blockLocation.getHosts();
            for (String host : hosts) {
                System.out.println(host);
            }
            String[] names = blockLocation.getNames();
            for (String name : names) {
                System.out.println(name);
            }
            String[] topologs = blockLocation.getTopologyPaths();
            for (String topolog : topologs) {
                System.out.println(topolog);
            }
        }
    }
    

通过FileSystem API访问Datanode信息
----------------------------------------------------------------------------------------------
    /**
     * 通过FileSystem API访问Datanode信息
     * @throws IOException
     */
    @Test
    public void datanodeLByFS() throws IOException{
        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "hyxy"); 
//        conf.set("dfs.permissions.enabled", "false");
        FileSystem fs = FileSystem.get(conf);
        DistributedFileSystem dfs = (DistributedFileSystem)fs;
        DatanodeInfo[] dni = dfs.getDataNodeStats();
        for (DatanodeInfo datanodeInfo : dni) {
            System.out.println("Capacity:"+datanodeInfo.getCapacity());
            System.out.println("Remaining:"+datanodeInfo.getRemaining());
            System.out.println("DatanodeReport:"+datanodeInfo.getDatanodeReport());
            System.out.println("------------------------------------------");
        }
    }


从hadoop URL读取数据
------------------------------------------
    static{
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
    }
    /**
     * 通过java.net.URL类访问读取HDFS数据
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        URL _url  =new URL("hdfs://master1:9000/spaceQuota/text.txt");
        InputStream in = _url.openStream();
        IOUtils.copyBytes(in, System.out, 4096,false);
        IOUtils.closeStream(in);
    }

通过FileSystem API访问读取HDFS数据
---------------------------------------------
    /**
     * 通过FileSystem API访问读取HDFS数据
     * $>hadoop fs -cat /spaceQuota/text.txt
     * @throws IOException
     */
    @Test
    public void readByFS() throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        String _path = "hdfs://master1:9000/spaceQuota/text.txt";
        FSDataInputStream  in = fs.open(new Path(_path));
        IOUtils.copyBytes(in, System.out, 4096, true);
    }
 
通过FileSystem API创建文件夹
--------------------------------------------------------------
    /**
     * 通过FileSystem API创建文件夹
     * $>hadoop fs -mkdir /mkdir_byAPI
     * 注意:修改集群权限:$>hadoop fs -chmod -R a+w /
     */
    @Test
    public void mkdirByAPI()throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path _path = new Path("/mkdir_byAPI");
        fs.mkdirs(_path);
        fs.close();
    }

FileStatus对象,获取文件的相关属性
-----------------------------------------------------------
    /**
     * 通过FileSystem API获取一些相关参数,有一些过期的方法,如何采用现有的
     */
    @Test
    public void getPar()throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        FileStatus fstu = fs.getFileStatus(new Path("/spaceQuota/text.txt"));
        long _blocksize = fstu.getBlockSize();
        System.out.println("getAccessTime="+fstu.getAccessTime());
        System.out.println("blockszie="+_blocksize);
        System.out.println("getGroup="+fstu.getGroup());
        System.out.println("getLen="+fstu.getLen());
        System.out.println("getModificationTime="+fstu.getModificationTime());
        System.out.println("getOwner="+fstu.getOwner());
        System.out.println("getReplication="+fstu.getReplication());
        System.out.println("getPath="+fstu.getPath());
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值