-
import java.io.OutputStream;
-
import java.net.URI;
-
import java.net.URL;
-
-
import org.apache.commons.io.IOUtils;
-
import org.apache.hadoop.conf.Configuration;
-
import org.apache.hadoop.fs.FSDataInputStream;
-
import org.apache.hadoop.fs.FileSystem;
-
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
-
import org.apache.hadoop.fs.Path;
-
-
public class HdfsTest {
-
-
public static void main(String[] args) throws Exception {
-
write();
-
}
-
-
public static void read1(String url) throws Exception {
-
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
-
System.out.println(IOUtils.toString(new URL(url).openStream()));
-
}
-
-
public static void read2(String url) throws Exception {
-
Configuration conf = new Configuration();
-
FileSystem fs = FileSystem.get(URI.create(url), conf);
-
FSDataInputStream is = fs.open(new Path(url));
-
System.out.println(IOUtils.toString(is));
-
is.close();
-
fs.close();
-
}
-
-
public static void write() throws Exception {
-
String hdfsFile = "hdfs://192.168.202.134:9000/test/text2.txt";
-
Configuration conf = new Configuration();
-
FileSystem fs = FileSystem.get(URI.create(hdfsFile), conf, "andy");
-
OutputStream out = fs.create(new Path(hdfsFile), true);
-
out.write("new file text2.txt".getBytes());
-
out.close();
-
fs.close();
-
read1(hdfsFile);
-
read2(hdfsFile);
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10742815/viewspace-2135204/
,如需转载,请注明出处,否则将追究法律责任。