温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何让HDFS中的Java和Python API接口连接

发布时间:2021-10-09 16:38:24 来源:亿速云 阅读:183 作者:柒染 栏目:大数据

今天就跟大家聊聊有关如何让HDFS中的Java和Python API接口连接,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

现在进入HDFS中的Java和Python的API操作,后面可能介绍Scala的相关的。

在讲Java API之前介绍一下使用的IDE——IntelliJ IDEA ,我本人使用的是2020.3 x64的社区版本。

Java API

创建maven工程,关于Maven的配置,在IDEA中,Maven下载源必须配置成阿里云。

如何让HDFS中的Java和Python API接口连接

在对应的D:\apache-maven-3.8.1-bin\apache-maven-3.8.1\conf\settings.xml需要设置阿里云的下载源。

下面创建maven工程,添加常见的依赖

如何让HDFS中的Java和Python API接口连接

添加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>

HDFS文件上传

在这里编写测试类即可,新建一个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中,就会看见我刚刚上传的机器学习相关的停用词。

如何让HDFS中的Java和Python API接口连接

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"); }

Python API

下面主要介绍hdfs

我们通过命令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连接实在简单。

看完上述内容,你们对如何让HDFS中的Java和Python API接口连接有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI