大数据之HDFS

二、HDFS

分布式存储:多副本分块存储,并通过元数据记录数据信息

适用场景:大文件,一次写入多次读取

特点:

  • 主从架构:NameNode为主节点,DataNode为从节点
  • 分块存储:每一块大小128M,不足128M按照128M存,存在DataNode上
  • 副本机制:默认存3份副本,分别存在不同的DataNode上,1份原来文件+2份复制的文件
  • 元数据管理:NameNode管理文件自身属性和文件块位置映射信息

1、命令行

hadoop fs -ls file:///#操作本地文件系统
hadoop fs -ls hdfs://hadoop100:8020/#操作HDFS
hadoop fs -ls #直接根目录
  • 创建文件夹

    hadoop fs -mkdir [-p] <path>
    
  • 查看指定目录

    hadoop fs -ls [-h] [-R] [<path>...]
    
  • 上传文件到HDFS

    hadoop fs -put [-f] [-p] <localsrc> ...<dst>
    
  • 查看文件 cat tail

  • 下载文件get,拷贝文件cp,移动文件mv

  • 追加数据到HDFS文件中(用于小文件合并)

    hadoop fs -appendToFile <localsrc> ...<dst>
    

2、职责

主角色:NameNode是核心管理者,管理元数据,是访问HDFS的唯一入口,通常配置大内存

从角色:DataNode负责数据存储(打工人),通常配置大磁盘

辅助角色:SecondNode帮助主角色进行元数据合并

3、工作流程

在这里插入图片描述
1、pipeline传输管道:

沿着一个方向传输

2、ACK应答

3、副本存储策略:

  • 第一副本:优先本地客户端,否则就近随机
  • 第二副本:不同于第一副本的机架
  • 第三副本:第二副本相同机架的不同机器

4、代码操作HDFS

  1. 3、新建maven项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>hadoop-HDFS</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <hadoop.version>3.3.0</hadoop.version>
    </properties>

    <dependencies>
        <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>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>
</project>

4、创建HDFS客户端及方法

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.checkerframework.dataflow.qual.TerminatesExecution;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class TestHDFS {
    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("dfs.replication", String.valueOf(2));
        fs = FileSystem.get(new URI("hdfs://hadoop100:8020"), conf, "root");
    }
    @After
    public void HDFSClose() throws IOException {
        fs.close();
    }
    //新建文件夹
    @Test
    public void mkDir() throws IOException {
        fs.mkdirs(new Path("/testJava"));
    }
    //上传
    @Test
    public void testPut() throws IOException {
        //参数1:是否删除原数据,参数2:是否覆盖,参数3:原文件路径,参数4:目的路径
        fs.copyFromLocalFile(true,true,new Path("C:\\Users\\Administrator\\Desktop\\test2.txt"),new Path("/testJava"));
    }
    @Test
    public void testGet() throws IOException {
        fs.copyToLocalFile(true,new Path("/testJava/test.txt"),new Path("C:\\Users\\Administrator\\Desktop"),true);
    }
    @Test
    public void testDel() throws IOException {
        //非空目录注意递归删除
        fs.delete(new Path("/testJava/test2.txt"),true);
    }
    //文件夹更名
    @Test
    public void testMove() throws IOException {
        fs.rename(new Path("/testJava"),new Path("/testJava1"));
    }
    //查看HDFS信息
    @Test
    public void testFile() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            System.out.println(listFiles.next());
        }

    }
}

5、创建HDFS配置文件

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->
<!-- 设置副本数为1 -->
<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
</configuration>

配置文件的优先级:

用config.set代码配置>resource目录下的配置文件>服务器上的hdfs-site.xml>服务器上的hdfs-default.xml

  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值