帮我写一个 HDFS-JAVA接口之读取文件
时间: 2024-01-20 12:02:08 浏览: 28
以下是一个简单的 HDFS-JAVA 接口,用于读取 HDFS 中的文件:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FSDataInputStream;
import java.io.IOException;
public class HdfsReader {
private FileSystem fs;
private Configuration conf;
public HdfsReader(String hdfsUri) throws IOException {
conf = new Configuration();
conf.set("fs.defaultFS", hdfsUri);
fs = FileSystem.get(conf);
}
public String readFile(String filePath) throws IOException {
Path path = new Path(filePath);
if (!fs.exists(path)) {
throw new IOException("File not found: " + filePath);
}
FileStatus fileStatus = fs.getFileStatus(path);
if (fileStatus.isDirectory()) {
throw new IOException(filePath + " is a directory, not a file.");
}
byte[] buffer = new byte[1024];
StringBuilder content = new StringBuilder();
try (FSDataInputStream inputStream = fs.open(path)) {
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) > 0) {
content.append(new String(buffer, 0, bytesRead));
}
}
return content.toString();
}
public void close() throws IOException {
fs.close();
}
}
```
使用示例:
```java
HdfsReader reader = new HdfsReader("hdfs://localhost:9000");
String content = reader.readFile("/path/to/hdfs/file");
System.out.println(content);
reader.close();
```
注意:在使用之前,请确保已经配置好 Hadoop 环境,并且已经启动了 HDFS 服务。