hadoop学习笔记HDFS(二)之HDFSApi

1 基本环境:

1.1 操作系统 win10

1.2开发工具:IDEA

1.3 jar包版本  2.6.0-cdh5.15.1

2 开发前配置

2.1 配置haoop本地环境

2.1.1 下载hadoop对应版本的bin,我这里使用的是hadoop2.7.6版本;使用一切正常。

可以到这里下载:https://pan.baidu.com/s/1d5fDT6IdjAuKI4ZxbX8xRQ 提取码 m3mq

2.1.2 配置HADOOP_HOME

2.1.3 将%HADOOP_HOME%\bin添加到path变量中

2.1.4 将%HADOOP_HOME%\bin\winutils.exe 添加到CLASS_PATH中。

2.1.5 如果运行还报错可以尝试将bin下的hadoop.dll拷贝至C:\Windows\System32

2.2 使用的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.imooc.big.data</groupId>
    <artifactId>hadoop-train</artifactId>
    <version>1.0</version>
    <properties>
        <java.version>1.8</java.version>
        <hadoop.version>2.6.0-cdh5.15.1</hadoop.version>
    </properties>

    <repositories>
        <!--cdh 仓库-->
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>
    </repositories>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <!--hadoop 依赖包-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>

</project>

3 一个java测试文件

package com.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

/**
 * Created by 86186 on 2019/9/21.
 */
public class HDFSApp {


    public static void main(String[] args)throws  Exception {
        Configuration configuration =new Configuration();
        FileSystem fileSystem = FileSystem.
                get(new URI("hdfs://192.168.0.120:8020"),configuration,"hadoop");
        Path path =new Path("/hdfsapi2/test");
        boolean result=fileSystem.mkdirs(path);
        System.out.println(">>>>result>>>>>:"+result);
    }
}

3.1 这里主要涉及两个类

3.1.1 Configuration.java 用于配置hdfs系统的一些信息;这里采用默认值;

3.1.2 FileSystem.java 用户操作hdfs文件系统的主类;里面有许多方法用于实现java api操作hdfs文件系统;

 

3.2 查看结果

3.2.1 输出结果

>>>>result>>>>>:true

3.2.2 客户端

4 一个单元测试用例:

package com.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;

/**
 * 使用java API 操作HDFS文件系统
 *  1 创建Configuration
 *  2 获取FileSysrem
 *  3 HDFS API的操作
 */
public class HDFSAppTest {
    public final  static String URL="hdfs://192.168.0.120:8020";// hdfs 连接ip:port
    public final static String USER="hadoop"; hdfs 连接用户
    Configuration configuration=null;
    FileSystem fileSystem = null;

    /**
     * 初始化
     * @throws Exception
     */
    @Before
    public void fnInitial()throws  Exception{
        System.out.println(">>>>>fnInitial()方法执行力>>>>>");
        configuration =new Configuration();
        configuration.set("dfs.replication","1");//测试阶段 设置1个副本
        /**
         * 三个参数
         * 1 hdfs访问 URI
         * 2 客户端配置参数
         * 3 用户名
         */
        fileSystem = FileSystem.
                get(new URI(URL),configuration,USER);
    }

    /**
     * 资源释放
     */
    @After
    public void fnRelease(){
        configuration=null;
        fileSystem=null;
        System.out.println(">>>>>>>>>fnRelease<<<<<<<<");
    }

    /**
     * 查看副本数
     */
    @Test
    public void test(){
        String value= configuration.get("dfs.replication");
        System.out.println("value:"+value);
    }

    /**
     * 创建文件
     * @throws Exception
     */
    @Test
    public void mkdir()throws  Exception{
        System.out.println("重命名");
        fileSystem.mkdirs(new Path("/hdfsapit"));
    }

    /**
     * 本地文件复制到hdfs
     * @throws Exception
     */
    @Test
    public void copyFromLocal()throws  Exception{
        System.out.println("copyFromLocal");
        fileSystem.copyFromLocalFile(new Path("D:\\F\\aa.txt"),new Path("/hdfsapit/test/aaa.txt"));
    }

    /**
     * 重命名
     * @throws Exception
     */
    @Test
    public void rename()throws  Exception{
        System.out.println("重命名");
        fileSystem.rename(new Path("/hdfsapit/test/aaa.txt"),new Path("/hdfsapit/test/a.txt"));
    }

    /**
     * 带进度条上传文件到hdfs
     * @throws Exception
     */
    @Test
    public void copyFromLocalPro()throws  Exception{
        System.out.println("进度条");
        InputStream in =new BufferedInputStream(new FileInputStream(new File("D:\\F\\sssh.7z")));
        FSDataOutputStream out =fileSystem.create(new Path("/hdfsapit/test/sssh.7z"), new Progressable() {
            public void progress() {
                this.toString();
                System.out.println("当前进度》。");
            }
        });
        IOUtils.copyBytes(in,out,4096);

    }

    /**
     * 从hdfs 到本地
     * @throws Exception
     */
    @Test
    public void copyToLocal()throws  Exception{
        System.out.println("从hdfs拷贝到本地");
        fileSystem.copyToLocalFile(new Path("/hdfsapit/test/a.txt"),new Path("D:\\F/"));
    }

    /**
     * 删除hdfs文件
     * @throws Exception
     */
    @Test
    public void delete()throws  Exception {
      boolean isDel= fileSystem.delete(new Path("/hdfsapit/test/a.txt"),true);
      System.out.println("del>>>>>>:"+isDel);
    }

    /**
     * 获取文件块存储位置信息
     * @throws Exception
     */
    @Test
    public void getFileBlockLocations()throws  Exception{
        FileStatus fileStatus =fileSystem.getFileStatus(new Path("/hdfsapit/test/sssh.7z"));
         BlockLocation[] blockLocations= fileSystem.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
        for(BlockLocation block:blockLocations){
            System.out.println(">>>>>>block:>>>>>>"+block.toString());
        }
    }



    /**
     * 查看目标文件夹下所有文件--递归
     * @throws Exception
     */
    @Test
    public void listFileRecur()throws  Exception{
        RemoteIterator<LocatedFileStatus> listFiles=fileSystem.listFiles(new Path("/hdfsapit"),true);
        while (listFiles.hasNext()){
            LocatedFileStatus status=listFiles.next();
            System.out.println(">>>>>status>>>>>>>:"+status.toString());
        }
    }


    /**
     * 查看目标文件夹下所有文件
     * @throws Exception
     */
    @Test
    public void listFile()throws  Exception{
        System.out.println("查看目标文件夹:");
        FileStatus[] fileStatuses= fileSystem.listStatus(new Path("/hdfsapit/test"));
        for(FileStatus status:fileStatuses){
            System.out.println("status:"+status.toString());
        }

    }

    /**
     * 创建文件并写入内容
     * @throws Exception
     */
    @Test
    public void create()throws  Exception{
        System.out.println("创建text");
        FSDataOutputStream out= fileSystem.create(new Path("/hdfsapit/test/b.txt"));
        out.writeUTF("hello");
        out.flush();
        out.close();

    }

    /**
     * 查看HDFS内容
     * @throws Exception
     */
    @Test
    public void text()throws  Exception{
        System.out.println("查看");
        FSDataInputStream in = fileSystem.open(new Path("/hdfsapit/test/b.txt"));
        IOUtils.copyBytes(in,System.out,10240);
        System.out.println(in);
    }

}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值