运行不通过请检查一下几个问题
- 虚拟机hadoop和hdfs是否打开
- jps命令,节点集群是否全面
- datanode节点是否缺失
- 文件已经存在报错等。
package edu.sugon;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.net.URI;
public class HDFSOpt1 {
private static final String hdfsUrl = "hdfs://192.168.10.100";
private static final String filePath = "/test/hello.txt";
private static final String filePath2 = "/test/pom.xml";
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.addResource("core-site.xml");
conf.addResource("hdfs-site.xml");
System.setProperty("user", "hadoop");
FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
Path file2 = new Path(filePath2);
// 创建文件
Path file = new Path(filePath);
if (!fs.exists(file)) {
fs.createNewFile(file);
System.out.println("Create file: " + filePath);
}
// 删除文件
if (fs.delete(file, true)) {
System.out.println("Delete file: " + filePath);
}
// 列出根目录下所有文件
FileStatus[] files = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : files) {
System.out.println("File: " + fileStatus.getPath());
}
// 上传文件
Path localPath = new Path("D:\\Hadoop\\ideaIJ\\studyspace\\hadoop\\hadoop\\hdfs\\pom.xml");
Path hdfsPath = new Path("/test/pom.xml");
fs.copyFromLocalFile(localPath, hdfsPath);
System.out.println("Uploaded file from " + localPath + " to " + hdfsPath);
// 下载文件
Path localFile = new Path("D:\\pom2.xml");
Path hdfsFile = new Path("/test/pom.xml");
fs.copyToLocalFile(hdfsFile, localFile);
System.out.println("Downloaded file from " + hdfsFile + " to " + localFile);
// 追加文件
FSDataOutputStream appendStream = fs.append(file2);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(appendStream));
writer.write("Appending new line...");
writer.newLine();
writer.close();
appendStream.close();
System.out.println("Append content to " + filePath2);
// 读文件
FSDataInputStream inputStream = fs.open(file2);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
inputStream.close();
}
}