HDFS API使用

HDFS shell API

HDFS作为大数据的文件系统,可以放置数据文件,列举几个常用的shell脚本命令,
用法和linux中的基本类似,不过这个是hadoop里的一套,所以我们要用hadoop fs开头
具体可以使用hadoop fs --help 查看帮助信息

例如:
ls
hadoop fs -ls /

mkdir
hadoop fs -mkdir -p /hdfsapi/test

touch
hadoop fs -touch /hdfsapi/test/a.txt

chmod
hadoop fs -chmod -R 777 /hdfsapi/test/a.txt

chown
hadoop fs -chown -R HADOOP:HADOOP /hdfsapi

HDFS JAVA API

先下载一个hadoop hdfs插件,github上有源码,需要自己编译。
github地址:https://github.com/fangyuzhong2016/HadoopIntellijPlugin

编译方法:

  1. 修改pom.xml中的hadoop版本信息和IntelliJ的安装目录
    macos的安装目录应该在/Applications/IntelliJ IDEA.app/Contents

  2. 先后执行如下命令

mvn clean
mvn assembly:assembly
  1. 在项目的target目录下就能找到一个zip的压缩包,这个就是插件,可以安装到idea中去
JAVA操作HDFS API

1.创建一个maven工厂

  ##pom.xml 文件配置信息如下
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.创建一个test类

 public class ApiTest {

    public static final String HDFS_PATH = "hdfs://hadoop01:8020";
    //文件系统
    FileSystem fileSystem = null;
    //配置类
    Configuration configuration = null;

    //Before适用于类加载之前
    @Before
    public void setUp() throws Exception {
        configuration = new Configuration(true);
        //拿到文件系统
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
    }

    //关闭资源用的这个
    @After
    public void tearDown() throws Exception {
        //释放资源
        configuration = null;
        fileSystem = null;
    }

    /**
     * 创建HDFS目录
     */
    @Test
    public void mkdir() throws Exception {
        fileSystem.mkdirs(new Path("/hdfsapi/test"));
    }

    /**
     * 创建文件
     */
    @Test
    public void create() throws Exception {
        FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
        output.write("hello hadoop".getBytes());
        output.flush();
        output.close();
    }

    /**
     * 查看HDFS文件上的内容
     */
    @Test
    public void cat() throws Exception {
        FSDataInputStream in = fileSystem.open(new Path("/hdfsapi/test/a.txt"));
        IOUtils.copyBytes(in, System.out, 1024);
        in.close();
    }
    
    /**
     * 重命名文件
     */
    @Test
    public void rename() throws Exception {
        Path oldPath = new Path("/hdfsapi/test/a.txt");
        Path newPath = new Path("/hdfsapi/test/b.txt");
        fileSystem.rename(oldPath, newPath);
    }

    /**
     * 上传一个文件
     *
     * @throws Exception
     */
    @Test
    public void copyFromLocalFile() throws Exception {
        Path localPath = new Path("/Users/waterair/Documents/workspace/hadoop-3.1.4-src.tar.gz");
        Path hdfsPath = new Path("/hdfsapi/test");
        fileSystem.copyFromLocalFile(localPath, hdfsPath);
    }

    /**
     * 上传一个大文件
     *
     * @throws Exception
     */
    @Test
    public void copyFromLocalBigFile() throws Exception {
        InputStream in = new BufferedInputStream(
                new FileInputStream(
                        new File("/Users/waterair/Documents/workspace/hadoopIntelliJ/target/HadoopIntellijPlugin-1.0.zip")));

        FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/all.zip"),
                new Progressable() {
                    public void progress() {
                        System.out.print(".");  //带进度提醒信息
                    }
                });


        IOUtils.copyBytes(in, output, 4096);
    }
    /**
     * 下载HDFS文件
     */
    @Test
    public void copyTOLocalFile() throws Exception{
        Path localPath = new Path("/Users/waterair/Documents/workspace/b.txt");
        Path hdfsPath = new Path("/hdfsapi/test/b.txt");
        fileSystem.copyToLocalFile(false,hdfsPath,localPath,true);
    }

    /**
     * 列举目录下所有文件信息
     * @throws Exception
     */
    @Test
    public void listFiles() throws Exception {
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/hdfsapi/test"));

        for (FileStatus fileStatus : fileStatuses) {
            String isDir = fileStatus.isDirectory() ? "文件夹" : "文件";
            //副本
            short replication = fileStatus.getReplication();
            //大小
            long len = fileStatus.getLen();
            //路径
            String path = fileStatus.getPath().toString();

            System.out.println(isDir + "\t" + replication + "\t" + len + "\t" + path);
        }
    }

    /**
     * 展示HDFS中块信息
     * @throws Exception
     */
    @Test
    public void blks() throws Exception {
        Path i = new Path("/hdfsapi/test/test.txt");
        FileStatus fileStatus = fileSystem.getFileStatus(i);
        BlockLocation[] blks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
        for (BlockLocation blk : blks) {
            System.out.println(blk);
        }
    }

    /**
     * 删除HDFS目录下所有
     * @throws Exception
     */
    @Test
    public void delete() throws Exception{
        fileSystem.delete(new Path("/hdfsapi"),true);
    }
}
posted @ 2021-03-29 20:06  Tenic  阅读(110)  评论(0)    收藏  举报