首先获取文件配置
//获得文件系统对象,由于只有root用户才有写权限,因此用root用户去获取文件系统对象
创建一个txt文件,再使用IO流写入数据
最后再关闭
1. package hdfsapi; 2. 3. import java.io.BufferedWriter; 4. import java.io.IOException; 5. import java.io.OutputStreamWriter; 6. import java.net.URI; 7. 8. import org.apache.hadoop.conf.Configuration; 9. import org.apache.hadoop.fs.FSDataOutputStream; 10. import org.apache.hadoop.fs.FileSystem; 11. import org.apache.hadoop.fs.Path; 12. 13. public class E6_CreateAPI { 14. 15. public static void main(String[] args) throws IOException, InterruptedException { 16. // TODO Auto-generated method stub 17. Configuration conf =new Configuration(); 18. //conf.set("fs.defaultFS", "hdfs://master:8020"); 19. //FileSystem fs = FileSystem.get(conf); 20. FileSystem fs = FileSystem.get(URI.create("hdfs://master:8020"), conf, "root"); 21. FSDataOutputStream fin = fs.create(new Path("/user/root/hello1.txt")); 22. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fin)); 23. bw.write("Hello World!"); 24. bw.newLine(); 25. bw.write("I am a student.\n"); 26. bw.close(); 27. fin.close(); 28. fs.close(); 29. System.out.println("done..."); 30. } 31. 32. }