IDEA下进行Hadoop HDFS常用API调试(三)

准备

hadoop集群和开发环境在前面必须准备好,调试代码没有将Configuration配置进hadoop的hdfs地址,调试时请手动添加。

同时调试时,请修改main方法到需要调试的方法。

调试

package me.j360.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

/**
 * Created with j360 -> me.j360.hdfs.
 * User: min_xu
 * Date: 2015/4/14
 * Time: 22:27
 * 说明:
 */
public class HdfsApi {
    public static void main(String[] args) throws Exception {
        createFile();
    }

    public static void createFile() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        byte[] buff = "hello hadoop world!\n".getBytes();
        Path dfs = new Path("/test");
        FSDataOutputStream outputStream = hdfs.create(dfs);
        outputStream.write(buff, 0, buff.length);
    }


    public static void createDir() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path dfs = new Path("/TestDir");
        hdfs.mkdirs(dfs);
    }

    public static void rename() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path frpaht = new Path("/test");    //旧的文件名
        Path topath = new Path("/test1");    //新的文件名
        boolean isRename = hdfs.rename(frpaht, topath);
        String result = isRename ? "成功" : "失败";
        System.out.println("文件重命名结果为:" + result);
    }

    public static void deleteFile() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path delef = new Path("/test1");
    }

    public static void checkFile() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path findf = new Path("/test1");
        boolean isExists = hdfs.exists(findf);
        System.out.println("Exist?" + isExists);
    }

    public static void getModifyTime() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path fpath = new Path("/user/hadoop/test/file1.txt");
        FileStatus fileStatus = hdfs.getFileStatus(fpath);
        long modiTime = fileStatus.getModificationTime();
        System.out.println("file1.txt的修改时间是" + modiTime);
    }

    /**
     * 通过"FileSystem.getFileBlockLocation(FileStatus file,long start,long len)
     * 可查找指定文件在HDFS集群上的位置,其中file为文件的完整路径,start和len来标识查找文件的路径。具体实现如下
     */
    public static void getFileLocation() throws Exception {
        Configuration conf = new Configuration();
        FileSystem hdfs = FileSystem.get(conf);
        Path fpath = new Path("/user/hadoop/cygwin");
        FileStatus filestatus = hdfs.getFileStatus(fpath);
        BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
        int blockLen = blkLocations.length;
        for (int i = 0; i < blockLen; i++) {
            String[] hosts = blkLocations[i].getHosts();
            System.out.println("block_" + i + "_location:" + hosts[0]);
        }
    }


    /**
     * 通过"DatanodeInfo.getHostName()"可获取HDFS集群上的所有节点名称
     * */
    public static void getList() throws Exception {
        Configuration conf=new Configuration();
        FileSystem fs=FileSystem.get(conf);
        DistributedFileSystem hdfs = (DistributedFileSystem)fs;
        DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
        for(int i=0;i<dataNodeStats.length;i++){
            System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
        }
    }

    


}


转载于:https://my.oschina.net/smartsales/blog/401591

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值