Java API操作HDFS
public class TestMkdir {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ip:9000") ,conf);
fs.mkdirs(new Path("/demo1"));
fs.close();
}
}
public class TestPut {
public static void main(String[] args) throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ip:9000"),configuration);
fs.copyFromLocalFile(new Path("文件路径(eclipse所在的文件系统)"),new Path("/demo1"));
fs.close();
}
}
public class TestGet {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ip:9000"),conf);
fs.copyToLocalFile(new Path("文件路径(eclipse所在的文件系统)"), new Path("/home"));
fs.close();
}
}
public class TestDelete {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ip:9000"),conf);
fs.delete(new Path("/demo1"), true);
fs.close();
}
}
public class Testls {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://if:9000"),conf);
FileStatus[] fileStatus = fs.listStatus(new Path("/"));
for(FileStatus f : fileStatus){
Path path = f.getPath();
String str = path.getName();
System.out.println(str);
}
}
}
本文详细介绍了使用Java API进行HDFS文件系统操作的方法,包括创建目录、上传和下载文件、删除文件以及查询目录内容等核心功能,为Hadoop开发者提供实用的代码示例。
1995

被折叠的 条评论
为什么被折叠?



