最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)對Hadoop的操作

 更新時間:2021年07月01日 11:33:51   作者:Even710  
這篇文章主要介紹了java實(shí)現(xiàn)對Hadoop的操作,通過非常完整詳細(xì)的代碼展示了如何去進(jìn)行一系列操作,包括基本操作,文件讀寫,需要的朋友可以參考下

基本操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

@RunWith(JUnit4.class)
@DisplayName("Test using junit4")
public class HadoopClientTest {

    private FileSystem fileSystem = null;

    @BeforeEach
    public void init() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();

        configuration.set("dfs.replication", "1");
        configuration.set("dfs.blocksize", "64m");
        fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000"), configuration, "root");
    }
    /**
     * 從本地復(fù)制文件到Hadoop
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void copyFileFromLocal() throws URISyntaxException, IOException, InterruptedException {
        // 上傳文件
        fileSystem.copyFromLocalFile(new Path("C:\\Users\\Administrator\\Desktop\\win10激活.txt"), new Path("/even1"));
        // 關(guān)閉流,報(bào)錯winUtils,因?yàn)槭褂昧薼inux的tar包,如果windows要使用,則需要編譯好這個winUtils包才能使用
        fileSystem.close();
    }

    /**
     * 從Hadoop下載文件到本地,下載需要配置Hadoop環(huán)境,并添加winutils到bin目錄
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void copyFileToLocal() throws URISyntaxException, IOException, InterruptedException {
        // 下載文件
        fileSystem.copyToLocalFile(new Path("/win10激活.txt"), new Path("E:/"));
        // 關(guān)閉流,報(bào)錯winUtils,因?yàn)槭褂昧薼inux的tar包,如果windows要使用,則需要編譯好這個winUtils包才能使用
        fileSystem.close();
    }


    /**
     * 創(chuàng)建文件夾
     *
     * @throws IOException
     */
    @Test
    public void hdfsMkdir() throws IOException {
        // 調(diào)用創(chuàng)建文件夾方法
        fileSystem.mkdirs(new Path("/even1"));
        // 關(guān)閉方法
        fileSystem.close();
    }

    /**
     * 移動文件/修改文件名
     */
    public void hdfsRename() throws IOException {
        fileSystem.rename(new Path(""), new Path(""));
        fileSystem.close();
    }

    /**
     * 刪除文件/文件夾
     *
     * @throws IOException
     */
    @Test
    public void hdfsRm() throws IOException {
//        fileSystem.delete(new Path(""));
        // 第二個參數(shù)表示遞歸刪除
        fileSystem.delete(new Path(""), true);

        fileSystem.close();
    }

    /**
     * 查看hdfs指定目錄的信息
     *
     * @throws IOException
     */
    @Test
    public void hdfsLs() throws IOException {
        // 調(diào)用方法返回遠(yuǎn)程迭代器,第二個參數(shù)是把目錄文件夾內(nèi)的文件也列出來
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus locatedFileStatus = listFiles.next();

            System.out.println("文件路徑:" + locatedFileStatus.getPath());
            System.out.println("塊大?。? + locatedFileStatus.getBlockSize());
            System.out.println("文件長度:" + locatedFileStatus.getLen());
            System.out.println("副本數(shù)量:" + locatedFileStatus.getReplication());
            System.out.println("塊信息:" + Arrays.toString(locatedFileStatus.getBlockLocations()));
        }

        fileSystem.close();
    }

    /**
     * 判斷是文件還是文件夾
     */
    @Test
    public void findHdfs() throws IOException {
        // 1,展示狀態(tài)信息
        FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
        // 2,遍歷所有文件
        for (FileStatus fileStatus : listStatus) {
            if (fileStatus.isFile())
                System.out.println("是文件:" + fileStatus.getPath().getName());
            else if (fileStatus.isDirectory())
                System.out.println("是文件夾:" + fileStatus.getPath().getName());
        }

        fileSystem.close();
    }

}

