Hadoop学习笔记(二)(HDFS)

hdfs基本架构:1 Master(NameNode/NN)  带 N个Slaves(DataNode/DN)

1个文件会被拆分成多个Block
blocksize:128M
130M ==> 2个Block: 128M 和 2M

NN(NameNode):
1)负责客户端请求的响应
2)负责元数据(文件的名称、副本系数、Block存放的DN)的管理

DN(DataNode):
1)存储用户的文件对应的数据块(Block)
2)要定期向NN发送心跳信息,汇报本身及其所有的block信息,健康状况

A typical deployment has a dedicated machine that runs only the NameNode software. 
Each of the other machines in the cluster runs one instance of the DataNode software.
The architecture does not preclude running multiple DataNodes on the same machine 
but in a real deployment that is rarely the case.

NameNode + N个DataNode
建议:NN和DN是部署在不同的节点上
replication factor:副本系数、副本因子

All blocks in a file except the last block are the same size

我自己的使用习惯:
hadoop/hadoop
/home/hadoop
    software: 存放的是安装的软件包
    app : 存放的是所有软件的安装目录
    data: 存放的是课程中所有使用的测试数据目录
    source: 存放的是软件源码目录,spark

Hadoop shell的基本使用(分为两种)
hdfs dfs
hadoop fs

hdfs的基本操作:
vi hello.txt
hadoop fs -put hello.txt /
hadoop fs -ls /
hadoop fs -text /hello.txt
hadoop fs -mkdir /test
hadoop fs -mkdir -p /test/a
hadoop fs -ls -R /
hadoop fs -copyFromLocal hello.txt /test/a/h.txt
hadoop fs -get /test/a/h.txt
hadoop fs -rm /h.txt

Hdfs在java中的开发:1)
project添加hadoop依赖

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>${hadoop.version}</version>
    </dependency>
</dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
    </properties>


    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>
    </repositories>

2)

public static final String HDFS_PATH="hdfs://hadoop000:8020";
Configuration configuration=null;
FileSystem fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,用户名);

3)
创建HDFS目录  fileSystem.mkdirs(new Path("/hdfsapi/test "));
创建文件      FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
                    output.write("hello".getBytes());
                    output.flush();
                    output.close();
查看内容      FSDataInputStream in = fileSystem.open(new Path("/hdfsapi/test/a.txt"));
                     IOUtils.copyBytes(in,System.out,1024);
                     in.close;
重命名                  fileSystem.rename(src,des); 
上传文件到HDFS fileSystem.copyFromLocalFile(src,des); 
上传带进度条   InputStram in =new BufferedInputStream(new FileInputStream(new File(path)));
                         FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/a.txt") new Progressabel(){});
                         IOUtils.copyBytes(in,output,4096);
下载到本地       fileSystem.copyToLocalFile(src,des);
展示所有文件
                         FileStatus[] fileStatus = fileSystem.listStatus(new Path())
删除文件          fileSystem.delete(new Path(src,boolean));

 

问题:我们已经在hdfs-site.xml中设置了副本系数为1,为什么此时查询文件看到的3呢?
答:如果你是通过hdfs shell的方式put的上去的那么,采用默认的副本系数1
    如果我们是java api上传上去的,在本地我们并没有手工设置副本系数,所以否则采用的是hadoop自己的副本系数3

 HDFS优点:

  •  数据冗余、硬件容错
  •  处理流式数据访问
  •  适合存储大文件
  •  可以运行在廉价设备上

 
 HDFS缺点:

  •  低延迟数据访问
  •  不适合小文件存储
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值