• Hadoop——API操作


    代码示例:

    package com.atguigu.hdfs;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.junit.After;
    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 HdfsClient {
        private FileSystem fs;
        @Before
        public void  init() throws URISyntaxException, IOException, InterruptedException {
            URI uri=new URI("hdfs://master:9000");
            Configuration configuration=new Configuration();
            String user="root";
            fs=FileSystem.get(uri,configuration,user);
        }
        @After
        public void close() throws IOException {
            // 3 关闭资源
            fs.close();
        }
        //创建目录
        @Test
        public void testmkdir() throws IOException, URISyntaxException, InterruptedException {
    
            fs.mkdirs(new Path("/homework"));
        }
        //上传
        @Test
        public void testPut() throws IOException {
            //
            fs.copyFromLocalFile(true,true,new Path("D:\sunwukong.txt"),new Path("/xiyou/huaguoshan"));
        }
        @Test
        public void testListFiles() throws IOException, InterruptedException,
                URISyntaxException {
    // 1 获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"),
                    configuration, "root");
    // 2 获取文件详情
            RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),
                    true);
            while (listFiles.hasNext()) {
                LocatedFileStatus fileStatus = listFiles.next();
                System.out.println("========" + fileStatus.getPath() + "=========");
                System.out.println(fileStatus.getPermission());
                System.out.println(fileStatus.getOwner());
                System.out.println(fileStatus.getGroup());
                System.out.println(fileStatus.getLen());
                System.out.println(fileStatus.getModificationTime());
                System.out.println(fileStatus.getReplication());
                System.out.println(fileStatus.getBlockSize());
                System.out.println(fileStatus.getPath().getName());
    // 获取块信息
                BlockLocation[] blockLocations = fileStatus.getBlockLocations();
                System.out.println(Arrays.toString(blockLocations));
            }
    // 3 关闭资源
            fs.close();
        }
    
    }

    运行截图:

  • 相关阅读:
    arthas-常用命令
    k8s-容器技术-Mount Namespace
    k8s-statefulset介绍
    k8s-yaml配置文件
    k8s-控制器模式
    k8s-pod使用
    k8s-pod简介(半原创)
    k8s-安装我们第一个集群
    k8s-安装
    Corn表达式详解(转)
  • 原文地址:https://www.cnblogs.com/zyj3955/p/15313922.html
Copyright © 2020-2023  润新知