HDFS学习 Java连接hadoop

建立连接获取hadoop下的文件信息

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class ListFile {
    
    
    public static void main(String [] args) throws IOException, Exception,URISyntaxException{
    
    
        FileSystem file = FileSystem.get(new URI("hdfs://虚拟机IP地址:9000"),new Configuration(),"hadoop");//获取FileSystem对象
        RemoteIterator<LocatedFileStatus> iterator = file.listFiles(new Path("/"), true);//调用listFiles 获取 /目录下所有的文件信息
        while(iterator.hasNext()) {
    
    //遍历 / 文件夹
            LocatedFileStatus fileStatus =  iterator.next();//获取文件状态对象
            Path path = fileStatus.getPath();//获取决定路径
            String name = path.getName();//获取名字
            System.out.println(path.toString()+"       "+path.getName());
            BlockLocation[] locations = fileStatus.getBlockLocations();//获取文件块的信息
            System.out.println("block"+locations.length);//输出文件块的大小
        }

    }
}

编写hadoop连接工具类

package com.clouddisk.cloud.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSUtils {
    
    
    private static Configuration conf ;
    private static FileSystem fs ;
    private static String address = "hdfs://192.168.43.60:9000";
    public FileSystem getConf() throws URISyntaxException, IOException, InterruptedException {
    
    
        conf = new Configuration();
        fs = FileSystem.get(new URI(address),conf,"hadoop");
        return fs;
    }
    public void release() throws IOException {
    
    
        fs.close();
    }
    public String getAddress(){
    
    
        return address;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43573663/article/details/108985365