之前详细介绍了HDFS的简易实际操作,今日进到HDFS中的Java和Python的API实际操作,后边很有可能详细介绍Scala的有关的。
在讲Java API以前介绍一下应用的IDE——IntelliJ IDEA ,我自己应用的是2020.3 x64的小区版本号。
建立maven工程项目,有关Maven的配备,在IDEA中,Maven免费下载源务必配备成阿里云服务器。
在相匹配的D:\apache-maven-3.8.1-bin\apache-maven-3.8.1\conf\settings.xml必须设定阿里云服务器的免费下载源。
下边建立maven工程项目,加上普遍的依靠
加上hadoop-client依靠,版本号最好是和hadoop特定的一致,并加上junit单元测试卷依靠。
- <dependencies>
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-common</artifactId>
- <version>3.1.4</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-hdfs</artifactId>
- <version>3.1.4</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-client</artifactId>
- <version>3.1.4</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- </dependency>
- </dependencies>
在这儿撰写检测类就可以,新创建一个java文件:main.java
这儿的FileSyste一开始是当地的系统文件,必须复位为HDFS的系统文件
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.junit.Test;
- import java.net.URI;
- public class main {
- @Test
- public void testPut() throws Exception {
- // 获得FileSystem类的方式有很多种多样,这儿只写一种(较为常见的是使URI)
- Configuration configuration = new Configuration();
- // user是Hadoop群集的账户,联接端口号默认设置9000
- FileSystem fileSystem = FileSystem.get(
- new URI("hdfs://192.168.147.128:9000"),
- configuration,
- "hadoop");
- // 将f:/stopword.txt 上传入 /user/stopword.txt
- fileSystem.copyFromLocalFile(
- new Path("f:/stopword.txt"), new Path("/user/stopword.txt"));
- fileSystem.close();
- }
- }
在相匹配的HDFS中,便会看到我刚提交的深度学习有关的停用词。
因为每一次都必须复位FileSystem,较为懒的我立即应用@Before每一次载入。
HDFS压缩文件下载的API插口是copyToLocalFile,实际编码以下。
- @Test
- public void testDownload() throws Exception {
- Configuration configuration = new Configuration();
- FileSystem fileSystem = FileSystem.get(
- new URI("hdfs://192.168.147.128:9000"),
- configuration,
- "hadoop");
- fileSystem.copyToLocalFile(
- false,
- new Path("/user/stopword.txt"),
- new Path("stop.txt"),
- true);
- fileSystem.close();
- System.out.println("over");
- }
下边关键详细介绍hdfs,参照:https://hdfscli.readthedocs.io/
大家根据指令pip install hdfs安裝hdfs库,在应用hdfs前,应用指令hadoop fs -chmod -R 777 / 对文件列表及文件目录下全部的文档授予可写应写可实行管理权限。
- >>> from hdfs.client import Client
- >>> #2.X版本号port 应用50070 3.x版本号port 应用9870
- >>> client = Client('http://192.168.147.128:9870')
- >>> client.list('/') #查询hdfs /下的文件目录
- ['hadoop-3.1.4.tar.gz']
- >>> client.makedirs('/test')
- >>> client.list('/')
- ['hadoop-3.1.4.tar.gz', 'test']
- >>> client.delete("/test")
- True
- >>> client.download('/hadoop-3.1.4.tar.gz','C:\\Users\\YIUYE\\Desktop')
- 'C:\\Users\\YIUYE\\Desktop\\hadoop-3.1.4.tar.gz'
- >>> client.upload('/','C:\\Users\\YIUYE\\Desktop\\demo.txt')
- >>> client.list('/')
- '/demo.txt'
- >>> client.list('/')
- ['demo.txt', 'hadoop-3.1.4.tar.gz']
- >>> # 提交demo.txt 內容:Hello \n hdfs
- >>> with client.read("/demo.txt") as reader:
- ... print(reader.read())
- b'Hello \r\nhdfs\r\n'
相对性于Java API,Python API联接确实简易。