這篇文章給大家分享的是有關(guān)java如何使用IO流的方式實(shí)現(xiàn)hdfs數(shù)據(jù)的上傳和下載的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
目前成都創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、白銀網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
IO操作-文件上傳
1、把本地d盤(pán)下的hello.txt文件上傳到HDFS根目錄
2、代碼編寫(xiě)
@Test public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException { // 1 獲取文件系統(tǒng) Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root"); // 2 創(chuàng)建輸入流 FileInputStream fis = new FileInputStream(new File("d:/hello.txt")); // 3 獲取輸出流 FSDataOutputStream fos = fs.create(new Path("/hello.txt")); // 4 流對(duì)拷 IOUtils.copyBytes(fis, fos, configuration); // 5 關(guān)閉資源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close(); }
3、瀏覽器輸入http://hadoop100:50070/explorer.html#/查看是否上傳
IO操作-文件下載
1、從HDFS下載hello.txt文件到d盤(pán)根目錄下
2、代碼編寫(xiě)
@Test public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException { // 1 獲取文件系統(tǒng) Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root"); // 2 獲取輸入流 FSDataInputStream fis = fs.open(new Path("/hello.txt")); // 3 獲取輸出流 FileOutputStream fos = new FileOutputStream(new File("d:/hello.txt")); // 4 流的對(duì)拷 IOUtils.copyBytes(fis, fos, configuration); // 5 關(guān)閉資源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close(); }
3、查看目錄下是否有文件
IO操作-分塊讀取
1、分塊讀取HDFS上的大文件,比如根目錄下的/hadoop-2.7.2.tar.gz
2、下載第一塊,代碼如下
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 獲取輸入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));
// 3 創(chuàng)建輸出流
FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part1"));
// 4 流的拷貝
byte[] buf = new byte[1024];
for(int i =0 ; i < 1024 * 128; i++){
fis.read(buf);
fos.write(buf);
}
// 5關(guān)閉資源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
}
3、下載第二塊,代碼如下
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");
// 2 打開(kāi)輸入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));
// 3 定位輸入數(shù)據(jù)位置
fis.seek(1024*1024*128);
// 4 創(chuàng)建輸出流
FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part2"));
// 5 流的對(duì)拷
IOUtils.copyBytes(fis, fos, configuration);
// 6 關(guān)閉資源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
4、合并文件,把兩塊文件合并為一個(gè)
在Window命令窗口中進(jìn)入到目錄E:\,然后執(zhí)行如下命令,對(duì)數(shù)據(jù)進(jìn)行合并
type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1,合并完成后,將hadoop-2.7.2.tar.gz.part1重新命名為hadoop-2.7.2.tar.gz。解壓發(fā)現(xiàn)該tar包非常完整。
感謝各位的閱讀!關(guān)于“java如何使用IO流的方式實(shí)現(xiàn)hdfs數(shù)據(jù)的上傳和下載”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文標(biāo)題:java如何使用IO流的方式實(shí)現(xiàn)hdfs數(shù)據(jù)的上傳和下載
網(wǎng)站網(wǎng)址:http://www.muchs.cn/article6/phopog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、ChatGPT、Google、軟件開(kāi)發(fā)、微信公眾號(hào)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)