HDFS的Java API的访问方式实例代码

2025-05-27 0 77

本文研究的主要是HDFS的Java 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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 HDFS的Java API的访问方式实例代码 https://www.kuaiidc.com/76160.html

相关文章

发表评论
暂无评论