技术标签: hdfs hadoop big data 大数据
1)产生背景
随着数据量的增大,应该操作系统中存不下所有的数据,那么就要分配到更多的操作系统的磁盘中,但是又不方便管理与维护,需要一种系统来管理多台机器上的文件
,这就是分布式文件管理系统。HDFS
只是分布式文件管理系统中的一种。
2)HDFS定义
HDFS(Hadoop Distributed File System)
,它是一个文件系统,用于存储文件,通过目录树来定位文件;其次是分布式
,由很多机器联合起来实现功能,集群中的服务器有各自的角色。
HDFS的使用场景:一次写入,多次读出
。一个文件经过创建、写入和关闭之后就不需要改变。
1)高容错性
2)适合处理大数据
PB级别的数据
百万规模
以上的文件数量3)可创建在廉价机器上
,通过多副本机制,提高可靠性
1)不适合低延时数据访问
,比如毫秒级的存储数据,是做不到的
2)无法高效的对大量小文件进行存储
3)不支持并发写入、文件随机修改。
只支持数据append
,不支持文件的随机修改
1)Namenode就是:master,管理者
2)DataNode:就是Slave,Namenode下达命令,Datanode执行实际的操作。
3)Client:就是客户端
HDFS中的文件在物理上是分块存储(Block),块的大小可以通过配置参数(dfs blocksize)来规定,默认大小是128M,1.x版本中是64M
思考;为什么block的大小不能设置太小,也不能设置太大
太小,会增加寻址时间
。程序一直在寻找块的开始位置。太大
,从磁盘传输数据的时间
会明显大于定位这个块开始位置所需要的时间
,导致程序在处理这块数据时,会很慢。总结:HDFS块的大小设置主要取决于磁盘的传输速率
hadoop fs 具体命令 或者 hdfs dfs 具体命令
两者完全相同
[atguigu@hadoop102 hadoop-3.1.3]$ bin/hadoop fs
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] <localsrc> ... <dst>]
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] <path> ...]
[-cp [-f] [-p] <src> ... <dst>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] <path> ...]
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getmerge [-nl] <src> <localdst>]
[-help [cmd ...]]
[-ls [-d] [-h] [-R] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] <localsrc> ... <dst>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
<acl_spec> <path>]]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
1)启动Hadoop集群(方便后续的测试)
[atguigu@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh
[atguigu@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh
2)-help:输出这个命令参数
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -help rm
3)创建/sanguo文件夹
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /sanguo
1)-moveFromLocal:从本地剪切粘贴到HDFS
[atguigu@hadoop102 hadoop-3.1.3]$ vim shuguo.txt
输入:
shuguo
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -moveFromLocal ./shuguo.txt /sanguo
2)-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去
[atguigu@hadoop102 hadoop-3.1.3]$ vim weiguo.txt
输入:
weiguo
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -copyFromLocal weiguo.txt /sanguo
3)-put:等同于copyFromLocal,生产环境更习惯用put
[atguigu@hadoop102 hadoop-3.1.3]$ vim wuguo.txt
输入:
wuguo
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -put ./wuguo.txt /sanguo
4)-appendToFile:追加一个文件到已经存在的文件末尾
[atguigu@hadoop102 hadoop-3.1.3]$ vim liubei.txt
输入:
liubei
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo.txt
1)-copyToLocal:从HDFS拷贝到本地
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -copyToLocal /sanguo/shuguo.txt ./
2)-get:等同于copyToLocal,生产环境更习惯用get
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -get /sanguo/shuguo.txt ./shuguo2.txt
2.3.4 HDFS直接操作
1)-ls: 显示目录信息
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -ls /sanguo
2)-cat:显示文件内容
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -cat /sanguo/shuguo.txt
3)-chgrp、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -chmod 666 /sanguo/shuguo.txt
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -chown atguigu:atguigu /sanguo/shuguo.txt
4)-mkdir:创建路径
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /jinguo
5)-cp:从HDFS的一个路径拷贝到HDFS的另一个路径
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -cp /sanguo/shuguo.txt /jinguo
6)-mv:在HDFS目录中移动文件
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/wuguo.txt /jinguo
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/weiguo.txt /jinguo
7)-tail:显示一个文件的末尾1kb的数据
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -tail /jinguo/shuguo.txt
8)-rm:删除文件或文件夹
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -rm /sanguo/shuguo.txt
9)-rm -r:递归删除目录及目录里面内容
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -rm -r /sanguo
10)-du统计文件夹的大小信息
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -du -s -h /jinguo
27 81 /jinguo
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -du -h /jinguo
14 42 /jinguo/shuguo.txt
7 21 /jinguo/weiguo.txt
6 18 /jinguo/wuguo.tx
说明:27表示文件大小;81表示27*3个副本;/jinguo表示查看的目录
11)-setrep:设置HDFS中文件的副本数量
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -setrep 10 /jinguo/shuguo.txt
这里设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的数量。因为目前只有3台设备,最多也就3个副本,只有节点数的增加到10台时,副本数才能达到10。
1) 在IDEA中创建一个Maven工程HdfsClientDemo,并导入相应的依赖坐标+日志添加
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
2)在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3)创建包名:com.atguigu.hdfs
4)创建HdfsClient类
public class HdfsClient {
@Test
public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
// 1 获取文件系统
Configuration configuration = new Configuration();
// FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration);
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration,"atguigu");
// 2 创建目录
fs.mkdirs(new Path("/xiyou/huaguoshan/"));
// 3 关闭资源
fs.close();
}
}
5)执行程序
客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从采用Windows默认用户访问HDFS,会报权限异常错误。所以在访问HDFS时,一定要配置用户。
org.apache.hadoop.security.AccessControlException: Permission denied: user=56576, access=WRITE, inode="/xiyou/huaguoshan":atguigu:supergroup:drwxr-xr-x
1)编写源代码
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 上传文件
fs.copyFromLocalFile(new Path("d:/sunwukong.txt"), new Path("/xiyou/huaguoshan"));
// 3 关闭资源
fs.close();
}
2)将hdfs-site.xml拷贝到项目的resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
3)参数优先级
参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的自定义配置(xxx-site.xml) >(4)服务器的默认配置(xxx-default.xml)
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("d:/sunwukong2.txt"), true);
// 3 关闭资源
fs.close();
}
注意:如果执行上面代码,下载不了文件,有可能是你电脑的微软支持的运行库少,需要安装一下微软运行库。
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 修改文件名称
fs.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("/xiyou/huaguoshan/meihouwang.txt"));
// 3 关闭资源
fs.close();
}
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 执行删除
fs.delete(new Path("/xiyou"), true);
// 3 关闭资源
fs.close();
}
查看文件名称、权限、长度、块信息
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("========" + fileStatus.getPath() + "=========");
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getOwner());
System.out.println(fileStatus.getGroup());
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getModificationTime());
System.out.println(fileStatus.getReplication());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPath().getName());
// 获取块信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
// 3 关闭资源
fs.close();
}
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 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();
}
每传一个packet会放入一个应答队列等待应答
)。在HDFS写数据的过程中,NameNode会选择距离待上传数据最近距离的DataNode接受数据,那么这个最近距离怎么计算?
节点距离:两个节点达到最近的共同祖先的距离总和。
(1)机架感知说明
For the common case, when the replication factor is three, HDFS’s placement policy is to put one replica on the local machine if the writer is on a datanode, otherwise on a random datanode, another replica on a node in a different (remote) rack, and the last on a different node in the same remote rack. This policy cuts the inter-rack write traffic which generally improves write performance. The chance of rack failure is far less than that of node failure; this policy does not impact data reliability and availability guarantees. However, it does reduce the aggregate network bandwidth used when reading data since a block is placed in only two unique racks rather than three. With this policy, the replicas of a file do not evenly distribute across the racks. One third of replicas are on one node, two thirds of replicas are on one rack, and the other third are evenly distributed across the remaining racks. This policy improves write performance without compromising data reliability or read performance.
(2)源码说明
Crtl + n 查找BlockPlacementPolicyDefault,在该类中查找chooseTargetInOrder方法。
(1)客户端通过DistributedFileSystem向NameNode请求下载文件,NameNode通过查询元数据,找到文件块所在的DataNode地址。
(2)挑选一台DataNode(就近原则,然后随机)服务器,请求读取数据。
(3)DataNode开始传输数据给客户端(从磁盘里面读取数据输入流,以Packet为单位来做校验)。
(4)客户端以Packet为单位接收,先在本地缓存,然后写入目标文件。
文章浏览阅读1.7k次。srvctl常用查看与停库命令 检查节点应用程序状态[grid@db1 ~]$ srvctl status nodeappsVIP db1vip is enabledVIP db1vip is running on node: db1VIP db2vip is enabledVIP db2vip is running on node: db2_oracle srvctl 查看信息 db
文章浏览阅读2.8k次,点赞6次,收藏15次。Numpy 计算男女生各科成绩统计指标1.随机数生成六个班的考试成绩,3门考试:Python、数学、语文。每个班50人import numpy as nparr = np.random.randint(0,151,size = (6,50,3))for i in range(0,6): print(i+1,"班成绩为\n",arr[i])2.将六个班的考试成绩进行合并得到scorescore = np.concatenate(arr)score.shape>>>_生成性别数组
文章浏览阅读5.6k次,点赞20次,收藏27次。ImportError: cannot import name ‘_validate_lengths’ from 'numpy.lib.arraypad’解决方法安装scikit-image库时,同时安装了numpy依赖库,运行某个程序时,出现上面的错误。网上找了很多方法,有的说时版本太高了,但是安装了低版本也没有解决。直到在一篇博客找到了方法,虽然这个方法简单粗暴,但是好用的没得说。cannot import name ‘_validate_lengths不能导入这个函数,直接找到保存这个函数的所在_importerror: cannot import name '_validate_lengths
文章浏览阅读111次。变量: 存储单个数据 数组: 存储多个数据 [] 变量多了存数组 数组: 相同数据类型的有序集合 数组的特点: 1.数组是一个引用数据类型[] 2.数组是一个容器,长度一旦确定不可改变 3.数组中存放的数据数据类型要求相同 4.位置有序(下标|索引 都是从0开始,每次+1) 数组的声明: 数据类型 变量名; ---变量的声明 数据类型[] 数组名; ---数组的声明..._js根据数据修改数组对象长度
文章浏览阅读4.8k次,点赞4次,收藏33次。分治算法实现两个n位的正整数相乘 分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同,最后按原问题的要求,将子问题的解逐层合并构成原问题的解,快速排序算法便是基于分治策略的一种排序方法。这里要讲的是利用分治算法来实现两个n位的正整数相乘:具体思路给定两个均为n位的十进制正整数x、y,将其拆分为左右各一半的xl、xr、yl、yr。具体表示如_两个n整数相乘
文章浏览阅读935次。java.math.BigDecimal.add(BigDecimal val)用于计算两个BigDecimal的算术和。此方法用于查找大量算术加法,该算术加法远大于Java的最大数据类型double的范围,而不会影响结果的精度。此方法对当前的BigDecimal进行操作,调用该方法并将BigDecimal作为参数传递。Java中有两种add方法的重载,如下所示:add(BigDecimal va..._, both 'add(bigdecimal)' and 'add(bigdecimal, mathcontext)' match
文章浏览阅读726次。1、 TCP服务端① 先WSAStartup->检测DLL版本 信号检测② socket() ->创建套接字 买了个手机③ bind ->绑定IP、端口号 办了个卡④ listen ->监听 手机待机⑤ _对流式服务器端监听套接字感兴趣的事件是fd_accept,fd_close 。
文章浏览阅读805次。第一步:安装pip3sudo apt-get install python3-pip顺便安装numpypip3 install numpy然后会提示你更新pip的版本,于是我就选择更新了pip3 install --upgrade pip第二步:配置pip使用的镜像源附:国内镜像源列表豆瓣(douban) http://pypi.douban.com/simple/清华大学 ..._ubuntu18.04安装torch使用国内镜像源的命令
文章浏览阅读139次。3.1 概述说起垃圾收集(Garbage Collection,GC),大部分人都把这项技术当做Java语言的伴生产物。事实上,GC的历史远远比Java久远,1960年诞生于MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语言。当Lisp还在胚胎时期时,人们就在思考GC需要完成的三件事情:哪些内存需要回收?什么时候回收?如何回收?经过半个世纪的发展,内存的动态分配与内存回收技术已经相当..._desired survivor size 52428800 bytes, new threshold 15
文章浏览阅读106次。轻易不要用多表查询,如下代码以及模拟数据测试结果能让你清晰地认识到多表查询,join,left join,inner join的可怕之处,能不用就不要用/*** 获取持有偶像币总估值* @param $uid* @return mixed* @author zzl [email protected]* 时间:2019.06.18*/public function getHasCoinExpect($ui..._bc_add
文章浏览阅读1.9k次。打开IntelliJ IDEA,File -> Settings -> Editor -> File Types在红框部分加上你想过滤的文件或文件夹名转载于:https://www.cnblogs.com/warehouse/p/7018875.html_idea 隐藏文件
文章浏览阅读253次。达观杯linghtgbm1.linghtgbm2.linghtgbm代码实现1.linghtgbmLightGBM是个快速的,分布式的,高性能的基于决策树算法的梯度提升框架。可用于排序,分类,回归以及很多其他的机器学习任务中。在竞赛题中,我们知道XGBoost算法非常热门,它是一种优秀的拉动框架,但是在使用过程中,其训练耗时很长,内存占用比较大。在2017年年1月微软在GitHub的上开源了..._linghtgbm