HDFS API常用的方法及使用

2023-05-16

文章目录

  • POM依赖
  • HDFS API
    • 上传本地数据到HDFS系统
    • 删除指定路径下的文件
    • 创建文件夹及文件,并向文件中写入数据
    • 获取指定文件的日期
    • 重命名目录
    • 获取指定路径所有的文件

POM依赖

<repositories>
    <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.apache.Hadoop</groupId>
        <artifactId>Hadoop-client</artifactId>
        <version>2.6.0-mr1-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.Hadoop</groupId>
        <artifactId>Hadoop-common</artifactId>
        <version>2.6.0-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.Hadoop</groupId>
        <artifactId>Hadoop-hdfs</artifactId>
        <version>2.6.0-cdh5.14.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.Hadoop</groupId>
        <artifactId>Hadoop-mapreduce-client-core</artifactId>
        <version>2.6.0-cdh5.14.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <!--    <verbal>true</verbal>-->
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>true</minimizeJar>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      <!--  <plugin>
            <artifactId>maven-assembly-plugin </artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>cn.itcast.Hadoop.db.DBToHdfs2</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>-->
    </plugins>
</build>

HDFS API

上传本地数据到HDFS系统

copyFromLocalFile()

	/**
     * 上传本地数据到HDFS系统
     */
    @Test
    public void copyFromLocalFile() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21/"), configuration);
        fileSystem.copyFromLocalFile(new Path("D://123.txt"), new Path("/"));

        fileSystem.close();
    }

删除指定路径下的文件

delete()

    /**
     * 删除指定路径下的文件
     */
    public void deleteFile() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21/"), configuration);
        Path rmPath = new Path("/abc/a.txt");
        /**
         * recursive 是否进行递归删除该文件夹下所有的文件?
         * true:是
         * false:否
         */
        boolean delete = fileSystem.delete(rmPath, true);

        System.out.println(delete ? "文件删除成功" : "文件删除失败");
        fileSystem.close();
    }

创建文件夹及文件,并向文件中写入数据

mkdir()
create()

    /**
     * 创建文件夹/abc和a.txt文件,并向a.txt文件中写入数据
     */
    public void createData() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21"), configuration);
        FSDataOutputStream fsDataOutputStream = null;
        // 检查要创建的文件夹是否已在文件系统中存在
        Path dir = new Path("/abc");
        if (!fileSystem.exists(dir)) {
            fileSystem.mkdirs(dir);
        }

        // 检查要创建的文件是否已在文件系统中存在
        Path file = new Path(dir, "/a.txt");
        if (!fileSystem.exists(file)) {
            fsDataOutputStream = fileSystem.create(file, true, 4096, new Progressable() {
                @Override
                public void progress() {
                    System.out.println("显示创建进度(可有可无)");
                }
            });

        }
        
		byte[] bytes = "向文件中写入的数据内容".getBytes();

        fsDataOutputStream.write(bytes, 0, bytes.length);
        fsDataOutputStream.flush();
        fsDataOutputStream.close();
        fileSystem.close();
    }

获取指定文件的日期

getFileStatus()
getModificationTime()

    /**
     * 获取指定文件的日期
     */
    public void getTime() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21/"), configuration);
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/a.txt"));
        long modificationTime = fileStatus.getModificationTime();

        System.out.println(modificationTime);
        fileSystem.close();
    }

重命名目录

rename()

	/**
     * 重命名目录名
     */
    public void rename() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21/"), configuration);
        Path oldPath = new Path("/abc");
        Path newPath = new Path("/123");

        boolean rename = fileSystem.rename(oldPath, newPath);
        System.out.println(rename ? "重命名成功" : "重命名失败");
        fileSystem.close();
    }

获取指定路径所有的文件

