利用Java连接Hadoop进行编程

作者:wr456wr 时间:2022-11-12 09:02:12 

实验环境

  • hadoop版本:3.3.2

  • jdk版本:1.8

  • hadoop安装系统:ubuntu18.04

  • 编程环境:IDEA

  • 编程主机:windows

实验内容

测试Java远程连接hadoop

创建maven工程,引入以下依赖:

<dependency>
               <groupId>org.testng</groupId>
               <artifactId>testng</artifactId>
               <version>RELEASE</version>
               <scope>compile</scope>
           </dependency>
           <dependency>
               <groupId>org.apache.hadoop</groupId>
               <artifactId>hadoop-common</artifactId>
           </dependency>
           <dependency>
               <groupId>org.apache.hadoop</groupId>
               <artifactId>hadoop-hdfs</artifactId>
               <version>3.3.2</version>
           </dependency>
           <dependency>
               <groupId>org.apache.hadoop</groupId>
               <artifactId>hadoop-common</artifactId>
               <version>3.3.2</version>
           </dependency>
           <dependency>
               <groupId>org.apache.hadoop</groupId>
               <artifactId>hadoop-core</artifactId>
               <version>1.2.1</version>
           </dependency>

虚拟机的/etc/hosts配置

利用Java连接Hadoop进行编程

hdfs-site.xml配置

<configuration>
       <property>
               <name>dfs.replication</name>
               <value>1</value>
       </property>
       <property>
               <name>dfs.namenode.name.dir</name>
               <value>file:/root/rDesk/hadoop-3.3.2/tmp/dfs/name</value>
       </property>
       <property>
               <name>dfs.datanode.http.address</name>
               <value>VM-12-11-ubuntu:50010</value>
       </property>
       <property>
               <name>dfs.client.use.datanode.hostname</name>
               <value>true</value>
       </property>
       <property>
               <name>dfs.datanode.data.dir</name>
               <value>file:/root/rDesk/hadoop-3.3.2/tmp/dfs/data</value>
       </property>
</configuration>

利用Java连接Hadoop进行编程

core-site.xml配置

<configuration>
 <property>
         <name>hadoop.tmp.dir</name>
         <value>file:/root/rDesk/hadoop-3.3.2/tmp</value>
         <description>Abase for other temporary directories.</description>
 </property>
 <property>
         <name>fs.defaultFS</name>
         <value>hdfs://VM-12-11-ubuntu:9000</value>
 </property>
</configuration>

利用Java连接Hadoop进行编程

启动hadoop

sbin/start-dfs.sh

主机的hosts(C:\Windows\System32\drivers\etc)文件配置

利用Java连接Hadoop进行编程

尝试连接到虚拟机的hadoop并读取文件内容,这里我读取hdfs下的/root/iinput文件内容

利用Java连接Hadoop进行编程

Java代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
public class TestConnectHadoop {
   public static void main(String[] args) throws Exception {

String hostname = "VM-12-11-ubuntu";
       String HDFS_PATH = "hdfs://" + hostname + ":9000";
       Configuration conf = new Configuration();
       conf.set("fs.defaultFS", HDFS_PATH);
       conf.set("fs.hdfs.impl", DistributedFileSystem.class.getName());
       conf.set("dfs.client.use.datanode.hostname", "true");

FileSystem fs = FileSystem.get(conf);
       FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
       for (FileStatus fileStatus : fileStatuses) {
           System.out.println(fileStatus.toString());
       }
       FileStatus fileStatus = fs.getFileStatus(new Path("/root/iinput"));
       System.out.println(fileStatus.getOwner());
       System.out.println(fileStatus.getGroup());

System.out.println(fileStatus.getPath());
       FSDataInputStream open = fs.open(fileStatus.getPath());
       byte[] buf = new byte[1024];
       int n = -1;
       StringBuilder sb = new StringBuilder();
       while ((n = open.read(buf)) > 0) {
           sb.append(new String(buf, 0, n));
       }
       System.out.println(sb);
   }
}

运行结果:

利用Java连接Hadoop进行编程

编程实现一个类&ldquo;MyFSDataInputStream&rdquo;,该类继承&ldquo;org.apache.hadoop.fs.FSDataInputStream",要求如下: ①实现按行读取HDFS中指定文件的方法&rdquo;readLine()&ldquo;,如果读到文件末尾,则返回为空,否则返回文件一行的文本

思路:emmm我的思路比较简单,只适用于该要求,仅作参考。
将所有的数据读取出来存储起来,然后根据换行符进行拆分,将拆分的字符串数组存储起来,用于readline返回

Java代码

import org.apache.hadoop.fs.FSDataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class MyFSDataInputStream extends FSDataInputStream {
   private String data = null;
   private String[] lines = null;
   private int count = 0;
   private FSDataInputStream in;
   public MyFSDataInputStream(InputStream in) throws IOException {
       super(in);
       this.in = (FSDataInputStream) in;
       init();
   }
   private void init() throws IOException {
       byte[] buf = new byte[1024];
       int n = -1;
       StringBuilder sb = new StringBuilder();
       while ((n = this.in.read(buf)) > 0) {
           sb.append(new String(buf, 0, n));
       }
       data = sb.toString();
       lines = data.split("\n");
   }
   /**
    * 实现按行读取HDFS中指定文件的方法”readLine()“,如果读到文件末尾,则返回为空,否则返回文件一行的文本
    */
   public String read_line() {
       return count < lines.length ? lines[count++] : null;
   }

}

