Win10?IDEA如何連接虛擬機(jī)中的Hadoop(HDFS)
獲取虛擬機(jī)的ip
虛擬機(jī)終端輸入
ip a
關(guān)閉虛擬機(jī)防火墻
sudo ufw disable
修改Hadoop的core-site.xml文件
將localhost
修改為虛擬機(jī)局域網(wǎng)IP
# 位置可能不一樣,和Hadoop安裝位置有關(guān) cd /usr/local/hadoop/etc/hadoop vim core-site.xml
重啟Hadoop
cd /usr/local/hadoop/ #目錄可能不一樣,修改成自己的目錄 ./sbin/stop-dfs.sh # 關(guān)閉hadoop ./sbin/start-dfs.sh #啟動(dòng)hadoop jps # 判斷是否啟動(dòng)成功
IDEA連接
創(chuàng)建Maven項(xiàng)目
IDEA自帶Maven,如果需要自己安裝Maven可以參考Maven安裝教程
創(chuàng)建項(xiàng)目,選擇Maven,模板選擇第一個(gè)maven-archetype-archetype
添加依賴(pom.xml)
記得修改自己hadoop
的版本,我的是3.3.5
設(shè)置好后Reload
一下
<properties> <hadoop.version>3.3.5</hadoop.version> </properties> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies>
設(shè)置好后Reload
一下 ,然后等下載好
創(chuàng)建Java文件并運(yùn)行
出現(xiàn)錯(cuò)誤請(qǐng)先檢查Hadoop是否重啟
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.IOException; public class Test01 { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); // 設(shè)置用戶名(一定要,不然默認(rèn)用戶名是win的用戶名) System.setProperty("HADOOP_USER_NAME","hadoop"); // IP地址修改成虛擬機(jī)的ip conf.set("fs.defaultFS","hdfs://192.168.111.131:9000"); conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs = FileSystem.get(conf); RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true); while (files.hasNext()) { FileStatus fileStatus = files.next(); System.out.println(fileStatus.getPath().toString()); } fs.close(); //關(guān)閉hdfs } }
端口轉(zhuǎn)發(fā)
完成到這里已經(jīng)可以用啦,不過(guò)可能不太方便
可以設(shè)置將win10的端口轉(zhuǎn)發(fā)
實(shí)現(xiàn)在代碼中直接訪問(wèn)localhost
創(chuàng)建test.bat
文件后輸入以下代碼
將IP
修改成虛擬機(jī)的IP
雙擊運(yùn)行
@REM 設(shè)置IP SET BigDataLANIP=192.168.111.131 @REM 設(shè)置命令以管理員身份運(yùn)行 %1 start "" mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit @REM 清空所有轉(zhuǎn)發(fā)規(guī)則 netsh interface portproxy reset @REM 轉(zhuǎn)發(fā)9000 netsh interface portproxy add v4tov4 listenport=9000 connectport=9000 connectaddress=%BigDataLANIP% @REM 轉(zhuǎn)發(fā)9870(HDFS的web管理界面) netsh interface portproxy add v4tov4 listenport=9870 connectport=9870 connectaddress=%BigDataLANIP% echo "succeed" timeout /t 5 /nobreak >nul
簡(jiǎn)單使用
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Test02 { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); // 設(shè)置用戶名(一定要,不然默認(rèn)用戶名是win的用戶名) System.setProperty("HADOOP_USER_NAME","hadoop"); conf.set("fs.defaultFS","hdfs://localhost:9000"); conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); // 列出根目錄下的所有文件和文件夾 FileSystem fs = FileSystem.get(conf); Path file = new Path("/"); FileStatus[] fileStatuses = fs.listStatus(file); for (FileStatus fileStatus : fileStatuses){ System.out.println(fileStatus.getPath()); } // 創(chuàng)建一個(gè)新的文件 test.txt 在HDFS的 /user/hadoop/test 目錄下(如果目錄不存在,則先創(chuàng)建目錄)。 Path dirPath = new Path("/user/hadoop/test"); if(!fs.exists(dirPath)){ fs.mkdirs(dirPath); } Path remotePath = new Path("/user/hadoop/test/test.txt"); FSDataOutputStream outputStream = fs.create(remotePath); outputStream.close(); // 向 test.txt 文件中寫入一段指定的文本內(nèi)容(如“Hello, HDFS!”)。 FSDataOutputStream outputStream2 = fs.create(remotePath); String s = "Hello, HDFS!"; outputStream2.write(s.getBytes()); outputStream2.close(); // 讀取 test.txt 文件的內(nèi)容,并打印到控制臺(tái)。 FSDataInputStream inputStream = fs.open(remotePath); BufferedReader d = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while ((line = d.readLine()) != null) System.out.println(line); // 關(guān)閉與HDFS的連接。 fs.close(); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+RabbitMQ實(shí)現(xiàn)消息可靠傳輸詳解
消息的可靠傳輸是面試必問(wèn)的問(wèn)題之一,保證消息的可靠傳輸主要在生產(chǎn)端開啟?comfirm?模式,RabbitMQ?開啟持久化,消費(fèi)端關(guān)閉自動(dòng)?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實(shí)現(xiàn)消息可靠傳輸,需要的可以參考一下2022-05-05Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線程池的特性,也可以實(shí)現(xiàn)任務(wù)循環(huán)執(zhí)行,本文主要介紹了Java ScheduledExecutorService的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05Seata?AT模式啟動(dòng)過(guò)程圖文示例詳解
這篇文章主要為大家介紹了Seata?AT模式啟動(dòng)過(guò)程圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09SpringMVC配置多個(gè)properties文件之通配符解析
這篇文章主要介紹了SpringMVC配置多個(gè)properties文件之通配符解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09基于Springboot2.3訪問(wèn)本地路徑下靜態(tài)資源的方法(解決報(bào)錯(cuò):Not allowed to load local
這篇文章主要介紹了基于Springboot2.3訪問(wèn)本地路徑下靜態(tài)資源的方法(解決報(bào)錯(cuò):Not allowed to load local resource),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08