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);
}
}
}