listStatus()

	/**
     * 获取指定路径所有的文件(HDFS文件系统中根路径的所有文件)
     */
    public void listStatus() throws Exception {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.100.21/"), configuration);
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath().toString());
        }
        fileSystem.close();
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HDFS API常用的方法及使用 的相关文章

  • ECMAScript 6 入门-变量的解构赋值

    数组的解构赋值 原文链接 详细的内容我就不说了 xff0c 大家自己打开链接自己看看吧 xff0c 我再怎么说也是拾人牙慧 xff1b 我只是摘抄一段放入博客内吧 xff1b 用途 变量的解构赋值用途很多 xff08 1 xff09 交换变
  • ECMAScript 6 入门-字符串的扩展

    codePointAt charAt 方法可返回指定位置的字符 用法 xff1a stringObject charAt index xff1b 参数 xff1a index 必需 表示字符串中某个位置的数字 xff0c 即字符在字符串中的
  • ECMAScript 6 入门-正则的扩展

    原文http es6 ruanyifeng com docs regex 正则的扩展 字符串的正则方法 字符串对象共有 4 个方法 xff0c 可以使用正则表达式 xff1a match replace search 和split matc
  • ECMAScript 6 入门-函数的扩展

    原文链接http es6 ruanyifeng com docs function 函数参数的默认值 ES6 允许为函数的参数设置默认值 xff0c 即直接写在参数定义的后面 function span class hljs keyword
  • CentOS 云镜像 CloudImage 版本对应下载

    因为要在OpenStack内创建CentOS 7 4的qcow2格式镜像 xff0c 所以去http cloud centos org centos 7 images 上下载 发现没有显式的标明CentOS 版本 xff1a 不过 xff0
  • ECMAScript 6 入门-数组的扩展

    原文http es6 ruanyifeng com docs array Array from 扩展运算符 扩展运算符 xff08 spread xff09 是三个点 xff08 xff09 它好比 rest 参数的逆运算 xff0c 将一
  • 使用递归-实现二维矩阵数组

    废话不多说 xff0c 直接上代码 xff1a 函数递归 把 1 2 3 4 5 6 得到 1 3 5 1 3 6 1 4 5 arr 需要递归的数组 1 2 3 4 5 6 function diguiArrData arr if arr
  • 微信小程序使用图标

    微信小程序使用阿里巴巴图标 因为业务需求项目上要使用一些图标 xff0c 下面简单的介绍一下使用方法 首先登陆自己的iconfont的账号 阿里巴巴矢量图标库在图标管理我的项目中查看 下载并保存文件iconfont css xff1b 把文
  • 微信小程序自定义组件-事件

    文档地址https developers weixin qq com miniprogram dev framework custom component 页面 page span class hljs preprocessor wxml
  • 微信小程序scroll-view左右滚动

    官方只有一个HTML的代码演示 xff0c 而css没有 xff1b 自己根据官方的HTML代码 xff0c 尝试几个方案 xff0c 都GG了 xff1b 最后实现成功 xff1b 因为需要用到scroll top的特性 xff1b 所以
  • 微信小程序去除button边框

    因为一些业务逻辑需要去除button边框 xff1b 这里试了几种去除的方式 xff1b 发现只有 span class hljs tag button span span class hljs pseudo after span span
  • 微信小程序-经纬度逆解析详细地址

    业务需求需要将经纬度解析为详细地址 xff0c 去年使用的是百度地图 xff0c 今年发现腾旭地图也更新了微信小程序的地图的接口 xff1b 1 http lbs qq com guides startup html 申请腾讯地图的key
  • 微信小程序-上传图片转base64

    最近业务需要 xff1a 将图片转为base64格式 so xff1b 百度n多资料最后写成 xff0c 还是有一点小bug就是生成的图片不太清晰 xff1b 文章最后还有代码的下载地址 参考资料 xff1a 微信小程序文档 wx canv
  • 数据结构和算法

  • 马原笔记-第三章

    实践与认识及其发展规律 实践与认识实践认识从实践到认识从认识到实践实践与认识的辩证运动及其规律 真理与价值真理真理的客观性真理的绝对性和相对性及其辩证关系真理的检验标准 价值真理和价值在实践中的辩证统一关系 认识世界和改造世界认识世界和改造
  • Debian9系统上 基于Qt5.12.1 开发的QML界面程序 无法输入中文

    问题背景 xff1a 使用QT的QML开发出来的客户端程序无法在TextEdit框输入中文 xff0c 当鼠标聚焦到输入光标后 xff0c 压根切换不了中文输入法 网上找了一大堆 xff0c 尝试了各种方案 xff0c 终于解决了问题 至此
  • react面试题

    请写一个函数 xff0c 获取当前url的参数值 xff0c 必须支持以下功能 xff1a 如果传了参数名则获取此参数名对应的参数值 xff0c 如果不传 xff0c 则获取所有参数 span class hljs comment 比如 x
  • webpack4+react+antd从零搭建React脚手架(三)-路由搭建

    react router 还是 react router dom xff1f 在 React 的使用中 xff0c 我们一般要引入两个包 xff0c react 和 react dom xff0c 那么 react router 和 rea
  • 2018秋季面试总结

    最近几天一直在找工作 xff0c 今天把面试遇到的问题一个一个的整理下来 基础问题 xff08 css html js xff09 算法问题 xff1a 使用一个for循环 xff0c 找出一个数组里面第二大的值 当时没有想出来 xff0c
  • js实现数组去重-ES6实现

    方法一 xff1a function unique arr const res 61 new Map return arr filter a 61 gt res has a amp amp res set a 1 就这么短 xff0c 就可

