昨天我们用java实现了上传,重命名,创建,删除等操作。
今天我们用java实现查看文件的一些属性。
package hdfs.doit;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class hadoop2 {
FileSystem hdfsClient = null;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
//创建一个hdfs的客户端
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.72.110:9000");
//获取root权限
hdfsClient = FileSystem.get(new URI("hdfs://192.168.72.110:9000"), conf, "root");
}
@Test
public void TextLs() throws IOException {
//返回的是迭代器
RemoteIterator<LocatedFileStatus> listFiles = hdfsClient.listFiles(new Path("/"), true);
// listFiles.hasNext()返回的是boolean,没元素就退出了
while (listFiles.hasNext()){
//用next()方法一个一个拿取listFiles里面的元素
LocatedFileStatus file = listFiles.next();
System.out.println(file.getPath());//返回路径 hdfs://192.168.72.110:9000/aa/a.sh
System.out.println(file.getAccessTime());//访问时间
System.out.println(file.getBlockSize());//返回块的大小 我这里每块是128MB
System.out.println(file.getGroup());//string型 用户组
System.out.println(file.getLen());//long 文件大小
System.out.println(file.getModificationTime());//long 最后修改时间
System.out.println(file.getOwner());//string 用户名
System.out.println(file.getReplication());//short 副本数
System.out.println(file.getPermission());//LocatedFileStatus 权限
BlockLocation[] blockLocations = file.getBlockLocations();
for (BlockLocation block:blockLocations) {
String[] hosts = block.getHosts();
System.out.println("----------------");
System.out.println(Arrays.toString(hosts));
System.out.println(block.getLength());//文件大小 与上面的file.getLen()相同
System.out.println(Arrays.toString(block.getNames()));
System.out.println(block.getOffset());//long 在分块时,第二块的起始点。 (上一个块的末大小)
System.out.println("-----------------");
}
}