Java大数据开发:Hadoop(8)-java操作HDFS

news/2024/5/20 5:31:04 标签: 大数据, hdfs, hadoop, spark

在上一节的学习中,我们认识了HDFS的结构,知道了HDFS的优点:适合大数据处理,无论是数据规模还是文件规模。当然也有他的缺点:不适合低延时数据访问,比如毫秒级的数据存储,那做不到,不适合对大量小文件进行存储,因为会占用namenode大量的内存存储,一个文件也只有一个写,不允许多个线程同时写。接下来,我们就用JavaAPI操作一下HDFS吧。

hdfs客户端环境准备

1、根据自己电脑的操作系统拷贝对应的编译后的hadoop jar包到非中文路径

2、配置HADOOP_HOME环境变量

3、创建一个Maven工程HdfsClientDemo

4、导入相应的依赖坐标

    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>RELEASE</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-core</artifactId>            <version>2.8.2</version>        </dependency>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-common</artifactId>            <version>2.7.2</version>        </dependency>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-client</artifactId>            <version>2.7.2</version>        </dependency>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-hdfs</artifactId>            <version>2.7.2</version>        </dependency>        <dependency>            <groupId>jdk.tools</groupId>            <artifactId>jdk.tools</artifactId>            <version>1.8</version>            <scope>system</scope>            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>        </dependency>    </dependencies>

 

5、添加日志,需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.FileAppenderlog4j.appender.logfile.File=target/spring.loglog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 

6、创建一个类,编写如下代码实现从hdfs上创建一个目录

public class HDFSClient {    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {        // 1 获取文件系统        Configuration configuration = new Configuration();        // 配置在集群上运行        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 创建目录        fs.mkdirs(new Path("/0331/test/creatDir"));
        // 3 关闭资源        fs.close();    }}

 

7、运行http://hadoop100:50070/explorer.html#/,查看目录创建情况,如下

 

 

文件上传

把d盘下 test.txt 文件上传到集群根目录下

 

1、编写代码

@Test    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统        Configuration configuration = new Configuration();        // 设置副本数,默认为3        configuration.set("dfs.replication", "2");        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 上传文件        fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("/test.txt"));
        // 3 关闭资源        fs.close();
        System.out.println("完成");    }

 

2、刷新页面,查看上传情况

 

 

3、参数优先级

 

这里指的是一些参数配置,比如代码里的副本数,这个参数如果不设置,用的配置是服务器的配置,这些参数我们可以在代码修改,也可以在classpath下创建配置文件指定,但配置是有优先级的:(1)代码中设置的值(最先生效) >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置。如果你对大数据开发感兴趣,想系统学习大数据的话,可以加入大数据技术学习交流扣扣君羊:522189307

 

文件下载

把集群根目录下的 test.txt 下载到d盘根目录

 

1、代码编写

@Test    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 执行下载操作        // boolean delSrc 指是否将原文件删除        // Path src 指要下载的文件路径        // Path dst 指将文件下载到的路径        // boolean useRawLocalFileSystem 是否开启文件校验        fs.copyToLocalFile(false, new Path("/test.txt"), new Path("d:/a.txt"), true);
        // 3 关闭资源        fs.close();    }

 

2、查看d盘根目录下生成的文件

 

文件夹删除

删除0331文件夹(0331是自定义的,作者是取自今天日期)

 

1、代码编写

@Test    public void testDelete() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 执行删除        fs.delete(new Path("/0331/"), true);
        // 3 关闭资源        fs.close();    }

 

2、查看hadoop管理页面,是否删除了文件夹。

 

文件更名

把test.txt文件名称改为b.txt

1、代码编写

  @Test    public void testRename() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 修改文件名称        fs.rename(new Path("/test.txt"), new Path("/b.txt"));
        // 3 关闭资源        fs.close();    }

2、查看hadoop管理页面,是否更名成功。

查看文件详情

查看文件名称,权限,长度,块信息

1、代码编写

