Hadoop文件读取及文件上传

文件操作

上传

1
2
3
hadoop fs -put localfile /user/hadoop/hadoopfile
hadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdir
hadoop fs -put localfile hdfs://host:port/hadoop/hadoopfile

具体示例

方式1

1
hadoop fs -put /root/zjhome/test.json hdfs://hacluster/zjhome/test.json

其中集群名可以通过下面的地址查看

http://hadoop02:50070/dfshealth.html#tab-overview

image-20230915174128298

不要使用

1
hadoop fs -put /root/zjhome/test.json hdfs://hadoop02:9000/zjhome/test.json

其中端口是在hdfs-site.xml中的dfs.namenode.rpc-address配置的端口。

这种方式只会找对应的服务器上找,如上只会从hadoop02上找,如果hadoop02不是激活状态则无法上传。

方式2

1
hadoop fs -put /root/zjhome/test.json /zjhome/test.json

注意如果父级目录没有创建,需要先创建。

查看文件列表

1
hadoop fs -ls /

创建目录

1
hadoop fs -mkdir -p /zjhome/

这里的-p选项会创建所有必要的父目录,如果它们不存在的话。

查看文件

1
2
3
hadoop fs -cat /zjhome/test.json

hadoop fs -cat hdfs://hac/data/files/taskjson/1714207200177672193.json

下载文件

1
hadoop fs -get /zjhome/test.json

文件读取

工具类

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
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ReadHDFSFile {

public static String getHadoopConfigRootPath() {
String conf = System.getenv("HADOOP_CONF_DIR");
if (conf == null) {
String hh = System.getenv("HADOOP_HOME");
if (hh == null) {
throw new RuntimeException("找不到配置文件");
}
conf = hh + "/etc/hadoop";
}
return conf;
}

public static Configuration loadHDFS() throws RuntimeException {
String conf = getHadoopConfigRootPath();
Configuration config = new Configuration();
config.addResource(new Path(conf + "/core-site.xml"));
config.addResource(new Path(conf + "/hdfs-site.xml"));
return config;
}

public static String getStrByPath(String hdfsFilePath) {
try {
Configuration conf = loadHDFS();
FileSystem fs = FileSystem.get(conf);
Path filePath = new Path(hdfsFilePath);

if (fs.exists(filePath)) {
FSDataInputStream inputStream = fs.open(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder content = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}

reader.close();
inputStream.close();
fs.close();
return content.toString();
} else {
System.out.println("文件不存在:" + hdfsFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}

return "";
}

public static void main(String[] args) {
String hdfsFilePath = "hdfs://hacluster/zjhome/test.json";
String fileContent = getStrByPath(hdfsFilePath);
System.out.println("文件内容:\n" + fileContent);
}
}

注意

要读取服务器上的Hadoop配置,否则无法找到文件。

依赖的Jar

1
2
3
4
5
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version> <!-- 使用与你的Hadoop集群版本匹配的版本 -->
</dependency>