自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HDFS中的Java和Python API接口連接

大數(shù)據(jù)
今天進(jìn)入HDFS中的Java和Python的API操作,后面可能介紹Scala的相關(guān)的。

[[393001]]

上次介紹了HDFS的簡單操作,今天進(jìn)入HDFS中的Java和Python的API操作,后面可能介紹Scala的相關(guān)的。

在講Java API之前介紹一下使用的IDE——IntelliJ IDEA ,我本人使用的是2020.3 x64的社區(qū)版本。

Java API

創(chuàng)建maven工程,關(guān)于Maven的配置,在IDEA中,Maven下載源必須配置成阿里云。

在對(duì)應(yīng)的D:\apache-maven-3.8.1-bin\apache-maven-3.8.1\conf\settings.xml需要設(shè)置阿里云的下載源。

下面創(chuàng)建maven工程,添加常見的依賴

 

添加hadoop-client依賴,版本最好和hadoop指定的一致,并添加junit單元測試依賴。

  1. <dependencies> 
  2.   <dependency> 
  3.         <groupId>org.apache.hadoop</groupId> 
  4.         <artifactId>hadoop-common</artifactId> 
  5.         <version>3.1.4</version> 
  6.   </dependency> 
  7.   <dependency> 
  8.         <groupId>org.apache.hadoop</groupId> 
  9.         <artifactId>hadoop-hdfs</artifactId> 
  10.         <version>3.1.4</version> 
  11.   </dependency> 
  12.   <dependency> 
  13.       <groupId>org.apache.hadoop</groupId> 
  14.       <artifactId>hadoop-client</artifactId> 
  15.       <version>3.1.4</version> 
  16.   </dependency> 
  17.   <dependency> 
  18.       <groupId>junit</groupId> 
  19.       <artifactId>junit</artifactId> 
  20.       <version>4.11</version> 
  21.   </dependency> 
  22. </dependencies> 

HDFS文件上傳

在這里編寫測試類即可,新建一個(gè)java文件:main.java

這里的FileSyste一開始是本地的文件系統(tǒng),需要初始化為HDFS的文件系統(tǒng)

  1. import org.apache.hadoop.conf.Configuration; 
  2. import org.apache.hadoop.fs.FileSystem; 
  3. import org.apache.hadoop.fs.Path; 
  4. import org.junit.Test; 
  5. import java.net.URI; 
  6. public class main { 
  7.  
  8.     @Test 
  9.     public void testPut() throws Exception { 
  10.         //   獲取FileSystem類的方法有很多種,這里只寫一種(比較常用的是使URI) 
  11.         Configuration configuration = new Configuration(); 
  12.         // user是Hadoop集群的賬號(hào),連接端口默認(rèn)9000 
  13.         FileSystem fileSystem = FileSystem.get( 
  14.                 new URI("hdfs://192.168.147.128:9000"), 
  15.                 configuration, 
  16.                 "hadoop"); 
  17.         // 將f:/stopword.txt 上傳到 /user/stopword.txt 
  18.         fileSystem.copyFromLocalFile( 
  19.                 new Path("f:/stopword.txt"), new Path("/user/stopword.txt")); 
  20.         fileSystem.close(); 
  21.     } 

在對(duì)應(yīng)的HDFS中,就會(huì)看見我剛剛上傳的機(jī)器學(xué)習(xí)相關(guān)的停用詞。


HDFS文件下載

由于每次都需要初始化FileSystem,比較懶的我直接使用@Before每次加載。

HDFS文件下載的API接口是copyToLocalFile,具體代碼如下。

  1. @Test 
  2. public void testDownload() throws Exception { 
  3.     Configuration configuration = new Configuration(); 
  4.     FileSystem fileSystem = FileSystem.get( 
  5.             new URI("hdfs://192.168.147.128:9000"), 
  6.             configuration, 
  7.             "hadoop"); 
  8.     fileSystem.copyToLocalFile( 
  9.             false
  10.             new Path("/user/stopword.txt"), 
  11.             new Path("stop.txt"), 
  12.             true); 
  13.     fileSystem.close(); 
  14.     System.out.println("over"); 

Python API

下面主要介紹hdfs,參考:https://hdfscli.readthedocs.io/

我們通過命令pip install hdfs安裝hdfs庫,在使用hdfs前,使用命令hadoop fs -chmod -R 777 / 對(duì)當(dāng)前目錄及目錄下所有的文件賦予可讀可寫可執(zhí)行權(quán)限。

  1. >>> from hdfs.client import Client 
  2. >>> #2.X版本port 使用50070  3.x版本port 使用9870 
  3. >>> client = Client('http://192.168.147.128:9870')   
  4. >>> client.list('/')   #查看hdfs /下的目錄 
  5. ['hadoop-3.1.4.tar.gz'
  6. >>> client.makedirs('/test'
  7. >>> client.list('/'
  8. ['hadoop-3.1.4.tar.gz''test'
  9. >>> client.delete("/test"
  10. True 
  11. >>> client.download('/hadoop-3.1.4.tar.gz','C:\\Users\\YIUYE\\Desktop'
  12. 'C:\\Users\\YIUYE\\Desktop\\hadoop-3.1.4.tar.gz' 
  13. >>> client.upload('/','C:\\Users\\YIUYE\\Desktop\\demo.txt'
  14. >>> client.list('/'
  15. '/demo.txt' 
  16. >>> client.list('/'
  17. ['demo.txt''hadoop-3.1.4.tar.gz'
  18. >>> # 上傳demo.txt 內(nèi)容:Hello \n hdfs 
  19. >>> with client.read("/demo.txt"as reader: 
  20. ...          print(reader.read()) 
  21. b'Hello \r\nhdfs\r\n' 

相對(duì)于Java API,Python API連接實(shí)在簡單。

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2020-10-19 19:05:20

VueAxiosAPI

2013-01-08 10:01:56

HDFS

2012-07-11 17:21:23

HadoopHDFS

2014-01-02 15:30:56

PostgreSQLJava

2012-04-25 09:44:38

JavaAndroid

2012-04-24 22:08:59

Android

2011-07-10 14:07:59

JAVA

2018-11-27 09:28:41

API攻擊惡意

2021-01-14 08:16:41

Python接口編程

2024-07-26 21:55:39

RustRESTfulAPI

2009-06-16 11:30:00

Java抽象類Java接口

2009-06-14 21:31:29

Java抽象類Java接口

2011-12-22 10:48:21

Java

2021-08-24 10:42:12

IBPython腳本

2011-05-19 18:01:56

JAVA

2010-06-18 15:15:13

UML

2021-09-13 07:53:30

安全

2024-09-09 11:35:35

2010-06-07 13:35:16

Hadoop簡介

2011-07-15 15:47:02

JAVA
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)