随机推荐

  • vue 报错 Avoid mutating a prop directly since the value will be overwritten whenever the parent compo

    Vue报错 xff1a Avoid mutating a prop directly since the value will be overwritten whenever the parent component re renders
  • 老男孩全栈Python开发

    地址 视频地址 p34 28 05
  • js实现文件分片上传

    span class token operator lt span span class token operator span span class token constant DOCTYPE span html span class
  • python实战-VScode环境搭建

    环境搭建 xff08 VScode xff09 以前用的是pycharm的专业版 xff0c 后来搞得破解不了了 xff0c 花了几个小时研究了一下怎么用VScode来搭建python开发的环境 开始之前 xff1a 装好python环境确
  • nginx部署多个vue项目

    上一篇已经介绍了然后配置web项目 xff1b 今天由于公司需求 xff0c 需要在同一域名端口下 xff0c 部署两个项目 xff1b 今天花了一上午终于弄好了 xff0c 选择赶紧做一个笔记 如何连接阿里云服务器就不在这里说了 xff0
  • 汇编语言

    基础知识 1 1机器语言 计算机的机器指令是一列二进制数字 计算机将之为转变为一列高低电平 xff0c 以使计算机的电子器件受到驱动 xff0c 进行运算 1 2 汇编语言的产生 程序员 汇编指令 编译器 机器码 计算机 1 3 汇编语言的
  • Bootloader概述

    Bootloader概述 分类 xff1a 嵌入式 Bootloader概述 Bootloader的含义 PC机上 windows linux 引导过程简介 嵌入式Bootloader 介绍与分析 嵌入式bootloader 介绍 Boot
  • vue SEO优化

    第一 xff0c ssr vue官方文档给出的服务器渲染方案 xff0c 这是一套完整的构建vue服务端渲染应用的指南 xff0c 具体参考https cn vuejs org v2 guide ssr html 第二 xff0c vue
  • 阿里云ubuntu 16.0402 安装mysql

    参考文献 阿里云服务器Ubuntu 16 04 3安装mysql如何远程连接阿里云上mysql数据库 出错10061 阿里云ubuntu 16 0402 安装mysql 前提 xff1a 装上Xshell xff0c winscp等远程工具
  • 阿里云开发工具汇总

    阿里云Ubuntu16 04 安装pip span class token function wget span https bootstrap pypa io get pip py python get pip py 阿里云Ubuntu1
  • 基于机器学习的异常检测与分析技术

    传统的运维方式在监控 问题发现 告警以及故障处理等各个环节均存在明显不足 xff0c 需要大量依赖人的经验 xff0c 在数据采集 异常诊断分析 故障处理的效率等方面有待提高 本关键技术面对传统运维故障处理效率低 问题定位不准确 人力成本高
  • Mac系统 消除系统更新提示的小红点

    1 首先在系统偏好设置中将软件更新中 自动保持我的Mac运行最新版本勾掉 xff1b 2 打开终端输入如下指令 xff1a defaults span class token function write span com apple sy
  • VSCode远程连接Ubuntu问题1:The remote host may not meet VS Code Server‘s prerequisites for glibc and libstd

    VScode配置远程免密连接Ubuntu16 0 4 xff0c 一直以来使用正常 xff0c 今天使用VSCode报错 xff0c 错误具体信息如下 xff1a The remote host may not meet VS Code S
  • wsl上的ubuntu执行docker和docker-compose失败问题排查

    起因 中秋放假没有关机 xff0c 肥来之后wsl上的docker compose 和docker报错啦 表现 docker compose ps报错 root 64 YK PC 00036 docker compose ps bash m
  • GVIM设置默认字体、背景颜色以及行号

    GVIM设置默认字体 背景颜色以及行号 xff1a 1 打开terminal去到根目录下 2 输入gvim vimrc打开空白文件 3 依次输入 set nu set guifont 61 Monospace 15 colorscheme
  • 2019-3-27毕设之python小白第三课---调用API接口

    CN DBpedia的接口调用 Introduction to APIs Knowledge Works以REST API的方式对外提供服务 一站式解决您的语义分析诉求 从多个维度为您的应用场景提供海量背景知识 API包括CN dbpedi
  • Python 使用xlrd模块获取Excel中超链接的URL并下载到本地

    本文介绍了xlrd模块如何操作Excel 目录 一 xlrd模块介绍二 使用步骤1 引入库2 示例代码 一 xlrd模块介绍 1 xlrd是读取Excel表格数据的模块 2 高版本的xlrd目前去除了对xlsx格式的支持 xff0c 支持x
  • 查看树莓派实时温度

    打开终端 xff0c 使用指令查看CPU温度 xff0c 依次输入以下指令 xff1a 1 cd sys class thermal thermal zone0 cat temp 返回值 除以1000 51540 返回值除以1000为当前C
  • 人脸识别概述[L-softmax | 人脸验证 | DeepFace | FaceNet | Triplet loss]

    文章目录 思维导图疑问用softmax分类做人脸识别 xff0c 怎么应用呀 概念区分主流方法损失函数Softmax loss基于欧式距离的损失函数对比损失 xff08 Contrastive Loss xff09 三元组损失 xff08
  • HDFS API常用的方法及使用

    文章目录 POM依赖HDFS API上传本地数据到HDFS系统删除指定路径下的文件创建文件夹及文件 xff0c 并向文件中写入数据获取指定文件的日期重命名目录获取指定路径所有的文件 POM依赖 span class token tag sp