    @Test    public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
        // 1获取文件系统        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 获取文件详情        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {            LocatedFileStatus status = listFiles.next();
            // 输出详情            // 文件名称            System.out.println(status.getPath().getName());            // 长度            System.out.println(status.getLen());            // 权限            System.out.println(status.getPermission());            // 分组            System.out.println(status.getGroup());
            // 获取存储的块信息            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                // 获取块存储的主机节点                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {                    System.out.println(host);                }            }
            System.out.println("-----------分割线----------");        }
        // 3 关闭资源        fs.close();    }

 

2、查看控制台日志打印

b.txt9rw-r--r--supergrouphadoop101hadoop102-----------分割线----------

 

文件和文件夹的判断

判断是文件还是文件夹

 

1、代码编写

    @Test    public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件配置信息        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
        // 2 判断是文件还是文件夹        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            // 如果是文件            if (fileStatus.isFile()) {                System.out.println("f:" + fileStatus.getPath().getName());            } else {                System.out.println("d:" + fileStatus.getPath().getName());            }        }
        // 3 关闭资源        fs.close();    }

2、查看控制台打印

d:0331    //文件夹f:b.txt   //文件

以上是文件与hdfs的交互,文件的来源可以是从mysql数据库中获取的数据,也可以是从消息中间件获取的数据。


http://www.niftyadmin.cn/n/1574998.html

相关文章

Java多线程学习(五)线程间通信知识点补充

系列文章传送门: Java多线程学习&#xff08;一&#xff09;Java多线程入门 Java多线程学习&#xff08;二&#xff09;synchronized关键字&#xff08;1&#xff09; Java多线程学习&#xff08;二&#xff09;synchronized关键字&#xff08;2&#xff09; Java多线程学习&am…

怎样成为一名真正的数据科学家?这10本书就是答案

导读&#xff1a;社交、出行、办公、购物、娱乐……一个生活在2020年的人&#xff0c;每天要产生多少数据&#xff1f;这些数据将怎样改变我们的生活、工作和思维方式&#xff1f;将创造哪些价值&#xff1f;这些价值又该怎样挖掘&#xff1f; 数据科学家被《哈佛商业评论》称…

关于高可用负载均衡的探索

2019独角兽企业重金招聘Python工程师标准>>> 本文于3月22日晚由张新峰&#xff0c;杭州爱医康架构师技术分享整理而成。本次分享介绍了如何使用负载均衡达到一个对用户友好&#xff08;稳定无感&#xff09;、对运维友好&#xff08;傻瓜高效&#xff09;、对架构友…

玩转大数据开发工具--上下全篇

为了降低大数据应用开发的门槛&#xff0c;简化开发过程&#xff0c;星环随Transwarp Data Hub 5.0开发出了大数据开发套件Transwarp Studio。Studio由一套PaaS产品构成&#xff0c;提供从提取、存储、计算、展示的全链路大数据开发服务&#xff0c;全面覆盖大数据开发流水线上…

kali wifi (not complete!)

1. apt-get install isc-dhcp-server -y 2. airmon-ng check kill airmon-ng start wlan0airbase-ng -e FreeWiFi -c 6 wlan0mon 转载于:https://www.cnblogs.com/yugengde/p/8650241.html

大数据开发实战系列之Spark电商平台

源于企业级电商网站的大数据统计分析平台&#xff0c;该平台以 Spark 框架为核心&#xff0c;对电商网站的日志进行离线和实时分析。 该大数据分析平台对电商网站的各种用户行为&#xff08;访问行为、购物行为、广告点击行为等&#xff09;进行分析&#xff0c;根据平台统计出…

netstat、Linux下抓包

1、netstat 命令 netstat 是在内核中访问网络连接状态及其相关信息的程序&#xff0c;它能提供TCP连接&#xff0c;TCP和UDP监听&#xff0c;进程内存管理的相关报告。netstat 是控制台命令,是一个监控TCP/IP网络的非常有用的工具&#xff0c;它可以显示路由表、实际的网络连接…

想从事数据科学,编码技能够格了吗?

先来看看这样一个故事&#xff1a; “那是周五的晚上。我记得非常清楚&#xff0c;要去跟父母一同度假。那是他们是第一次去班加罗尔&#xff0c;我都计划好了带他们逛逛。工作已经完成&#xff0c;且一般周五晚都不会太忙。可就在下班时&#xff0c;对方突然发邮件问我要很早…