• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Hadoop API的使用

武飞扬头像
AdamShyly
帮助1

本次实验环境是在windows系统下,通过windows的hadoop依赖在IDEA进行

首先需要下载windows支持的hadoop依赖程序

学新通

以下是下载链接

winutils-1: winutils.exe hadoop.dll and hdfs.dll binaries for hadoop windows - Gitee.com 

解压后还需要配置环境变量

学新通 

 学新通


接下来就可以在 IDEA 通过 Maven 来使用 Hadoop 的API函数啦,以下是项目目录结构

学新通

 以下是 pom.xml 依赖

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
    <modelVersion>4.0.0</modelVersion>
  6.  
     
  7.  
    <groupId>com.atguigu</groupId>
  8.  
    <artifactId>HDFSClient</artifactId>
  9.  
    <version>1.0-SNAPSHOT</version>
  10.  
     
  11.  
    <properties>
  12.  
    <maven.compiler.source>8</maven.compiler.source>
  13.  
    <maven.compiler.target>8</maven.compiler.target>
  14.  
    </properties>
  15.  
     
  16.  
    <dependencies>
  17.  
    <dependency>
  18.  
    <groupId>org.apache.hadoop</groupId>
  19.  
    <artifactId>hadoop-client</artifactId>
  20.  
    <version>3.2.3</version>
  21.  
    </dependency>
  22.  
    <dependency>
  23.  
    <groupId>junit</groupId>
  24.  
    <artifactId>junit</artifactId>
  25.  
    <version>4.12</version>
  26.  
    <scope>test</scope>
  27.  
    </dependency>
  28.  
    <dependency>
  29.  
    <groupId>org.slf4j</groupId>
  30.  
    <artifactId>slf4j-log4j12</artifactId>
  31.  
    <version>1.7.36</version>
  32.  
    </dependency>
  33.  
    <dependency>
  34.  
    <groupId>org.testng</groupId>
  35.  
    <artifactId>testng</artifactId>
  36.  
    <version>RELEASE</version>
  37.  
    <scope>compile</scope>
  38.  
    </dependency>
  39.  
    <dependency>
  40.  
    <groupId>junit</groupId>
  41.  
    <artifactId>junit</artifactId>
  42.  
    <version>4.13.2</version>
  43.  
    <scope>compile</scope>
  44.  
    </dependency>
  45.  
    </dependencies>
  46.  
     
  47.  
    </project>
学新通

 以下是 log4j.properties 的配置信息

  1.  
    log4j.rootLogger=INFO, stdout
  2.  
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3.  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4.  
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
  5.  
    log4j.appender.logfile=org.apache.log4j.FileAppender
  6.  
    log4j.appender.logfile.File=target/spring.log
  7.  
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  8.  
    log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

