<>客户端环境准备

hadoop的 Windows依赖文件夹,拷贝hadoop-3.1.0到非中文路径(比如d:\)。

* 配置HADOOP_HOME环境变量

* 配置Path环境变量。

不能放在包含有空格的目录下,cmd 输入hadoop显示此时不应有 \hadoop-3.0.0\bin\。我放在E:\Program Files
(x86) \hadoop-3.0.0\bin\中,就出现错误

验证Hadoop环境变量是否正常。双击winutils.exe,如果报如下错误。说明缺少微软运行库(正版系统往往有这个问题)。里面有对应的微软运行库安装包双击安装即可。

* 配置Path环境变量。然后重启电脑
*
如果上述操作后在后面代码执行的过程中,还有问题可以将bin目录下hadoop.dll和winutils.exe放到C:/windows/system32目录下
* 在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>
* 在项目的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
* 创建HdfsClient类
<>方式一
public class HdfsClient{ @Test public void testMkdirs() throws IOException,
InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration
configuration = new Configuration(); // 配置在集群上运行 FileSystem fs =
FileSystem.get( new URI("hdfs://hadoop102:8020"), configuration, "xiaoming" );
} }
vim core-site.xml

<>方式二 给main方法传参数
1.代码部分 public class HdfsClient{ @Test public void testMkdirs() throws
IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration(); // 配置在集群上运行
configuration.set("fs.defaultFS", "hdfs://hadoop102:8020"); FileSystem fs =
FileSystem.get(configuration); } }
* 配置部分
运行时需要配置用户名称(默认是使用windows用户名操作HDFS)

给main方法传参 :
1.在IDEA中传参 – 在运行按钮上右键—>点击Edit 类名.main() —>
Program Arguments后面写传的参数 —>默认就是字符串 多个参数之间用空格隔开,注意:如果右键没有就先运行一遍。
2. java 字节码文件名 参数1 参数2 …

客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=*****,
为用户名称。
错误:Permission denied: user=XXXXXX, access=WRITE, inode=“/demo”:
:supergroup:drwxr-xr-x
解决方案 :①修改权限–不建议 ②修改操作HDFS的用户名(默认是windows系统登录的用户名)
修改用户名:在IDEA中传参 -- 在运行按钮上右键--->点击Edit 类名.main() ---> VM OPtions :
-DHADOOP_USER_NAME=******

public static void main(String[] args) throws URISyntaxException,
IOException, InterruptedException { URI uri = new URI("hdfs://hadoop102:8020");
Configuration conf = new Configuration(); conf.set("fs.defaultFS",
"hdfs://hadoop102:8020"); conf.set("dfs.replication","2"); FileSystem fs =
FileSystem.get( conf); // fs.copyToLocalFile(false, new
Path("/input/word.txt"), new Path("E:\\io"), false);
fs.copyFromLocalFile(false,true,new Path("E:\\io\\upload.txt"),new
Path("/input")); fs.close(); } }

VM options可以调节JVM的堆栈等大小。

<>HDFS文件上传
@Test public void testCopyFromLocalFile() throws IOException,
InterruptedException, URISyntaxException { // 1 获取文件系统 Configuration
configuration = new Configuration(); FileSystem fs = FileSystem.get(new
URI("hdfs://hadoop102:8020"), configuration, "xiaoming"); // 2 上传文件
fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));
// 3 关闭资源 fs.close();
IDEA创建Maven工程,由于版本不一样,这里我使用2022.3版本,有一个将junit加入classpath的设置,还有将各种目录设置:Mark
Directory as :src/main/java 关联为 Sources Root;(右击java》选择mark Dirctory
as》选择Sources Root);src/main/resources 关联为Resources Root;(右击resources》选择mark
Dirctory as》选择Resources Root);src/test/java 关联为Test Sources
Root;(右击test文件夹下test》选择mark Dirctory as》选择Test Sources Root;src/test/resources
关联为 Test Resources Root;(右击test文件夹下resources》选择mark Dirctory as》选择Test
Resources Root)

还有最后一步就是:选中pom.xml文件,右键add as Maven project。Jar包没有生效就重新加载pom.xml文件。

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


例如:本地上传a.txt到HDFS上面,首先会在本地对a.txt进行一个校验得到一个校验值(123),然后将a.txt和校验值123一起上传到HDFS。在HDFS上面进行性文件a.txt的再一次校验值***,将123

***进行对比。下载也是一样的流程。比如上传文件到百度网盘,会发现有时候特别快,上传的文件会有一个校验值,其他人上传相同文件的时候,会将另外待上传相同文件计算一个校验值去服务端查找,有此校验值,就保存一个在百度网网盘。上传违法文件时,也会在黑名单保留此文件的校验值。

<>测试参数优先级

* 编写源代码 @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,
"xiaoming"); // 2 上传文件 fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new
Path("/banzhang.txt")); // 3 关闭资源 fs.close(); System.out.println("over");
* 将hdfs-site.xml拷贝到项目的根目录(resource)下 <?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)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置
参数的配置 : 客户端代码,客户端的配置文件,服务器端xxx-default.xml,服务器端的xxx-site.xml
在客户端的执行的命令 : 客户端代码 > 客户端的配置文件 > 服务器端的xxx-default.xml(默认备份数3)
如果注释掉客户端代码和配置文件,这个时候如果服务端也配置了hdfs-site.xml (例如配置8),是不生效的,上传文件默认备份数3。
在服务器端执行命令 : hadoop fs -put a.txt /demo
服务器端xxx-site.xml > 服务器端xxx-default.xml

<>验证1:客户端和配置文件都设置了备份数,看谁优先级高!

客户端设置为文件备份数2,配置文件设置为文件备份数5

<>验证2: 只在配置文件里设置文件备份数

<>验证3: 服务端 /opt/module/hadoop-3.1.3/etc/hadoop/hdfs-site.xml。设置之后必须分发xsync
hdfs-site.xml并重启HDFS。注意:此操作仅涉及服务端,与客户端没有任何关系!!!!!

分发重启不能立马操作!
以下结果证明了服务器端xxx-site.xml > 服务器端xxx-default.xml

注意:刚启动集群不能立马操作,否则会报下面的错。

分享到此,感谢下伙伴们支持!

技术
今日推荐
PPT
阅读数 135
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信