您的当前位置:首页 > 晨间快报 > AI一键去纹身,几秒钟让你看见明星「真面目」 正文
时间:2026-10-24 来源:网络整理 编辑:晨间快报
AI一键去纹身,几秒钟让你看见明星「真面目」
上次介绍了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单元测试依赖。
org.apache.hadoop hadoop-common 3.1.4 org.apache.hadoop hadoop-hdfs 3.1.4 org.apache.hadoop hadoop-client 3.1.4 junit junit 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 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文件下载
由于每次都需要初始化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,参考: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 hdfs >>> with client.read("/demo.txt") as reader: ... print(reader.read()) b'Hello hdfs '
相对于Java API,Python API连接实在简单。
标签: 云深处 DEEP Robotics 中信建投 科创板2026-10-10
消息称“通义”应用团队从阿里云分拆,并入阿里智能信息事业群2026-07-26
深度学习GPU最全对比,到底谁才是性价比之王?2026-11-07
Facebook:正在评估反垄断诉讼2026-11-13
标签: G7 卡车2026-06-01
标签: 科大讯飞 人工智能 讯飞开放平台 大模型2026-11-10
标签: 江阴银行 2025年财报 净利润增长 资产质量2026-10-01
MongoDB Atlas支持多云数据库集群 阿里云在列2026-11-05
标签: 立景创新 立讯精密 王来春 摄像头模组2026-07-26
标签: 创维集团 智能家电 新能源业务 现代服务业2026-10-28
#亚马逊考虑接受狗狗币支付#上热搜 网友:有没有猫猫币2026-08-27
标签: 飞猪 Z世代 入境游 出境游2026-07-11
蚂蚁集团重磅推出 dInfer:让扩散语言模型的推理速度飙升 10 倍!2026-11-06
谷歌云试图抢占SAP软件云市场 但击败微软和亚马逊并不易2026-11-01
标签: 新桥生物 givastomig 港交所 CLDN18.22026-10-22
标签: 零跑汽车 朱江明 廉洁合规 反腐举报2026-10-21