文件讀寫

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@RunWith(JUnit4.class)
@DisplayName("this is read write test!")
public class HadoopReadWriteTest {
    FileSystem fileSystem = null;
    Configuration configuration = null;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // 1,加載配置
        configuration = new Configuration();
        // 2,構(gòu)建客戶端
        fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000/"), configuration, "root");
    }


    @Test
    public void testReadData() throws IOException {
        // 1,獲取hdfs文件流
        FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));
        // 2,設(shè)置一次獲取的大小
        byte[] bytes = new byte[1024];
        // 3,讀取數(shù)據(jù)
        while (open.read(bytes) != -1)
            System.out.println(Arrays.toString(bytes));

        open.close();
        fileSystem.close();
    }

    /**
     * 使用緩存流
     *
     * @throws IOException
     */
    @Test
    public void testReadData1() throws IOException {
        FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));

        // 使用緩沖流會快點(diǎn)
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open, StandardCharsets.UTF_8));

        String line = "";

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

        bufferedReader.close();
        open.close();
        fileSystem.close();
    }

    /**
     * 指定偏移量來實(shí)現(xiàn)只讀部分內(nèi)容
     */
    @Test
    public void readSomeData() throws IOException {
        FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));


        // 指定開始的index
        open.seek(14);

        // 指定讀的多少
        byte[] bytes = new byte[5];
        while (open.read(bytes) != -1)
            System.out.println(new String(bytes));

        open.close();
        fileSystem.close();

    }

    /**
     * 流方式寫數(shù)據(jù)
     * @throws IOException
     */
    @Test
    public void writeData() throws IOException {
        // 1,獲取輸出流
        FSDataOutputStream out = fileSystem.create(new Path("/win11.txt"), false);

        // 2,獲取需要寫的文件輸入流
        FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt"));

        byte[] b = new byte[1024];
        int read = 0;
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }
        in.close();
        out.close();
        fileSystem.close();
    }

    /**
     * 直接寫字符串
     */
    @Test
    public void writeData1() throws IOException {
        // 1,創(chuàng)建輸出流
        FSDataOutputStream out = fileSystem.create(new Path("/aibaobao.txt"), false);
        // 2,寫數(shù)據(jù)
        out.write("wochaoaibaobao".getBytes());
        // 3,關(guān)閉流
        IOUtils.closeStream(out);
        fileSystem.close();
    }

    /**
     * IOUtils方式上傳
     *
     * @throws IOException
     */
    @Test
    public void putToHdfs() throws IOException {
        // 1,獲取輸入流
        FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt"));
        // 2,獲取輸出流
        FSDataOutputStream out = fileSystem.create(new Path("/haddopPut.txt"), false);
        // 3,拷貝
        IOUtils.copyBytes(in, out, configuration);
        // 4,關(guān)閉流
        IOUtils.closeStream(in);
        IOUtils.closeStream(out);
        fileSystem.close();
    }

    /**
     * IOUtils方式下載
     * @throws IOException
     */
    @Test
    public void getFromHdfs() throws IOException {
        // 1,獲取輸入流
        FSDataInputStream open = fileSystem.open(new Path("/haddopPut.txt"));
        // 2,獲取輸出流
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\haddopPut.txt"));
        // 3,拷貝
        IOUtils.copyBytes(open, out, configuration);
        // 4,關(guān)閉流
        IOUtils.closeStream(open);
        IOUtils.closeStream(out);
        fileSystem.close();
    }
}

到此這篇關(guān)于java實(shí)現(xiàn)對Hadoop的操作的文章就介紹到這了,更多相關(guān)Java Hadoop內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Security實(shí)現(xiàn)用戶名密碼登錄詳解

    Spring?Security實(shí)現(xiàn)用戶名密碼登錄詳解

    這篇文章主要為大家詳細(xì)介紹了Spring Security如何實(shí)現(xiàn)用戶名密碼登錄功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-10-10
  • Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    本文我們深入探索了Java中的Synchronized關(guān)鍵字,包括其互斥性和可重入性的特性,文章詳細(xì)介紹了Synchronized的三種使用方式:修飾代碼塊、修飾普通方法和修飾靜態(tài)方法,感興趣的朋友一起看看吧
    2025-04-04
  • SpringBoot+Querydsl?框架實(shí)現(xiàn)復(fù)雜查詢解析

    SpringBoot+Querydsl?框架實(shí)現(xiàn)復(fù)雜查詢解析

    本篇主要將介紹的是利用spring query dsl框架實(shí)現(xiàn)的服務(wù)端查詢解析和實(shí)現(xiàn)介紹,對SpringBoot?Querydsl?查詢操作感興趣的朋友一起看看吧
    2022-05-05
  • Java中Volatile關(guān)鍵字能保證原子性嗎

    Java中Volatile關(guān)鍵字能保證原子性嗎

    這篇文章主要介紹了Java中Volatile關(guān)鍵字能保證原子性嗎,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Hibernate雙向一對一映射關(guān)系配置代碼實(shí)例

    Hibernate雙向一對一映射關(guān)系配置代碼實(shí)例

    這篇文章主要介紹了Hibernate雙向一對一映射關(guān)系配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 淺談Java中static和非static的區(qū)別

    淺談Java中static和非static的區(qū)別

    這篇文章主要介紹了Java中static和非static的相關(guān)內(nèi)容,小編覺得還是很不錯的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • Java設(shè)計(jì)模式之建造者模式實(shí)例詳解

    Java設(shè)計(jì)模式之建造者模式實(shí)例詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式,結(jié)合具體實(shí)例形式分析了建造者模式的概念、原理、實(shí)現(xiàn)方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • mac 安裝java1.8的過程詳解

    mac 安裝java1.8的過程詳解

    這篇文章主要介紹了mac 安裝java1.8,包括下載過程及配置環(huán)境相關(guān)知識介紹,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Java使用正則表達(dá)式檢索、替換String中特定字符和正則表達(dá)式的一切

    Java使用正則表達(dá)式檢索、替換String中特定字符和正則表達(dá)式的一切

    這篇文章主要給大家介紹了關(guān)于Java使用正則表達(dá)式檢索、替換String中特定字符和正則表達(dá)式的一切,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 淺談java線程中生產(chǎn)者與消費(fèi)者的問題

    淺談java線程中生產(chǎn)者與消費(fèi)者的問題

    下面小編就為大家?guī)硪黄獪\談java線程中生產(chǎn)者與消費(fèi)者的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07

最新評論

嘉鱼县| 仪征市| 博爱县| 东丰县| 东宁县| 胶州市| 资源县| 九龙城区| 梓潼县| 铜陵市| 仁怀市| 绥棱县| 鞍山市| 行唐县| 油尖旺区| 温宿县| 察隅县| 益阳市| 虎林市| 温泉县| 双城市| 郎溪县| 玛多县| 清镇市| 四子王旗| 保靖县| 巢湖市| 安化县| 红原县| 多伦县| 碌曲县| 潜江市| 金塔县| 永泰县| 黄平县| 绩溪县| 吉首市| 芷江| 德清县| 溆浦县| 三台县|