赞
踩
HDFS (全称:Hadoop Distribute File System,Hadoop 分布式文件系统)是 Hadoop 核心组成,是分布式存储服务。
分布式文件系统横跨多台计算机,在大数据时代有着广泛的应用前景,它们为存储和处理超大规模数据提供所需的扩展能力。
HDFS是分布式文件系统中的一种
HDFS 通过统一的命名空间目录树来定位文件; 另外,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色(分布式本质是拆分,各司其职)
基本语法
bin/hadoop fs 具体命令 OR bin/hdfs dfs 具体命令
命令大全
hdfs dfs Usage: hadoop fs [generic options] [-appendToFile <localsrc> ... <dst>] [-cat [-ignoreCrc] <src> ...] [-checksum <src> ...] [-chgrp [-R] GROUP PATH...] [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-copyFromLocal [-f] [-p] [-l] [-d] <localsrc> ... <dst>] [-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...] [-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>] [-createSnapshot <snapshotDir> [<snapshotName>]] [-deleteSnapshot <snapshotDir> <snapshotName>] [-df [-h] [<path> ...]] [-du [-s] [-h] [-x] <path> ...] [-expunge] [-find <path> ... <expression> ...] [-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-getfacl [-R] <path>] [-getfattr [-R] {-n name | -d} [-e en] <path>] [-getmerge [-nl] [-skip-empty-file] <src> <localdst>] [-help [cmd ...]] [-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]] [-mkdir [-p] <path> ...] [-moveFromLocal <localsrc> ... <dst>] [-moveToLocal <src> <localdst>] [-mv <src> ... <dst>] [-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>] [-renameSnapshot <snapshotDir> <oldName> <newName>] [-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...] [-rmdir [--ignore-fail-on-non-empty] <dir> ...] [-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]] [-setfattr {-n name [-v value] | -x name} <path>] [-setrep [-R] [-w] <rep> <path> ...] [-stat [format] <path> ...] [-tail [-f] <file>] [-test -[defsz] <path>] [-text [-ignoreCrc] <src> ...] [-touchz <path> ...] [-truncate [-w] <length> <path> ...] [-usage [cmd ...]] Generic options supported are: -conf <configuration file> specify an application configuration file -D <property=value> define a value for a given property -fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations. -jt <local|resourcemanager:port> specify a ResourceManager -files <file1,...> specify a comma-separated list of files to be copied to the map reduce cluster -libjars <jar1,...> specify a comma-separated list of jar files to be included in the classpath -archives <archive1,...> specify a comma-separated list of archives to be unarchived on the compute machines The general command line syntax is: command [genericOptions] [commandOptions]
[root@linux121 hadoop-2.9.2]$ sbin/start-dfs.sh
[root@linux122 hadoop-2.9.2]$ sbin/start-yarn.sh
hadoop fs -help rm
hdfs fs-ls /
Found 5 items
drwxr-xr-x - root supergroup 0 2020-10-12 15:41 /test
drwx------ - root supergroup 0 2020-10-12 16:28 /tmp
drwxr-xr-x - root supergroup 0 2020-10-12 15:47 /wcinput
drwxr-xr-x - root supergroup 0 2020-10-12 15:45 /wcinputls
drwxr-xr-x - root supergroup 0 2020-10-12 17:51 /wcoutput
fs -mkdir -p /lagou/bigdata
hadoop fs -ls /
Found 6 items
drwxr-xr-x - root supergroup 0 2020-10-12 19:31 /lagou
drwxr-xr-x - root supergroup 0 2020-10-12 15:41 /test
drwx------ - root supergroup 0 2020-10-12 16:28 /tmp
drwxr-xr-x - root supergroup 0 2020-10-12 15:47 /wcinput
drwxr-xr-x - root supergroup 0 2020-10-12 15:45 /wcinputls
drwxr-xr-x - root supergroup 0 2020-10-12 17:51 /wcoutput
touch hadoop.txt
[root@linux135 servers]# hadoop fs -moveFromLocal hadoop.txt /lagou/bigdata
[root@linux135 servers]# ll
总用量 4
drwxr-xr-x. 11 root root 4096 10月 12 15:06 hadoop-2.9.2
[root@linux135 servers]# hadoop fs -ls /lagou/bigdata
Found 1 items
-rw-r--r-- 3 root supergroup 0 2020-10-12 19:33 /lagou/bigdata/hadoop.txt
[root@linux135 servers]#
touch hdfs.txt
vim hdfs.txt
#输入 namenode datanode block replication
touch hdfs.txt
[root@linux135 servers]# hadoop fs -appendToFile hdfs.txt /lagou/bigdata/hadoop.txt
[root@linux135 servers]# hadoop fs -cat /lagou/bigdata/hadoop.txt
namenode datanode block replication
hadoop fs -chmod 666 /lagou/bigdata/hadoop.txt
hadoop fs -chown root:root /lagou/bigdata/hadoop.txt
ll
总用量 8
drwxr-xr-x. 11 root root 4096 10月 12 15:06 hadoop-2.9.2
-rw-r--r--. 1 root root 36 10月 12 19:35 hdfs.txt
[root@linux135 servers]# hadoop fs -copyToLocal /lagou/bigdata/hadoop.txt .
[root@linux135 servers]# ll
总用量 12
drwxr-xr-x. 11 root root 4096 10月 12 15:06 hadoop-2.9.2
-rw-r--r--. 1 root root 36 10月 12 19:39 hadoop.txt
-rw-r--r--. 1 root root 36 10月 12 19:35 hdfs.txt
[root@linux135 servers]# cat hadoop.txt
namenode datanode block replication
adoop fs -copyFromLocal README.txt /
fs -cp /lagou/bigdata/hadoop.txt /hdfs.txt
adoop fs -ls /lagou/bigdata/
Found 1 items
-rw-rw-rw- 3 root root 36 2020-10-12 19:36 /lagou/bigdata/hadoop.txt
[root@linux135 servers]# hadoop fs -ls /
Found 7 items
-rw-r--r-- 3 root supergroup 36 2020-10-12 19:41 /hdfs.txt
drwxr-xr-x - root supergroup 0 2020-10-12 19:31 /lagou
drwxr-xr-x - root supergroup 0 2020-10-12 15:41 /test
drwx------ - root supergroup 0 2020-10-12 16:28 /tmp
drwxr-xr-x - root supergroup 0 2020-10-12 15:47 /wcinput
drwxr-xr-x - root supergroup 0 2020-10-12 15:45 /wcinputls
drwxr-xr-x - root supergroup 0 2020-10-12 17:51 /wcoutput
hadoop fs -mv /hdfs.txt /lagou/bigdata/
hadoop fs -ls /
Found 6 items
drwxr-xr-x - root supergroup 0 2020-10-12 19:31 /lagou
drwxr-xr-x - root supergroup 0 2020-10-12 15:41 /test
drwx------ - root supergroup 0 2020-10-12 16:28 /tmp
drwxr-xr-x - root supergroup 0 2020-10-12 15:47 /wcinput
drwxr-xr-x - root supergroup 0 2020-10-12 15:45 /wcinputls
drwxr-xr-x - root supergroup 0 2020-10-12 17:51 /wcoutput
[root@linux135 servers]# hadoop fs -ls /lagou/bigdata
Found 2 items
-rw-rw-rw- 3 root root 36 2020-10-12 19:36 /lagou/bigdata/hadoop.txt
-rw-r--r-- 3 root supergroup 36 2020-10-12 19:41 /lagou/bigdata/hdfs.txt
hadoop fs -get /lagou/bigdata/hadoop.txt .
[root@linux135 servers]# ll
总用量 12
drwxr-xr-x. 11 root root 4096 10月 12 15:06 hadoop-2.9.2
-rw-r--r--. 1 root root 36 10月 12 19:44 hadoop.txt
-rw-r--r--. 1 root root 36 10月 12 19:35 hdfs.txt
hadoop fs -mkdir -p /user/root/test/
#本地文件系统创建yarn.txt
[root@linux121 hadoop-2.9.2]$ vim yarn.txt
resourcemanager nodemanager
hadoop fs -mkdir -p /user/root/test
[root@linux135 servers]# hadoop fs -put yarn.txt /user/root/test/
[root@linux135 servers]# hadoop fs -ls /user/root/test/
Found 1 items
-rw-r--r-- 3 root supergroup 28 2020-10-12 19:46 /user/root/test/yarn.txt
hadoop fs -tail /user/root/test/yarn.txt
resourcemanager nodemanager
hadoop fs -rm /user/root/test/yarn.txt
Deleted /user/root/test/yarn.txt
[root@linux135 servers]# hadoop fs -ls /user/root/test/
[root@linux135 servers]#
hadoop fs -mkdir /test
hadoop fs -rmdir /test 或者 hadoop fs -rm -R /test
fs -du -s -h /user/root/test/
0 /user/root/test
[root@linux135 servers]# hadoop fs -du -s /user/root/test/
0 /user/root/test
[root@linux135 servers]# hadoop fs -du -h /user/root/test/
hadoop fs -setrep 10 /lagou/bigdata/hadoop.txt
这里设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的
数量。因为目前只有3台设备,最多也就3个副本,只有节点数的增加到10台时,副本数才能达到10。
Window下解压
配置环境变量
<?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>com.liu</groupId> <artifactId>stage04-hdfs</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.9.2</version> </dependency> <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> </dependencies> <!--maven打包插件 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin </artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
public class HdfsClient { @Test public void testMkdirs() throws IOException, InterruptedException, URISyntaxException { //1. 创建配置文件 Configuration configuration = new Configuration(); //2. 根据Configuration获取FileSystem对象 FileSystem fs = FileSystem.get(new URI("hdfs://192.168.181.135:9000"), configuration, "root"); //3. 使用FileSystem对象创建目录 fs.mkdirs(new Path("/api_test5")); //4.释放FileSystem对象 fs.close(); } }
java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset.
下载地址
https://github.com/cdarlint/winutils
下载对应版的一般下载的winutils.exe是这个包放到bin目录下
再次启动IDEA或者Ecplice即可
如果不指定操作HDFS集群的用户信息,默认是获取当前操作系统的用户信息,出现权限被拒绝的问
题,报错如下
从资料文件夹中找到winutils.exe拷贝放到windows系统Hadoop安装目录的bin目录下即可!!
HDFS文件系统权限问题
hdfs的文件权限机制与linux系统的文件权限机制类似!!
r:read w:write x:execute 权限x对于文件表示忽略,对于文件夹表示是否有权限访问其内容
如果linux系统用户zhangsan使用hadoop命令创建一个文件,那么这个文件在HDFS当中的owner
就是zhangsan
HDFS文件权限的目的,防止好人做错事,而不是阻止坏人做坏事。HDFS相信你告诉我你是谁,
你就是谁!!
解决方案
vim hdfs-site.xml
#添加如下属性
<property>
<name>dfs.permissions</name>
<value>true</value>
</property>
修改完成之后要分发到其它节点,同时要重启HDFS集群
我们可以考虑借助kerberos以及sentry等安全框架来管理大数据集群安全。所以我们直接修
改HDFS的根目录权限为777
hadoop fs -chmod -R 777 /
@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://192.168.181.135:9000"),
configuration, "root");
// 2 上传文件
fs.copyFromLocalFile(new Path("e:/test.txt"), new
Path("/test.txt"));
// 3 关闭资源
fs.close();
System.out.println("end");
}
默认副本数是3
configuration.set("dfs.replication", "2");
<
?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>
. 参数优先级
参数优先级排序:(1)代码中设置的值 >(2)用户自定义配置文件 >(3)服务器的默认配置
@Test public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.181.135:9000"), configuration, "root"); // 2 执行下载操作 // boolean delSrc 指是否将原文件删除 // Path src 指要下载的文件路径 // Path dst 指将文件下载到的路径 // boolean useRawLocalFileSystem 是否开启文件校验 fs.copyToLocalFile(false, new Path("/test.txt"), new Path("e:/test_copy.txt"), true); // 3 关闭资源 fs.close(); }
@Test
public void testDelete() throws IOException, InterruptedException,
URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.181.135:9000"),
configuration, "root");
// 2 执行删除 true递归
fs.delete(new Path("/api_test/"), true);
// 3 关闭资源
fs.close();
}
@Test public void testListFiles() throws IOException, InterruptedException, URISyntaxException{ Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://linux121: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); } } S ystem.out.println("-----------华丽的分割线----------"); } // 3 关闭资源 fs.close(); }
// 1 获取文件配置信息 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://linux121: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(); }
@Test public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"), configuration, "root"); // 2 创建输入流 FileInputStream fis = new FileInputStream(new File("e:/test.txt")); // 3 获取输出流 FSDataOutputStream fos = fs.create(new Path("/test_io.txt")); // 4 流对拷 IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close(); }
// 文件下载 @Test public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"), configuration, "root"); // 2 获取输入流 FSDataInputStream fis = fs.open(new Path("/test_io.txt")); // 3 获取输出流 FileOutputStream fos = new FileOutputStream(new File("e:/test_io_copy.txt")); // 4 流的对拷 IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close(); }
@Test public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.181.135:9000"), configuration, "root"); // 2 打开输入流,读取数据输出到控制台 FSDataInputStream in = null; try{ in= fs.open(new Path("/test.txt")); IOUtils.copyBytes(in, System.out, 4096, false); in.seek(0); //从头再次读取 IOUtils.copyBytes(in, System.out, 4096, false); }finally { IOUtils.closeStream(in); }
package com.liu.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class HdfsClient { Configuration configuration = null; FileSystem fs = null; @Before public void init() throws URISyntaxException, IOException, InterruptedException { //1 获取Hadoop 集群的configuration对象 configuration = new Configuration(); // configuration.set("fs.defaultFS", "hdfs://linux135:9000"); configuration.set("dfs.replication", "2"); //2 根据configuration获取Filesystem对象 fs = FileSystem.get(new URI("hdfs://192.168.181.135:9000"), configuration, "root"); } @After public void destory() throws IOException { //4 释放FileSystem对象(类似数据库连接) fs.close(); } @Test public void testMkdirs() throws IOException, InterruptedException, URISyntaxException { fs.mkdirs(new Path("/api_test")); } @Test public void testMkdirs2() throws IOException { fs.mkdirs(new Path("/api_test_2")); } @Test public void testCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException { //e:\test.txt //3. 使用FileSystem对象 // boolean delSrc 指是否将原文件删除 // Path src 指要下载的文件路径 // Path dst 指将文件下载到的路径 // boolean useRawLocalFileSystem 是否开启文件校验 fs.copyFromLocalFile(new Path("e:/test.txt"), new Path("/test.txt")); //上传文件到hdfs默认是3个副本, //如何改变上传文件的副本数量? //1 configuration对象中指定新的副本数量 } @Test public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException { // 2 执行下载操作 // boolean delSrc 指是否将原文件删除 // Path src 指要下载的文件路径 // Path dst 指将文件下载到的路径 // boolean useRawLocalFileSystem 是否开启文件校验 fs.copyToLocalFile(true, new Path("/test.txt"), new Path("e:/test_copy.txt"), true); } @Test public void testDelete() throws IOException { // 2 执行删除 fs.delete(new Path("/api_test_2"), true); } //获取文件详情 @Test public void testListFiles() throws IOException { 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("-----------华丽的分割线----------"); } } @Test public void testListStatus() throws IOException, InterruptedException, URISyntaxException { 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()); } } } @Test public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException { // 2 创建输入流 FileInputStream fis = new FileInputStream(new File("e:/test.txt")); // 3 获取输出流 FSDataOutputStream fos = fs.create(new Path("/test_io.txt")); // 4 流对拷 IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); } // // 文件下载 //1. 需求:从HDFS上下载test.txt文件到本地e盘上 //2. 编写代码 // 文件下载 @Test public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException { FSDataInputStream fis = fs.open(new Path("/test_io.txt")); // 3 获取输出流 FileOutputStream fos = new FileOutputStream(new File("e:/test_io_copy.txt")); // 4 流的对拷 IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); } @Test public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{ // 2 打开输入流,读取数据输出到控制台 FSDataInputStream fis = null; try { fis = fs.open(new Path("/test.txt")); IOUtils.copyBytes(fis, System.out, 4096, false); System.out.println("\n======从头再来========"); fis.seek(0); //从头再次读取 IOUtils.copyBytes(fis, System.out, 4096, false); }finally { IOUtils.closeStream(fis); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。