栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hadoop API

Hadoop API

1.创建maven工程

在pom.xml文件中添加如下依赖


    
        org.apache.hadoop
        hadoop-client
        3.1.3
    
    
    
        junit
        junit
        4.13.2
    
    
    
        org.slf4j
        slf4j-nop
        1.7.25
    

2.在项目的resources目录下,创建一个文件,命名为 log4j.properties ,在文中填入以下内容

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

3.API操作代码

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 {
    FileSystem fs;

    @Before
    public void into() throws URISyntaxException, IOException, InterruptedException {
        //链接集群NameNode地址
        URI uri = new URI("hdfs://hadoop:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
//        设置集群副本数量为2
//        configuration.set("dfs.replication","2");
        //创建一个用户
        String user = "root";
        fs = FileSystem.get(uri, configuration, user);
    }
    @Test
    //在HDFS上创建一个文件
    public void testMkdir() throws IOException {
        fs.mkdirs(new Path("/"));
    }

    @Test
    //将一个文件拷贝到HDFS
    public void testPut() throws IOException {
        fs.copyFromLocalFile(true, new Path("D://"), new Path("/"));
    }

    @Test
    //将一个文件下载到本地
    public void testGet() throws IOException {
        fs.copyToLocalFile(new Path("/fs.xml"), new Path("D://"));
    }
    @Test
    //删除HDFS文件
    public void testRm() throws IOException {
        fs.delete(new Path("/"));
    }
    @Test
    //文件的移动和改名
    public void testMv() throws IOException {
        //改名
        fs.rename(new Path("/"),new Path("/"));
        //移动
        fs.rename(new Path("/"),new Path("/"));
    }
    @Test
    //查看文件详情
    public void testxq() throws IOException {
        RemoteIterator listflie = fs.listFiles(new Path("/"), true);
        while(listflie.hasNext()) {
            LocatedFileStatus fileStatus = listflie.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());

            //由于返回块信息,只会返回一个地址,所以我们要对他toString()
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
        }
    }

    @Test
    //查看并判断一个目录下的文件是文件夹还是文件
    public void testfile() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));

            for(FileStatus fileStatus : fileStatuses) {
                if (fileStatus.isFile()) {
                    System.out.println("文件:"+fileStatus.getPath().getName());
                }else {
                    System.out.println("目录:"+fileStatus.getPath().getName());
                }
            }
    }

    @After
    public void close() throws IOException {
        fs.close();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号