java实现对Hadoop的操作
更新时间:2021年7月1日 15:00 点击:1572
基本操作
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.Test; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; @RunWith(JUnit4.class) @DisplayName("Test using junit4") public class HadoopClientTest { private FileSystem fileSystem = null; @BeforeEach public void init() throws URISyntaxException, IOException, InterruptedException { Configuration configuration = new Configuration(); configuration.set("dfs.replication", "1"); configuration.set("dfs.blocksize", "64m"); fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000"), configuration, "root"); } /** * 从本地复制文件到Hadoop * * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void copyFileFromLocal() throws URISyntaxException, IOException, InterruptedException { // 上传文件 fileSystem.copyFromLocalFile(new Path("C:\\Users\\Administrator\\Desktop\\win10激活.txt"), new Path("/even1")); // 关闭流,报错winUtils,因为使用了linux的tar包,如果windows要使用,则需要编译好这个winUtils包才能使用 fileSystem.close(); } /** * 从Hadoop下载文件到本地,下载需要配置Hadoop环境,并添加winutils到bin目录 * * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void copyFileToLocal() throws URISyntaxException, IOException, InterruptedException { // 下载文件 fileSystem.copyToLocalFile(new Path("/win10激活.txt"), new Path("E:/")); // 关闭流,报错winUtils,因为使用了linux的tar包,如果windows要使用,则需要编译好这个winUtils包才能使用 fileSystem.close(); } /** * 创建文件夹 * * @throws IOException */ @Test public void hdfsMkdir() throws IOException { // 调用创建文件夹方法 fileSystem.mkdirs(new Path("/even1")); // 关闭方法 fileSystem.close(); } /** * 移动文件/修改文件名 */ public void hdfsRename() throws IOException { fileSystem.rename(new Path(""), new Path("")); fileSystem.close(); } /** * 删除文件/文件夹 * * @throws IOException */ @Test public void hdfsRm() throws IOException { // fileSystem.delete(new Path("")); // 第二个参数表示递归删除 fileSystem.delete(new Path(""), true); fileSystem.close(); } /** * 查看hdfs指定目录的信息 * * @throws IOException */ @Test public void hdfsLs() throws IOException { // 调用方法返回远程迭代器,第二个参数是把目录文件夹内的文件也列出来 RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus locatedFileStatus = listFiles.next(); System.out.println("文件路径:" + locatedFileStatus.getPath()); System.out.println("块大小:" + locatedFileStatus.getBlockSize()); System.out.println("文件长度:" + locatedFileStatus.getLen()); System.out.println("副本数量:" + locatedFileStatus.getReplication()); System.out.println("块信息:" + Arrays.toString(locatedFileStatus.getBlockLocations())); } fileSystem.close(); } /** * 判断是文件还是文件夹 */ @Test public void findHdfs() throws IOException { // 1,展示状态信息 FileStatus[] listStatus = fileSystem.listStatus(new Path("/")); // 2,遍历所有文件 for (FileStatus fileStatus : listStatus) { if (fileStatus.isFile()) System.out.println("是文件:" + fileStatus.getPath().getName()); else if (fileStatus.isDirectory()) System.out.println("是文件夹:" + fileStatus.getPath().getName()); } fileSystem.close(); } }
文件读写
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.DisplayName; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.Arrays; @RunWith(JUnit4.class) @DisplayName("this is read write test!") public class HadoopReadWriteTest { FileSystem fileSystem = null; Configuration configuration = null; @Before public void init() throws URISyntaxException, IOException, InterruptedException { // 1,加载配置 configuration = new Configuration(); // 2,构建客户端 fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000/"), configuration, "root"); } @Test public void testReadData() throws IOException { // 1,获取hdfs文件流 FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt")); // 2,设置一次获取的大小 byte[] bytes = new byte[1024]; // 3,读取数据 while (open.read(bytes) != -1) System.out.println(Arrays.toString(bytes)); open.close(); fileSystem.close(); } /** * 使用缓存流 * * @throws IOException */ @Test public void testReadData1() throws IOException { FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt")); // 使用缓冲流会快点 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open, StandardCharsets.UTF_8)); String line = ""; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } bufferedReader.close(); open.close(); fileSystem.close(); } /** * 指定偏移量来实现只读部分内容 */ @Test public void readSomeData() throws IOException { FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt")); // 指定开始的index open.seek(14); // 指定读的多少 byte[] bytes = new byte[5]; while (open.read(bytes) != -1) System.out.println(new String(bytes)); open.close(); fileSystem.close(); } /** * 流方式写数据 * @throws IOException */ @Test public void writeData() throws IOException { // 1,获取输出流 FSDataOutputStream out = fileSystem.create(new Path("/win11.txt"), false); // 2,获取需要写的文件输入流 FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt")); byte[] b = new byte[1024]; int read = 0; while ((read = in.read(b)) != -1) { out.write(b, 0, read); } in.close(); out.close(); fileSystem.close(); } /** * 直接写字符串 */ @Test public void writeData1() throws IOException { // 1,创建输出流 FSDataOutputStream out = fileSystem.create(new Path("/aibaobao.txt"), false); // 2,写数据 out.write("wochaoaibaobao".getBytes()); // 3,关闭流 IOUtils.closeStream(out); fileSystem.close(); } /** * IOUtils方式上传 * * @throws IOException */ @Test public void putToHdfs() throws IOException { // 1,获取输入流 FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt")); // 2,获取输出流 FSDataOutputStream out = fileSystem.create(new Path("/haddopPut.txt"), false); // 3,拷贝 IOUtils.copyBytes(in, out, configuration); // 4,关闭流 IOUtils.closeStream(in); IOUtils.closeStream(out); fileSystem.close(); } /** * IOUtils方式下载 * @throws IOException */ @Test public void getFromHdfs() throws IOException { // 1,获取输入流 FSDataInputStream open = fileSystem.open(new Path("/haddopPut.txt")); // 2,获取输出流 FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\haddopPut.txt")); // 3,拷贝 IOUtils.copyBytes(open, out, configuration); // 4,关闭流 IOUtils.closeStream(open); IOUtils.closeStream(out); fileSystem.close(); } }
到此这篇关于java实现对Hadoop的操作的文章就介绍到这了,更多相关Java Hadoop内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
相关文章
- 这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试...2022-02-01
java 运行报错has been compiled by a more recent version of the Java Runtime
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)...2021-04-01- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了Java如何发起http请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-31
- 说起C#和Java这两门语言(语法,数据类型 等),个人以为,大概有90%以上的相似,甚至可以认为几乎一样。但是在工作中,我也发现了一些细微的差别...2020-06-25
- 这篇文章主要介绍了解决Java处理HTTP请求超时的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 这篇文章主要介绍了java 判断两个时间段是否重叠的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了超简洁java实现双色球若干注随机号码生成(实例代码),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
这篇文章主要介绍了java 画pdf用itext调整表格宽度、自定义各个列宽的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31- 这篇文章主要介绍了java正则表达式判断前端参数修改表中另一个字段的值,需要的朋友可以参考下...2021-05-07
Java使用ScriptEngine动态执行代码(附Java几种动态执行代码比较)
这篇文章主要介绍了Java使用ScriptEngine动态执行代码,并且分享Java几种动态执行代码比较,需要的朋友可以参考下...2021-04-15- 这篇文章主要介绍了Java开发实现人机猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-03
Java 8 Stream 的终极技巧——Collectors 功能与操作方法详解
这篇文章主要介绍了Java 8 Stream Collectors 功能与操作方法,结合实例形式详细分析了Java 8 Stream Collectors 功能、操作方法及相关注意事项,需要的朋友可以参考下...2020-05-20- 这篇文章主要介绍了Java List集合返回值去掉中括号('[ ]')的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-29
Java中lombok的@Builder注解的解析与简单使用详解
这篇文章主要介绍了Java中lombok的@Builder注解的解析与简单使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-06- 下面小编就为大家带来一篇java中String类型变量的赋值问题介绍。小编觉得挺不错的。现在分享给大家,给大家一个参考。...2016-03-28
- 这篇文章主要介绍了Java线程池中的各个参数如何合理设置操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-19
- 在Java中,我们可以利用多线程来最大化地压榨CPU多核计算的能力,下面这篇文章主要给大家介绍了关于java中多线程与线程池基本使用的相关资料,需要的朋友可以参考下...2021-09-13