hdfs-api学习,需要导入share下的lib中的jar包,common、hdfs
jar包的依赖关系,common是最底层的
hdfs连接
Configuration conf = new Configuration();
//服务器地址端口号
conf.set("fs.defaultFS", "hdfs://192.168.31.12:9000");
try {
//连接到服务器上去
FileSystem fileSystem = FileSystem.get(conf);
//获取当前路径的状态
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/upload"));
System.out.println(fileStatus.isFile());//是否是文件
System.out.println(fileStatus.isDirectory());
System.out.println(fileStatus.getPath());//文件路径
System.out.println(fileStatus.getLen());
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
hdfs文件重命名
Configuration conf = new Configuration();
try {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.31.12:9000"), conf);
boolean rename = fileSystem.rename(new Path("/updata"), new Path("/upload"));
System.out.println(rename?"修改成功":"修改失败");
fileSystem.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/**
* 文件上传
*/