测试类:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
public class TestConnectHadoop {
   public static void main(String[] args) throws Exception {

String hostname = "VM-12-11-ubuntu";
       String HDFS_PATH = "hdfs://" + hostname + ":9000";
       Configuration conf = new Configuration();
       conf.set("fs.defaultFS", HDFS_PATH);
       conf.set("fs.hdfs.impl", DistributedFileSystem.class.getName());
       conf.set("dfs.client.use.datanode.hostname", "true");
       FileSystem fs = FileSystem.get(conf);
       FileStatus fileStatus = fs.getFileStatus(new Path("/root/iinput"));
       System.out.println(fileStatus.getOwner());
       System.out.println(fileStatus.getGroup());
       System.out.println(fileStatus.getPath());
       FSDataInputStream open = fs.open(fileStatus.getPath());
       MyFSDataInputStream myFSDataInputStream = new MyFSDataInputStream(open);
       String line = null;
       int count = 0;
       while ((line = myFSDataInputStream.read_line()) != null ) {
           System.out.printf("line %d is: %s\n", count++, line);
       }
       System.out.println("end");

}
}

运行结果:

利用Java连接Hadoop进行编程

②实现缓存功能,即利用&rdquo;MyFSDataInputStream&ldquo;读取若干字节数据时,首先查找缓存,如果缓存中有所需要数据,则直接由缓存提供,否则从HDFS中读取数据

import org.apache.hadoop.fs.FSDataInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class MyFSDataInputStream extends FSDataInputStream {
   private BufferedInputStream buffer;
   private String[] lines = null;
   private int count = 0;
   private FSDataInputStream in;
   public MyFSDataInputStream(InputStream in) throws IOException {
       super(in);
       this.in = (FSDataInputStream) in;
       init();
   }
   private void init() throws IOException {
       byte[] buf = new byte[1024];
       int n = -1;
       StringBuilder sb = new StringBuilder();
       while ((n = this.in.read(buf)) > 0) {
           sb.append(new String(buf, 0, n));
       }
       //缓存数据读取
       buffer = new BufferedInputStream(this.in);
       lines = sb.toString().split("\n");
   }
   /**
    * 实现按行读取HDFS中指定文件的方法”readLine()“,如果读到文件末尾,则返回为空,否则返回文件一行的文本
    */
   public String read_line() {
       return count < lines.length ? lines[count++] : null;
   }
   @Override
   public int read() throws IOException {
       return this.buffer.read();
   }
   public int readWithBuf(byte[] buf, int offset, int len) throws IOException {
       return this.buffer.read(buf, offset, len);
   }
   public int readWithBuf(byte[] buf) throws IOException {
       return this.buffer.read(buf);
   }
}

来源:https://blog.csdn.net/wr456wr/article/details/125363436

标签:Java,连接,Hadoop,编程
0
投稿

猜你喜欢

  • Java Cookie与Session实现会话跟踪详解

    2022-12-22 08:59:13
  • java 之JNA中的Memory和Pointer的使用方法

    2022-10-25 14:18:35
  • Android模拟器实现手机添加文件到sd卡的方法

    2022-01-18 08:38:27
  • java 中使用匿名类直接new接口详解及实例代码

    2021-08-13 22:26:12
  • Android setTag方法的key问题解决办法

    2021-09-12 14:29:37
  • kotlin 定义接口并实现回调的例子

    2022-12-06 14:30:19
  • spring cloud gateway网关路由分配代码实例解析

    2021-06-09 02:54:08
  • 使用C#调用系统API实现内存注入的代码

    2021-12-01 00:25:59
  • Android 实现自己的LOG信息

    2023-06-08 16:30:30
  • C# winform登陆框验证码的实现方法

    2022-08-26 12:45:58
  • Java 获取Web项目相对webapp地址的实例

    2022-07-03 17:46:00
  • java实例方法被覆盖,静态方法被隐藏Explain(详解)

    2022-07-20 08:05:03
  • Java利用Selenium操作浏览器的示例详解

    2022-06-17 17:34:20
  • C#实现将网页保存成图片的网页拍照功能

    2021-06-22 14:36:01
  • struts2实现多文件上传

    2023-11-23 02:27:53
  • c++ 函数指针相关总结

    2022-09-26 04:42:33
  • springboot反爬虫组件kk-anti-reptile的使用方法

    2022-01-09 14:12:59
  • Spring(AbstractRoutingDataSource)实现动态数据源切换示例

    2022-05-23 07:05:08
  • Java中Map的遍历方法及性能测试

    2023-07-14 08:54:15
  • java实现sftp客户端上传文件以及文件夹的功能代码

    2023-02-14 22:07:28
  • asp之家 软件编程 m.aspxhome.com