Hadoop3.0 Java API使用指南

0.开发环境概述

客户端环境

Windows 7 64位
Oracle JDK8 64位
Eclipse4.7

服务器伪分布式安装部署Hadoop3


1.Windows平台下Hadoop客户端运行环境搭建

-下载winutils
-解压缩到任意文件夹下
-新建环境变量HADOOP_HOME
-在环境变量PATH中添加%HADOOP_HOME%\bin

2.建立客户端工程

-新建Maven项目,POM文件如下:
<dependencies>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs-client</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
-拷贝服务器端上的core-site.xmlhdfs-site.xml到maven工程的src文件夹下,开始码代码;

-建立与服务器端Hadoop的连接

    private static Configuration conf = new Configuration();
    private FileSystem hdfs;
    private final static String HDFSUri = "hdfs://192.168.1.111:9001";
    public FileSystem initHDFS() throws URISyntaxException {
        URI uri = new URI(HDFSUri);
        conf.addResource("hdfs-site.xml");
        conf.addResource("core-site.xml");
        try {
            hdfs = FileSystem.get(uri,conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return hdfs;
    }

-检查文件是否存在

    /**
     * 检查文件是否存在
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testCheckFile() throws IllegalArgumentException, IOException {
        Path path = new Path("hdfs://192.168.1.111:9001/user/root/test.txt");
        boolean result = hdfsDao.exists(path);
        System.out.println(result?"文件存在":"文件不存在");
    }

-在HDFS上新建文件夹

    /**
     * 新建文件夹
     * @throws IOException
     */
    @Test
    public void testMkdir() throws IOException {
        Path folderPath = new Path("hdfs://192.168.1.133:9000/user/root/tiffData");
        boolean result = hdfsDao.mkdirs(folderPath);
        System.out.println(result?"添加文件夹成功":"添加文件夹失败");
    }

-删除文件夹/文件

    /**
     * 删除文件夹/文件
     * @throws IOException
     */
    @Test
    public void testDeldir() throws IOException {
        Path folderPath = new Path("hdfs://192.168.1.133:9000/user/root/tiffData");
        boolean result = hdfsDao.delete(folderPath, false);
        System.out.println(result?"删除文件夹成功":"删除文件夹失败");
    }

-获取指定目录下所有文件(忽略目录)

/**
     * 获取指定目录下所有文件(忽略目录)
     * @throws FileNotFoundException
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testGetAllFile() throws FileNotFoundException, IllegalArgumentException, IOException {
        RemoteIterator<LocatedFileStatus> listFiles = hdfsDao.listFiles(new Path("/"), true);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = (LocatedFileStatus) listFiles.next();
            //权限
            FsPermission permission = fileStatus.getPermission();
            //拥有者
            String owner = fileStatus.getOwner();
            //组
            String group = fileStatus.getGroup();
            //文件大小byte
            long len = fileStatus.getLen();
            long modificationTime = fileStatus.getModificationTime();
            Path path = fileStatus.getPath();
            System.out.println("-------------------------------");
            System.out.println("permission:"+permission);
            System.out.println("owner:"+owner);
            System.out.println("group:"+group);
            System.out.println("len:"+len);
            System.out.println("modificationTime:"+sdf.format(new Date(modificationTime)));
            System.out.println("path:"+path);
        }
    }

-从HDFS上拷贝文件到本地

    /**
     * 从HDFS上拷贝文件到本地
     */
    @Test
    public void testDownLoadFile() {
        Path src = new Path("hdfs://192.168.1.133:9000/user/root/test.txt");
        Path des = new Path("D:\\workData\\test.txt");
        try {
            hdfsDao.copyToLocalFile(src, des);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

-本地文件上传

    @Test
    public void testFileUpload() {
        Path src = new Path("D:\\workData\\test1.txt");
        Path des = new Path("hdfs://192.168.1.133:9000/user/root/test1.txt");
        try {
            hdfsDao.copyFromLocalFile(src, des);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 首先,我们在使用前先看看HDFS是什麽?这将有助于我们是以后的运维使用和故障排除思路的获得。 HDFS采用mast...
    W_Bousquet阅读 9,795评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,241评论 19 139
  • 1. Zookeeper介绍: 1.基本介绍: Zookeeper: 为分布式应用提供分布式协作(协调)服务。使用...
    奉先阅读 10,088评论 0 10
  • hadoop是什么?HDFS与MapReduceHive:数据仓库,在HDFS之上,后台执行,帮你执行。faceb...
    Babus阅读 7,296评论 0 5
  • 欢迎Follow我的GitHub, 关注我的简书. 其余参考Android目录. 本文的合集已经编著成书,高级An...
    SpikeKing阅读 13,967评论 2 6

友情链接更多精彩内容