一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java實(shí)現(xiàn)對(duì)Hadoop的操作

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

2021-09-23 13:21Even710 Java教程

這篇文章主要介紹了java實(shí)現(xiàn)對(duì)Hadoop的操作,通過(guò)非常完整詳細(xì)的代碼展示了如何去進(jìn)行一系列操作,包括基本操作,文件讀寫(xiě),需要的朋友可以參考下

基本操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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)錯(cuò)winUtils,因?yàn)槭褂昧薼inux的tar包,如果windows要使用,則需要編譯好這個(gè)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)錯(cuò)winUtils,因?yàn)槭褂昧薼inux的tar包,如果windows要使用,則需要編譯好這個(gè)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();
    }
 
    /**
     * 移動(dòng)文件/修改文件名
     */
    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(""));
        // 第二個(gè)參數(shù)表示遞歸刪除
        fileSystem.delete(new Path(""), true);
 
        fileSystem.close();
    }
 
    /**
     * 查看hdfs指定目錄的信息
     *
     * @throws IOException
     */
    @Test
    public void hdfsLs() throws IOException {
        // 調(diào)用方法返回遠(yuǎn)程迭代器,第二個(gè)參數(shù)是把目錄文件夾內(nèi)的文件也列出來(lá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("文件長(zhǎng)度:" + 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();
    }
 
}

文件讀寫(xiě)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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)建客戶(hù)端
        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"));
 
        // 使用緩沖流會(huì)快點(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();
    }
 
    /**
     * 指定偏移量來(lái)實(shí)現(xiàn)只讀部分內(nèi)容
     */
    @Test
    public void readSomeData() throws IOException {
        FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));
 
 
        // 指定開(kāi)始的index
        open.seek(14);
 
        // 指定讀的多少
        byte[] bytes = new byte[5];
        while (open.read(bytes) != -1)
            System.out.println(new String(bytes));
 
        open.close();
        fileSystem.close();
 
    }
 
    /**
     * 流方式寫(xiě)數(shù)據(jù)
     * @throws IOException
     */
    @Test
    public void writeData() throws IOException {
        // 1,獲取輸出流
        FSDataOutputStream out = fileSystem.create(new Path("/win11.txt"), false);
 
        // 2,獲取需要寫(xiě)的文件輸入流
        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();
    }
 
    /**
     * 直接寫(xiě)字符串
     */
    @Test
    public void writeData1() throws IOException {
        // 1,創(chuàng)建輸出流
        FSDataOutputStream out = fileSystem.create(new Path("/aibaobao.txt"), false);
        // 2,寫(xiě)數(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)對(duì)Hadoop的操作的文章就介紹到這了,更多相關(guān)Java Hadoop內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/weixin_37581297/article/details/84349916

延伸 · 閱讀

精彩推薦
  • Java教程Java實(shí)現(xiàn)搶紅包功能

    Java實(shí)現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)搶紅包功能,采用多線(xiàn)程模擬多人同時(shí)搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程20個(gè)非常實(shí)用的Java程序代碼片段

    20個(gè)非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個(gè)非常實(shí)用的Java程序片段,對(duì)java開(kāi)發(fā)項(xiàng)目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程Java8中Stream使用的一個(gè)注意事項(xiàng)

    Java8中Stream使用的一個(gè)注意事項(xiàng)

    最近在工作中發(fā)現(xiàn)了對(duì)于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7472021-02-04
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程升級(jí)IDEA后Lombok不能使用的解決方法

    升級(jí)IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級(jí),尋思已經(jīng)有好久沒(méi)有升過(guò)級(jí)了。升級(jí)完畢重啟之后,突然發(fā)現(xiàn)好多錯(cuò)誤,本文就來(lái)介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程xml與Java對(duì)象的轉(zhuǎn)換詳解

    xml與Java對(duì)象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對(duì)象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
主站蜘蛛池模板: 午夜精品久久久久久久99 | 亚州日韩精品AV片无码中文 | 精品国产精品人妻久久无码五月天 | 亚洲精彩视频在线观看 | 成人中文字幕在线高清 | 国产婷婷成人久久av免费高清 | 99热精品久久| 亚洲社区在线 | 亚洲热影院 | 国产90后美女露脸在线观看 | yellow字幕网在线zmzz91 | 欧美视频黑鬼大战白妞 | caoporm碰最新免费公开视频 | sp啪啪调教打屁股网站 | tube8最近日本护士 | 国产一区二区不卡视频 | 狠狠色 | 天堂bt在线 | 秋霞一级成人欧美理论 | 国产一区二区三区在线看 | 精品国产美女AV久久久久 | 99九九成人免费视频精品 | 欧洲美女啪啪 | 四虎国产欧美成人影院 | 猫咪社区在线播放 | 欧美日韩久久中文字幕 | 国产尤物视频 | 天使萌痴汉在线中文字幕 | 国产99er66在线视频 | 天天视频官网天天视频在线 | 91视频国产一区 | 亚洲国产成人综合 | 日本老妇和子乱视频 | voyeur 中国女厕 亚洲女厕 | 4hu四虎永久网址 | 男人天堂网址 | 国产欧美日韩精品一区二区三区 | 91精品国产色综合久久 | 波多野结衣中文字幕 | www亚洲视频| 日韩中文字幕在线不卡 |