用java向hdfs中写文件
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
public class testCreate {
public static void main(String[] args)
throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem=FileSystem.get(
new URI("hdfs://172.0.0.1:9000"),
new Configuration(),"root"
);
try {
OutputStream output = fileSystem.create(new Path("/user/test/a.txt"));
output.write("1\t1".getBytes("UTF-8"));
output.write("\n".getBytes("UTF-8"));
output.flush()
fileSystem.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
追加写入
Stream output = fileSystem.append(new Path("/user/test/a.txt"));