<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.8.0</version> </dependency>
/**
* 上传文件到hdfs上
*/
@Test
public void upload() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hzq:9000");
FileSystem fs = FileSystem.get(conf);
fs.copyFromLocalFile(new Path("/home/hzq/jdk1.8.tar.gz"),new Path("/demo"));
}
/**
* 将hdfs上文件下载到本地
*/
@Test
public void download() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hzq:9000");
FileSystem fs = FileSystem.newInstance(conf);
fs.copyToLocalFile(new Path("/java/jdk1.8.tar.gz"),new Path("/home/hzq/"));
}
/**
* 删除hdfs上的文件
* @throws IOException
*/
@Test
public void removeFile() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hzq:9000");
FileSystem fs = FileSystem.newInstance(conf);
fs.delete(new Path("/demo/jdk1.8.tar.gz"),true);
}
/**
* 在hdfs更目录下面创建test1文件夹
* @throws IOException
*/
@Test
public void mkdir() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hzq:9000");
FileSystem fs = FileSystem.newInstance(conf);
fs.mkdirs(new Path("/test1"));
}
@Test
public void listFiles() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hzq:9000");
FileSystem fs = FileSystem.newInstance(conf);
// true 表示递归查找 false 不进行递归查找
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
while (iterator.hasNext()){
LocatedFileStatus next = iterator.next();
System.out.println(next.getPath());
}
System.out.println("----------------------------------------------------------");
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (int i = 0; i < fileStatuses.length; i++) {
FileStatus fileStatus = fileStatuses[i];
System.out.println(fileStatus.getPath());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有