以下是 HdfsClient.java 测试HadoopAPI函数的源码,这里要注意导包别导错了

  1.  
    package com.atguigu.hdfs;
  2.  
     
  3.  
    import org.apache.hadoop.conf.Configuration;
  4.  
    import org.apache.hadoop.fs.*;
  5.  
    import org.junit.*;
  6.  
     
  7.  
     
  8.  
    import java.io.IOException;
  9.  
    import java.lang.reflect.Array;
  10.  
    import java.net.URI;
  11.  
    import java.net.URISyntaxException;
  12.  
    import java.util.Arrays;
  13.  
     
  14.  
     
  15.  
    public class HdfsClient {
  16.  
     
  17.  
    private FileSystem fs;
  18.  
     
  19.  
    @Before
  20.  
    public void init() throws URISyntaxException, IOException, InterruptedException {
  21.  
     
  22.  
    // 连接的集群nn地址
  23.  
    URI uri = new URI("hdfs://hadoop102:8020");
  24.  
     
  25.  
    // 创建一个配置文件
  26.  
    Configuration configuration = new Configuration();
  27.  
     
  28.  
    configuration.set("dfs.replication", "2");
  29.  
     
  30.  
    // 指定用户
  31.  
    String user = "hadoop";
  32.  
     
  33.  
    // 获取客户端对象
  34.  
    fs = FileSystem.get(uri, configuration, user);
  35.  
     
  36.  
    }
  37.  
     
  38.  
    @After
  39.  
    public void close() throws IOException {
  40.  
     
  41.  
    // 关闭资源
  42.  
    fs.close();
  43.  
    }
  44.  
     
  45.  
    @Test
  46.  
    public void testMkdir() throws IOException {
  47.  
     
  48.  
    // 创建一个文件夹
  49.  
    fs.mkdirs(new Path("/xiyou/huaguoshan1"));
  50.  
     
  51.  
    }
  52.  
     
  53.  
    /**
  54.  
    * 参数优先级
  55.  
    * hdfs-default.xml => hdfs.site.xml => 在项目资源目录下的配置文件 => 代码里面的配置
  56.  
    *
  57.  
    * @throws IOException
  58.  
    */
  59.  
    // 上传
  60.  
    @Test
  61.  
    public void testPut() throws IOException {
  62.  
     
  63.  
    fs.copyFromLocalFile(false, true, new Path("D:\\sunwukong.txt"), new Path("hdfs://hadoop102/xiyou/huaguoshan"));
  64.  
     
  65.  
    }
  66.  
     
  67.  
    // 文件下载
  68.  
    @Test
  69.  
    public void testGet() throws IOException {
  70.  
     
  71.  
    fs.copyToLocalFile(true, new Path("hdfs://hadoop102/xiyou/huaguoshan"), new Path("D:\\"), true);
  72.  
     
  73.  
    }
  74.  
     
  75.  
    // 删除
  76.  
    @Test
  77.  
    public void testRm() throws IOException {
  78.  
     
  79.  
    // 删除文件
  80.  
    // fs.delete(new Path("/jdk-8u321-linux-x64.tar.gz"), false);
  81.  
     
  82.  
    // 删除空目录
  83.  
    // fs.delete(new Path("/xiyou"), false);
  84.  
     
  85.  
    // 删除非空目录
  86.  
    fs.delete(new Path("/jinguo"), true);
  87.  
     
  88.  
    }
  89.  
     
  90.  
    // 文件的更名和移动
  91.  
    @Test
  92.  
    public void testMv() throws IOException {
  93.  
     
  94.  
    // 修改文件名
  95.  
    // fs.rename(new Path("/input/word.txt"), new Path("/input/ss.txt"));
  96.  
     
  97.  
    // 移动文件
  98.  
    // fs.rename(new Path("/input/ss.txt"), new Path("/cls.txt"));
  99.  
     
  100.  
    // 目录更名
  101.  
    fs.rename(new Path("/input"), new Path("/output"));
  102.  
     
  103.  
    }
  104.  
    // 获取文件详细信息
  105.  
    @Test
  106.  
    public void fileDetail() throws IOException {
  107.  
     
  108.  
    // 获取所有文件信息
  109.  
    RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
  110.  
     
  111.  
    // 遍历文件
  112.  
    while (listFiles.hasNext()) {
  113.  
    LocatedFileStatus fileStatus = listFiles.next();
  114.  
     
  115.  
    System.out.println("========" fileStatus.getPath() "========");
  116.  
    System.out.println(fileStatus.getPermission());
  117.  
    System.out.println(fileStatus.getOwner());
  118.  
    System.out.println(fileStatus.getGroup());
  119.  
    System.out.println(fileStatus.getLen());
  120.  
    System.out.println(fileStatus.getModificationTime());
  121.  
    System.out.println(fileStatus.getReplication());
  122.  
    System.out.println(fileStatus.getBlockSize());
  123.  
    System.out.println(fileStatus.getPath().getName());
  124.  
     
  125.  
    // 获取块信息
  126.  
    BlockLocation[] blockLocations = fileStatus.getBlockLocations();
  127.  
    // 从0位置读取128M 从134217728位置读128M
  128.  
    // [0,134217728,hadoop103,hadoop104,hadoop102, 134217728,134217728,hadoop104,hadoop103,hadoop102, 268435456,134217728,hadoop104,hadoop103,hadoop102, 402653184,89588777,hadoop104,hadoop103,hadoop102]
  129.  
    System.out.println(Arrays.toString(blockLocations));
  130.  
     
  131.  
    }
  132.  
    }
  133.  
     
  134.  
    // 判断是文件夹还是文件
  135.  
    @Test
  136.  
    public void testFile() throws IOException {
  137.  
     
  138.  
    FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
  139.  
     
  140.  
    for (FileStatus status : fileStatuses) {
  141.  
    if (status.isFile()) {
  142.  
    System.out.println("文件:" status.getPath().getName());
  143.  
    } else {
  144.  
    System.out.println("目录:" status.getPath().getName());
  145.  
    }
  146.  
    }
  147.  
    }
  148.  
     
  149.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkfege
系列文章
更多 icon
同类精品
更多 icon
继续加载