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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - HDFS的Java API的訪問方式實例代碼

HDFS的Java API的訪問方式實例代碼

2021-03-31 11:06墨梅寒香 JAVA教程

這篇文章主要介紹了HDFS的Java API的訪問方式實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文研究的主要是HDFSJava API的訪問方式,具體代碼如下所示,有詳細注釋。

最近的節奏有點兒快,等有空的時候把這個封裝一下

實現代碼

要導入的包:

?
1
2
3
4
5
6
7
8
9
10
11
12
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

實體方法:

?
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
/**
   * 獲取HDFS文件系統
   * @return
   * @throws IOException
   * @throws URISyntaxException
   */
public static FileSystem getFileSystem() throws IOException, URISyntaxException{
    //read config file
    Configuration conf = new Configuration();
    //返回默認文件系統
    //如果在Hadoop集群下運行,使用此種方法可以直接獲取默認文件系統
    //FileSystem fs = FileSystem.get(conf);
    //指定的文件系統地址
    URI uri = new URI("hdfs://hy:9000");
    //返回指定的文件系統
    //如果在本地測試,需要使用此種方法獲取文件系統
    FileSystem fs = FileSystem.get(uri, conf);
    return fs;
}
/**
   * 創建文件目錄
   * @throws Exception
   */
public static void mkdir() throws Exception{
    //獲取文件系統
    FileSystem fs = getFileSystem();
    //創建文件目錄
    fs.mkdirs(new Path("hdfs://hy:9000/hy/weibo"));
    //釋放資源
    fs.close();
}
/**
   * 刪除文件或者文件目錄
   * @throws Exception
   */
public static void rmdir() throws Exception{
    //獲取文件系統
    FileSystem fs = getFileSystem();
    //刪除文件或者文件目錄
    fs.delete(new Path("hdfs://hy:9000/hy/weibo"), true);
    //釋放資源
    fs.close();
}
/**
   * 獲取目錄下所有文件
   * @throws Exception
   */
public static void listAllFile() throws Exception{
    //獲取文件系統
    FileSystem fs = getFileSystem();
    //列出目錄內容
    FileStatus[] status = fs.listStatus(new Path("hdfs://hy:9000/hy/"));
    //獲取目錄下所有文件路徑
    Path[] listedPaths = FileUtil.stat2Paths(status);
    //循環讀取每個文件
    for (Path path : listedPaths) {
        System.out.println(path);
    }
    //釋放資源
    fs.close();
}
/**
   * 將文件上傳至HDFS
   * @throws Exception
   */
public static void copyToHDFS() throws Exception{
    //獲取文件對象
    FileSystem fs = getFileSystem();
    //源文件路徑是Linux下的路徑 Path srcPath = new Path("/home/hadoop/temp.jar");
    //如果需要在windows下測試,需要改為Windows下的路徑,比如 E://temp.jar
    Path srcPath = new Path("E://temp.jar");
    //目的路徑
    Path dstPath = new Path("hdfs://hy:9000/hy/weibo");
    //實現文件上傳
    fs.copyFromLocalFile(srcPath, dstPath);
    //釋放資源
    fs.close();
}
/**
   * 從HDFS上下載文件
   * @throws Exception
   */
public static void getFile() throws Exception{
    //獲得文件系統
    FileSystem fs = getFileSystem();
    //源文件路徑
    Path srcPath = new Path("hdfs://hy:9000/hy/weibo/temp.jar");
    //目的路徑,默認是Linux下的
    //如果在Windows下測試,需要改為Windows下的路徑,如C://User/andy/Desktop/
    Path dstPath = new Path("D://");
    //下載HDFS上的文件
    fs.copyToLocalFile(srcPath, dstPath);
    //釋放資源
    fs.close();
}
/**
   * 獲取HDFS集群點的信息
   * @throws Exception
   */
public static void getHDFSNodes() throws Exception{
    //獲取文件系統
    FileSystem fs = getFileSystem();
    //獲取分布式文件系統
    DistributedFileSystem hdfs = (DistributedFileSystem)fs;
    //獲取所有節點
    DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
    //循環比遍歷
    for (int i = 0; i < dataNodeStats.length; i++) {
        System.out.println("DataNote_" + i + "_Name:" + dataNodeStats[i].getHostName());
    }
    //釋放資源
    fs.close();
}
/**
   * 查找某個文件在HDFS集群的位置
   * @throws Exception
   */
public static void getFileLocal() throws Exception{
    //獲取文件系統
    FileSystem fs = getFileSystem();
    //文件路徑
    Path path = new Path("hdfs://hy:9000/hy/weibo/temp.jar");
    //獲取文件目錄
    FileStatus fileStatus = fs.getFileStatus(path);
    //獲取文件塊位置列表
    BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    //循環輸出塊信息
    for (int i = 0; i < blockLocations.length; i++) {
        String[] hosts = blockLocations[i].getHosts();
        System.out.println("block_" + i + "_location:" + hosts[0]);
    }
    //釋放資源
    fs.close();
}

總結

以上就是本文關于HDFS的Java API的訪問方式實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/u010176083/article/details/52464558

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 扒开女人下面使劲桶屁股动漫 | 亚洲精品在线免费观看视频 | 亚州笫一色惰网站 | 日本色播 | www四虎影视| 女子监狱第二季在线观看免费完整版 | 亚洲天堂99 | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 亚洲国产欧美在线人成 | 欧美视频黑鬼大战白妞 | 国产日韩一区二区三区 | 99九九成人免费视频精品 | 男插女的下面免费视频夜色 | 免费抽搐一进一出印度 | 好大用力深一点视频 | 色综合久久中文字幕综合网 | 国产原创一区二区 | 日本xxx在线观看免费播放 | 双子母性本能在线观看 | 亚洲免费视频在线 | 国产rpg迷雾之风冷狐破解 | 99久久精品6在线播放 | 日本韩国推理片免费观看网站 | 国产精品对白刺激久久久 | 色综合天天综合网看在线影院 | 成人精品一级毛片 | 国产欧美日韩不卡一区二区三区 | 日日碰碰 | 人人爱操| 四虎国产精品视频免费看 | 99视频久久精品久久 | 天堂a视频 | 国产资源免费 | 美国69xxxx59 | 美女尿口羞羞视频 | 天天操精品视频 | 青青国产成人久久激情91麻豆 | 亚洲精品乱码久久久久久蜜桃 | 激情乱文| 欧美一卡2卡三卡4卡5卡免费观看 | 午夜在线观看免费完整直播网页 |