当前位置:首页 > 站长资讯 > 正文内容

【说站】利用Java连接Hadoop进行编程

yc8882年前 (2022-07-24)站长资讯195

这篇文章主要介绍了利用Java连接Hadoop进行编程,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下!

实验环境

  • 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进行编程

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

思路: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进行编程

②实现缓存功能,即利用”MyFSDataInputStream“读取若干字节数据时,首先查找缓存,如果缓存中有所需要数据,则直接由缓存提供,否则从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;}@Overridepublic 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);}}


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/zhanzhang/7418.html

标签: Java
分享给朋友:

“【说站】利用Java连接Hadoop进行编程” 的相关文章

【说站】宝塔站点日志文件过大怎么办?网站日志切割教程

【说站】宝塔站点日志文件过大怎么办?网站日志切割教程

宝塔面板日志文件过大的原因?宝塔面板的网站日志文件默认是生成一个日志文件,然后系统每天不断的对这个文件进行写入操作,这样日子长了,这个日志文件就会越来越大,几百兆、几个G都是蛮正常的,这样对于我们分析...

【说站】Win10专业版如何激活?Win10激活工具

【说站】Win10专业版如何激活?Win10激活工具

本人用的Win10专业版一直用的好好地,今天桌面右下方就提示“激活Windows 转到设置以激活Windows”,因为一直用的是盗版Windows 10,出现这样的提示也蛮正常,没得办法,见招拆招,在...

【说站】excel筛选两列数据中的重复数据并排序

【说站】excel筛选两列数据中的重复数据并排序

如果靠人眼来一个个的对比excel的两列数据来去重的话,数据量少还能勉强对比一下,如果几千、几万条数据肯定就需要进行程式化处理,excel对于这个问题给我们提供了很方便的解决方案,这里主要用到exce...

【说站】Excel如何快速删除空行?WPS删除excel空白行

【说站】Excel如何快速删除空行?WPS删除excel空白行

站长我经常会处理excel文档,之前介绍过Microsoft Office excel文档删除空行的办法,今天介绍WPS Office下面的excel如何删除空白行。方法一:筛选  选中数据所在的那一...

【说站】宝塔如何按日期每天生成一个网站日志文件

【说站】宝塔如何按日期每天生成一个网站日志文件

宝塔面板默认的会按照nginx.conf的配置生成在/www/wwwlogs目录下面生成一个网站访问日志和一个网站错误日志,每当有新的记录时系统会不断的对这两个文件进行写入操作,但随着访问量的增长,日...

【说站】判断服务器IP否被墙 是否被TCP阻断

【说站】判断服务器IP否被墙 是否被TCP阻断

现在国内很多购买国外主机服务器的,但往往很多主机商的机子用的人多了,国内使用者用这些服务器做啥的都有,正儿八经的做外贸其实没多大事情,但往往有些人就是不遵守法律法规,长此以往用的人多了,这些国外的主机...