注意点:
9000 端口是 hdfs 文件系统的默认端口, 而 8020 才是 NameNode 的端口.
API 基本规律:
1: 首先创建 configuration 对象.
2: 获取 FileSystem 对象后, 绝大部分 对文件系统的操作都在这个对象中, 如 create, mkdir, delete 等等.
package hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
/**
* @author djh
* @date 2020/2/23 15:56
* @description 描述信息
*/
public class UploadFile {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs://hadoop4-host:8020");
FileSystem fileSystem = FileSystem.get(conf);
uploadFile(fileSystem);
}
private static void createFile(FileSystem fileSystem) throws IOException {
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/test.txt"));
fsDataOutputStream.write("This is data".getBytes());
fsDataOutputStream.close();
}
private static void uploadFile(FileSystem fileSystem) throws IOException {
fileSystem.copyFromLocalFile(new Path("/Users/ideamake/MyAllFiles/BigDataProject/GraduationDesign/src/main/resources/data/translate-user-profile.txt"), new Path("/GD/origin_data/user_profile_data.txt"));
}
}