HDFS相关java API的操作Test

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.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.FileUtil;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.hdfs.DistributedFileSystem;

import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

public class javaApiTest {

    public static void main(String[] args) throws IOException, URISyntaxException {

        //mkdir();

        //copyToHDFS();

        //getFile();

        //ListAllFile();

        getFileLocal();

        //rmDir();

        getHDFSNodes();

    }

    //获取HDFS文件系统

    public static FileSystem getFileSystem() throws IOException, URISyntaxException{

        Configuration configuration=new Configuration();//读取配置文件

        //windows环境需要指定文件系统路径  返回指定的文件系统    如果在本地测试,需要使用此种方法获取文件系统

        URI uri=new URI("hdfs://dajiangtai:9000");//指定文件系统地址

        FileSystem fSystem=FileSystem.get(uri, configuration);//获取文件系统对象

        //返回默认文件系统  如果在 Hadoop集群下运行,使用此种方法可直接获取默认文件系统

        //FileSystem fSystem=FileSystem.get(configuration);//获取文件系统对象

        return fSystem;

    }

    //创建文件目录

    public  static void mkdir() throws IOException, URISyntaxException{

        FileSystem fSystem=getFileSystem();//获取文件系统

        fSystem.mkdirs(new Path("/dajiangtai/data"));//创建文件目录

        fSystem.close();//释放资源

    }

    //文件上传至HDFS

    public static void copyToHDFS() throws IOException, URISyntaxException{

        FileSystem fileSystem = getFileSystem();//返回FileSystem对象

        Path srcpath=new Path("D://大数据文件/ppt/weibo.txt");

        Path dstpath=new Path("/dajiangtai/data");

        fileSystem.copyFromLocalFile(srcpath, dstpath);

        fileSystem.close();

    }

    //从HDFS下载文件

    public static void getFile() throws IOException, URISyntaxException{

        FileSystem fileSystem=getFileSystem();

        Path srcpath = new Path("/dajiangtai/data/weibo.txt");

        Path dstpath = new Path("d://大数据文件/");

        fileSystem.copyToLocalFile(srcpath, dstpath);

        fileSystem.close();

    }

    //删除文件或文件目录

    public static void rmDir() throws IOException, URISyntaxException{

        FileSystem fileSystem = getFileSystem();

        fileSystem.delete(new Path("/dajiangtai/data"),true);

        fileSystem.close();

    }

    //获取该目录下的所有文件(不递归显示)

    public static void ListAllFile() throws IOException, URISyntaxException{

        FileSystem fileSystem = getFileSystem();

        FileStatus[] lists=fileSystem.listStatus(new Path("/middle/weather"));//列出目录内容

        Path[] stat2Paths = FileUtil.stat2Paths(lists);//获取目录下的所有文件路径   //stat2Paths中的数据:[hdfs://dajiangtai:9000/middle/weather/30yr_03103.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03812.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03813.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03816.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03820.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03822.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03856.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03860.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03870.dat, hdfs://dajiangtai:9000/middle/weather/30yr_03872.dat, hdfs://dajiangtai:9000/middle/weather/out]

        for (Path path : stat2Paths) {

            System.out.println(path);

        }

        fileSystem.close();

    }

    //查找某个文件在HDFS集群的位置

    public static void getFileLocal() throws IOException, URISyntaxException{

        FileSystem fileSystem = getFileSystem();

        Path path = new Path("/dajiangtai/data/weibo.txt");

        FileStatus fileStatus = fileSystem.getFileStatus(path);//获取文件目录

        //获取文件块位置列表

        BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());   //[0,37,dajiangtai]

        for (int i = 0; i < fileBlockLocations.length; i++) {//循环输出块信息

            String[] hosts = fileBlockLocations[i].getHosts();

            System.out.println("block_"+i+"_location:"+hosts[0]);//显示:block_0_location:dajiangtai

        }

        fileSystem.close();

    }

    //获取HDFS集群节点信息

    public static void getHDFSNodes() throws IOException, URISyntaxException{

        FileSystem fileSystem = getFileSystem();

        DistributedFileSystem hdfs=(DistributedFileSystem) fileSystem;//获取分布式文件系统

        DatanodeInfo[] datanodeInfos=hdfs.getDataNodeStats();//获取所有节点   //[192.168.80.136:50010]

        for (int i = 0; i < datanodeInfos.length; i++) {

            System.out.println("DataNode_"+i+"_Name:"+datanodeInfos[i].getHostName());  //DataNode_0_Name:dajiangtai

        }

        fileSystem.close();

    }

}

猜你喜欢

转载自blog.csdn.net/lrf2454224026/article/details/82048975