设为首页 加入收藏

TOP

Hadoop之HDFS文件操作Java实现
2019-03-19 12:37:00 】 浏览:65
Tags:Hadoop HDFS 文件 操作 Java 实现
  • 新建一个Java工程
  • 导入hadoop相关包,具体为hadoop/share/common/lib、hadoop/share/common/hadoop-common-2.6.1.jar、hadoop/share/hdfs/lib以及hadoop/share/hdfs/hadoop-hdfs-2.6.1.jar
public class HdfsClient {
    public static void main(String[] args) throws Exception {

        /**
         * Configuration参数对象到机制:
         *      构造时,加载jar包中到默认配置xx-default.xml
         *      再加载用户配置的xx-site.xml,覆盖默认参数
         *      构造完成之后,还可以conf.set("p","V"),会再次覆盖用户配置文件中的参数
         */
        //new Configuration 会从项目到classpath自动加载core-default.xml、hdfs-default.xml等文件
        Configuration conf = new Configuration();

        //指定本客户端上传文件副本数为2
        conf.set("dfs.replication","2");
        //指定本客户端上传文件到hdfs时切块到规格大小:64M
        conf.set("dfs.blocksize","64M");

        /**
         * 构造一个访问指定hdfs到客户端对象
         * url--hdfs到url
         * conf--客户端特别指定到参数配置
         * root--客户端到用户名
         */
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://172.16.188.138:9000/"), conf, "root");


        //上传一个文件到hdfs中
        fileSystem.copyFromLocalFile(new Path("/Users/yangpeng/Desktop/软件包/apache-hive-1.2.2-bin.tar.gz"),new Path("/"));

        fileSystem.close();
    }

    FileSystem fileSystem =null;
    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();

        //指定本客户端上传文件副本数为2
        conf.set("dfs.replication","2");
        //指定本客户端上传文件到hdfs时切块到规格大小:64M
        conf.set("dfs.blocksize","64M");

        /**
         * 构造一个访问指定hdfs到客户端对象
         * url--hdfs到url
         * conf--客户端特别指定到参数配置
         * root--客户端到用户名
         */
        fileSystem = FileSystem.get(new URI("hdfs://172.16.188.138:9000/"), conf, "root");

    }

    /**
     * 从hdfs下载文件到本地客户端
     */
    @Test
    public void testGet() throws IOException {

        fileSystem.copyToLocalFile(new Path("/a.txt"),new Path("/Users/yangpeng/Desktop"));
        fileSystem.close();
    }


    /**
     * hdfs内部移动文件,修改名称
     */
    @Test
    public void testRename() throws Exception{

        fileSystem.rename(new Path("/apache-hive-1.2.2-bin.tar.gz"),new Path("/user"));
        fileSystem.close();
    }


    /**
     * hdfs中创建文件夹
     */
    @Test
    public void testMkdir() throws Exception{

        fileSystem.mkdirs(new Path("/m"));
        fileSystem.close();
    }

    /**
     * hdfs删除目录或文件夹
     */
    @Test
    public void testDelete() throws Exception{

        fileSystem.delete(new Path("/user/apache-hive-1.2.2-bin.tar.gz"),true);
        fileSystem.close();
    }

    /**
     * 查询hdfs指定目录下的文件信息
     */
    @Test
    public void testLs() throws Exception{
        //只查询文件到信息
        RemoteIterator<LocatedFileStatus> iter = fileSystem.listFiles(new Path("/"), true);
        while (iter.hasNext()){
            LocatedFileStatus status = iter.next();
            System.out.println("块信息:"+ Arrays.toString(status.getBlockLocations()));
            System.out.println("块大小:"+status.getBlockSize());
            System.out.println("文件长度:"+status.getLen());
            System.out.println("副本数量:"+status.getReplication());
            System.out.println("_______________________________________");
        }
        fileSystem.close();
    }


    /**
     * 查询hdfs指定目录下的文件和文件夹信息
     * @throws Exception
     */
    @Test
    public void testLs2() throws Exception{

        FileStatus[] statuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus status: statuses) {
            System.out.println("文件全路径:"+ status.getPath());
            System.out.println(status.isDirectory()"这是文件夹":"这是文件");
            System.out.println("块大小:"+status.getBlockSize());
            System.out.println("文件长度:"+status.getLen());
            System.out.println("副本数量:"+status.getReplication());
            System.out.println("_______________________________________");
        }
    }
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Hadoop官网中英资料文档搜集 下一篇hadoop HDFS命令

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目