HDFS中的Java和Python API接口连接
上次介绍了HDFS的简单操作,今天进入HDFS中的Java和Python的API操作,后面可能介绍Scala的相关的。 在讲Java API之前介绍一下使用的IDE——IntelliJ IDEA ,我本人使用的是2020.3 x64的社区版本。 Java API 创建maven工程,关于Maven的配置,在IDEA中,Maven下载源必须配置成阿里云。 在对应的D:apache-maven-3.8.1-binapache-maven-3.8.1confsettings.xml需要设置阿里云的下载源。 下面创建maven工程,添加常见的依赖 添加hadoop-client依赖,版本最好和hadoop指定的一致,并添加junit单元测试依赖。 &nbHDsp; org.apache.hadoop hadoop-common 3.1.4 org.apache.hadoop hadoop-hdfs FS 3.1.4 org.apache.hadoop hadoop-client 3.1.4 junit junit &nb中的sp; 4.11 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 mJaain { @Test public void testPut() throws Exception { // 获取FileSystem类的方法有很多种,这里只写一种(比较常用的是使URI) Configuration configuration = new&vanbsp;Configuration(); // user是Hadoop集群的账号,连接端口默认9000 FileSystem fileSystem = FileSystem.get( new URI("hdfs://192.168.147.128:9000"), &n和Pbsp; configuration, "hadoop"); // 将f:/stopword.txt 上传到 /user/stopword.txt &nbytsp; 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( &nbsn p; new URI("hdfs://192.168.147.128:9000"), configuration, "hadoop"); fileSystem.copyToLocalFile( AP false, new Path("/user/stopword.txt"), new Path("stop.txt"), true); &nbI接sp; fileSystem.close(); System.out.println("over"); } Python API 下面主要介绍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'] >>> clHDient.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', 'hadooFSp-3.1.4.tar.gz'] >>> # 上传demo.txt 内容:Hello hdfs >>> with client.read("/demo.txt") as reader: ... print(reader.read()) b'Hello hdfs ' 相对于Java API,Python API连接实在简单。
HDhoFS文件下载
- 最近发表
- 随机阅读
-
- 标签: 奇瑞汽车
- 边缘计算将在企业中扮演越来越重要的角色
- 云计算核心技术Docker教程:Docker创建和管理卷
- 标签: 腾易科技 本地生态联盟 汽车营销 数据孤岛
- 京东云春晚炫技登顶“珠峰” 零资源增加背后有何底气
- 美城市Baltimore可能颁布最严格的面部识别禁令
- 消息称Meta在洽谈筹集350亿美元资金 用于打造数据中心
- 标签: 乐享科技
- AI训练的福音:关于合成数据的一切
- 标签: 滴滴 春运保障 司机补贴 安全出行
- 云计算开发:Python3三角函数sin()方法详解
- 谷歌地球推出3D时间推移功能 显示了气候变化对地球造成的破坏性
- IDC:2020上半年中国工业云市场规模达16.0亿美元
- 首次超越自回归模型!蚂蚁集团开源业内首个高性能扩散语言模型推理框架dInfer
- 基于kubernetes构建混合云的利弊
- 标签: Grok X xAI
- 2025 全球十大工程成就揭晓:DeepSeek入选
- 前馈网络+线性交互层=残差MLP,Facebook纯MLP图像分类架构入场
- 深度心理创伤:多名用户投诉ChatGPT引发妄想和偏执,敦促FTC介入
- 巴黎奥运会将基于阿里云实现云上转播
- 搜索
-
- 友情链接