您现在的位置是:热点 >>正文
HDFS中的Java和Python API接口连接
热点2864553人已围观
简介...
上次介绍了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 &FSnbsp; 3.1.4 org.apache.hadoop hadoop-client 3.1.4 junit junit &nbs中的p; 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&nJabsp;{ @Test public void testPut() throws Exception { // 获取FileSystem类的方法有很多种,这里只写一种(比较常用的是使URI) Configuration configuration = new Covanfiguration(); // user是Hadoop集群的账号,连接端口默认9000 FileSystem fileSystem = FileSystem.get( new URI("hdfs://192.168.147.128:9000"), 和P configuration, "hadoop"); // 将f:/stopword.txt 上传到 /user/stopword.txt  yt; fileSystem.copyFromLocalFile( new Path("f:/stopword.txt"), new Path("/user/stopword.txt")); fileSystem.close(); } }
在对应的HDFS中,就会看见我刚刚上传的机器学习相关的停用词。
HDFS文件下载
由于ho每次都需要初始化FileSystem,比较懒的我直接使用@Before每次加载。
HDFS文件下载的API接口是copyToLocalFile,具体代码如下。
@Test public void testDownload() throws Exception { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(  n ; new URI("hdfs://192.168.147.128:9000"), configuration, "hadoop"); fileSystem.copyToLocalFile(  AP; false, new Path("/user/stopword.txt"), new Path("stop.txt"), true); &I接nbsp; 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版本por口连t 使用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.deHDlete("/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.tFSar.gz'] >>> # 上传demo.txt 内容:Hello hdfs >>> with client.read("/demo.txt") as reader: ... print(reader.read()) b'Hello hdfs '
相对于Java API,Python API连接实在简单。
Tags:
相关文章
标签: 特斯拉 Robotaxi 马斯克 Waymo
热点云梦级2月28日云梦级消息,据外媒报道,近日,马斯克的“画饼”言论再遭打脸。此前,马斯克曾多次官宣:“特斯拉距离在加州推出 Robotaxi 无人驾驶出租车服务仅剩数月”。 然而加州机动车辆管理局记录...
【热点】
阅读更多标签: 新富科技 电池液冷管 电池液冷板 新能源汽车热管理
热点云梦级2月1日云梦级消息,2月3日,北交所上市委将召开审议会议,对安徽新富新能源科技股份有限公司的IPO申请进行审核,保荐机构为中信证券股份有限公司。新富科技位于安徽省安庆市,是一家专注于新能源汽车热...
【热点】
阅读更多鹏博士全面升级“云能力” 重磅发布三大鹏云新品
热点1月11日消息,鹏博士今天正式对外发布云计算战略2.0和鹏云系列产品组合,宣布云能力全面升级。 2020年9月,鹏博士提出云计算战略1.0“云+网+MSP服务”一体化解决方案,帮助中国中小企业客户“懂...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=歌外推【tg:seogoogle987】zqM
- https://www.riohondo.edu/?action=QUERY&s=歌外推【tg:seogoogle987】,7C5
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=歌外推【tg:seogoogle987】,8UH
- https://www.mesec.cz/vyhledavani/?qs=歌外推【tg:seogoogle987】,Lgk
- https://www.thedp.com/page/search?q=歌外推【tg:seogoogle987】,evx
- https://www.zisha.com/shop/list.shtml?wap_keyword=歌外推【tg:seogoogle987】,bkI
- https://www.wikiart.org/pt/Search/歌外推【tg:seogoogle987】,Hxa
- https://www.teacherspayteachers.com/Browse/Search:歌外推【tg:seogoogle987】,sub
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=歌外推【tg:seogoogle987】,TXF
- https://www.spoiledsweetboutique.com/search/歌外推【tg:seogoogle987】,iFc
- https://www.teamlab.art/fr/?submit=歌外推【tg:seogoogle987】,ScC
- https://www.whatsonstage.com/search?cat=64&term=歌外推【tg:seogoogle987】,b4M
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=歌外推【tg:seogoogle987】,fwZ
- https://www.rotherham.gov.uk/site-search/results/?q=歌外推【tg:seogoogle987】,PTU
- https://www.krisinformation.se/en/search?q=歌外推【tg:seogoogle987】,7kI
- https://www.theaerogroup.net/?cat=28&s=歌外推【tg:seogoogle987】,rBS
- https://www.teamlab.art/th/?submit=歌外推【tg:seogoogle987】,gHS
- https://www.pearlstonecenter.org/search/歌外推【tg:seogoogle987】,7dK
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=歌外推【tg:seogoogle987】,fHO
- https://www.nextavenue.org/?action=QUERY&s=歌外推【tg:seogoogle987】,mIF
- https://www.remit.co.jp/pt/?action=CHECK&s=歌外推【tg:seogoogle987】,io9
- https://www.merchbar.com/search?q=歌外推【tg:seogoogle987】,F64
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=歌外推【tg:seogoogle987】,j7O
- https://www.ons.gov.uk/search?q=歌外推【tg:seogoogle987】,Er6
- https://www.lneg.pt/?cat=140&s=歌外推【tg:seogoogle987】,aAx
- https://www.navylookout.com/?ID=nSBR6Ivdu6lZg1yr2SDs30Lo5sTs76yJ&s=歌外推【tg:seogoogle987】,H3A
- https://www.springfieldlibrary.org/library/?s=歌外推【tg:seogoogle987】,XPF
- https://www.toy-people.com/?s=歌外推【tg:seogoogle987】,9Ir
- https://www.remit.co.jp/zh/?s=歌外推【tg:seogoogle987】,sFD
- https://www.kingston.gov.uk/site-search/results/?q=歌外推【tg:seogoogle987】,CX0
- https://www.repetto.jp/item/search?action=QUERY&q=歌外推【tg:seogoogle987】,k3U
- https://www.tombow.com/search/歌外推【tg:seogoogle987】,OeF
- https://www.qwant.com/?q=歌外推【tg:seogoogle987】,KUi
- https://www.thealexandrian.net/?s=歌外推【tg:seogoogle987】,OhV
- https://hsmoa.com/search?query=歌外推【tg:seogoogle987】soF
- https://www.nuovabai.it/catalogsearch/result/?action=SEARCH&q=歌外推【tg:seogoogle987】,R7k
- https://www.takachi-enclosure.com/search?eqYB=gnI31Ou&text=歌外推【tg:seogoogle987】,Vis
- https://www.zutari.com/?s=歌外推【tg:seogoogle987】,caa
- https://www.vfw.org/search-results?q=歌外推【tg:seogoogle987】,l9t
- https://www.lindseymedicalsupply.com/search/歌外推【tg:seogoogle987】,M1W
- https://www.openspaceinstitute.org/search?q=歌外推【tg:seogoogle987】,Csp
- https://www.seagrant.wisc.edu/?page=63&s=歌外推【tg:seogoogle987】,giH
- https://www.ucycle.com/search/=歌外推【tg:seogoogle987】,vTq
- https://www.realsporting.com/buscador?buscar=歌外推【tg:seogoogle987】,pwI
- https://www.miamiopenmri.com/?action=CHECK&s=歌外推【tg:seogoogle987】,h3t
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=歌外推【tg:seogoogle987】,5ID
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=歌外推【tg:seogoogle987】,W3y
- https://www.mynextmove.org/find/search?s=歌外推【tg:seogoogle987】,uDN
- https://www.synonyms.com/antonyms/歌外推【tg:seogoogle987】,uDH
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=歌外推【tg:seogoogle987】,4j4