1 创建文件
private static String uri = "hdfs://192.168.174.130:8020/"; // hdfs 地址
private static String local = "C:/Users/Administrator/Desktop/333.txt"; // 本地路径
private static String local1 = "C:/Users/Administrator/Desktop/hdfs";
private static String remote = "/user/test1";
private static Configuration conf = new Configuration();
// 创建一个目录
public static void mkdir() throws Exception {
/*
* Configuration 用户加载配置文件
* URI全称为Uniform Resource Identifier,统一资源标识符,作用是标识某一互联网资源。
*/
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.131.100:8020"), new
Configuration(), "sheng"); // 创建一个文件夹,这里从HDFS的根目录写起的.
Boolean flag = fs.mkdirs(new Path("/user/test")); fs.close();
System.out.println(flag);
}
2 删除一个文件或者目录
public static void delete() throws Exception {
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.174.130:8020"), new Configuration(), "zx");
// 创建一个文件夹,这里从HDFS的根目录写起的.
Boolean flag = fs.delete(new Path("/user/test"));
fs.close();
System.out.println(flag);
}
3 修改文件或者目录
public static void update() throws Exception {
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.174.130:8020"), new Configuration(), "zx");
// 创建一个文件夹,这里从HDFS的根目录写起的.
Boolean flag = fs.rename(new Path("/user/test1"), new Path("/user/test3"));
fs.close();
System.out.println(flag);
}
4 上传文件
public static void go() throws Exception {
FileSystem fs = FileSystem.get(URI.create(uri), conf);
fs.copyFromLocalFile(new Path(local), new Path(remote));
System.out.println("copy from: " + local + " to " + remote);
fs.close();
}
5 下载文件
public static void get() throws Exception {
Path path = new Path(remote);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
fs.copyToLocalFile(path, new Path(local1));
System.out.println("download: from" + remote + " to " + local1);
fs.close();
}
6 查看文件内容
public static void cat() throws Exception {
/*
* IOUtils.copyBytes(in, out, 4096, false)
* –in:是FSDataInputStream类的对象,是有关读取文件的类,也就是所谓“输入流”
* –out:是FSDataOutputStream类的对象,是有关文件写入的类,也就是“输出流”
* –4096表示用来拷贝的buffer大小(buffer是缓冲区)–缓冲区大小 –// true -
* 是否关闭数据流,如果是false,就在finally里关掉。IOUtils.closeStream(in);
*
*
*/
String path1 = "/user/test1/333.txt";
Path path = new Path(path1);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
FSDataInputStream fsdis = null;
System.out.println("cat: " + path1);
try {
fsdis = fs.open(path);
IOUtils.copyBytes(fsdis, System.out, 4096, false);
} finally {
IOUtils.closeStream(fsdis);
fs.close();
}
}