赞
踩
pom.xml
文件里添加hadoop
和junit
依赖<dependencies>
<dependency>
<!--hadoop客户端-->
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.4</version>
</dependency>
<!--单元调试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
resources
目录里创建log4j.properties
文件log4j.rootLogger=stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/hdfs.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
/ied01
目录创建hadoop2.txt
文件net.xxr.hdfs
包,在包里创建CreateFileOnHDFS
类create1()
方法package net.xxr.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.Test; import java.net.URI; public class CreateFileOnHDFS { public void create1() throws Exception{ // 创建配置对象 Configuration conf = new Configuration(); // 定义统一资源标识符 String uri = "hdfs://master:9000"; // 创建文件系统对象(基于HDFS的文件系统) FileSystem fs = FileSystem.get(new URI(uri), conf); // 创建路径对象(指向文件) Path path = new Path(uri + "/ied01/hadoop2.txt"); // 基于路径对象创建文件 boolean result = fs.createNewFile(path); // 根据返回值判断文件是否创建成功 if (result) { System.out.println("文件[" + path + "]创建成功!"); }else { System.out.println("文件[" + path + "]创建失败!"); } } }
create2()
方法,实现判断文件是否存在@Test public void create2() throws Exception{ // 创建配置对象 Configuration conf = new Configuration(); // 定义统一资源标识符 String uri = "hdfs://master:9000"; // 创建文件系统对象(基于HDFS的文件系统) FileSystem fs = FileSystem.get(new URI(uri), conf); // 创建路径对象(指向文件) Path path = new Path(uri + "/ied01/hadoop2.txt"); // 判断路径对象指向的文件是否存在 if (fs.exists(path)) { // 提示用户文件已存在 System.out.println("文件[" + path + "]已存在!"); }else{ // 基于路径对象创建文件 boolean result = fs.createNewFile(path); // 根据返回值判断文件是否创建成功 if (result) { System.out.println("文件[" + path + "]创建成功!"); }else { System.out.println("文件[" + path + "]创建失败!"); } } }
net.xxr.hdfs
包里创建WriteFileOnHDFS
类package net.xxr.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.Test; import java.net.URI; /* 功能:写入HDFS文件 作者:小小榕 日期:2022年11月30日 */ public class WriteFileOnHDFS { @Test public void write1() throws Exception{ // 创建配置对象 Configuration conf = new Configuration(); conf.set("dfs.client.use.datanode.hostname","true"); // 定义统一资源标识符 String uri = "hdfs://master:9000"; // 创建文件系统对象(基于HDFS的文件系统) FileSystem fs = FileSystem.get(new URI(uri), conf,"root"); // 创建路径对象(指向文件) Path path = new Path(uri + "/ied01/hadoop2.txt"); // 创建文件系统数据字节输出流 FSDataOutputStream out = fs.create(path); // 通过字节输出流向文件写数据 out.write("Hello Hadoop World".getBytes()); // 关闭输出流 out.close(); // 关闭文件系统对象 fs.close(); System.out.println("文件[" + path + "]写入成功"); } }
test.txt
create2()
方法@Test public void write2() throws Exception { // 创建配置对象 Configuration conf = new Configuration(); // 设置数据节点主机名属性 conf.set("dfs.client.use.datanode.hostname", "true"); // 定义uri字符串 String uri = "hdfs://master:9000"; // 创建文件系统对象 FileSystem fs = FileSystem.get(new URI(uri), conf, "root"); // 创建路径对象(指向目录或文件) Path path = new Path(uri + "/ied01/exam2.txt"); // 创建文件系统数据字节输出流对象 FSDataOutputStream out = fs.create(path); // 创建文件字符输入流对象 FileReader fr = new FileReader("test.txt"); // 创建缓冲字符输入流对象 BufferedReader br = new BufferedReader(fr); // 定义行字符串 String nextLine = ""; // 通过循环读取缓冲字符输入流 while ((nextLine = br.readLine()) != null) { // 在控制台输出读取的行 System.out.println(nextLine); // 通过文件系统数据字节输出流对象写入指定文件 out.write(nextLine.getBytes()); } // 关闭文件系统字节输出流 out.close(); // 关闭缓冲字符输入流 br.close(); // 关闭文件字符输入流 fr.close(); // 提示用户写入文件成功 System.out.println("本地文件[test.txt]成功写入[" + path + "]!"); }
create2_()
方法@Test public void write2_() throws Exception { // 创建配置对象 Configuration conf = new Configuration(); // 设置数据节点主机名属性 conf.set("dfs.client.use.datanode.hostname", "true"); // 定义uri字符串 String uri = "hdfs://master:9000"; // 创建文件系统对象 FileSystem fs = FileSystem.get(new URI(uri), conf, "root"); // 创建路径对象(指向目录或文件) Path path = new Path(uri + "/ied01/test2.txt"); // 创建文件系统数据字节输出流对象 FSDataOutputStream out = fs.create(path); // 创建文件字节输入流对象 FileInputStream in = new FileInputStream("test.txt"); // 利用IOUtils类提供的字节拷贝方法来复制文件 IOUtils.copyBytes(in, out, conf); // 关闭文件字节输入流 in.close(); // 关闭文件系统数据字节输出流 out.close(); // 关闭文件系统 fs.close(); // 提示用户写入文件成功 System.out.println("本地文件[test.txt]成功写入[" + path + "]!"); }
/ied01/test.txt
内容net.xxr.hdfs
包里创建ReadFileOnHDFS
类read1()
方法package net.xxr.hdfs; 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.junit.Test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; /* 功能:读取HDFS文件 作者:小小榕 日期:2022年11月30日 */ public class ReadFileOnHDFS { @Test public void read1() throws Exception { // 创建配置对象 Configuration conf = new Configuration(); // 设置数据节点主机名属性 conf.set("dfs.client.use.datanode.hostname", "true"); // 定义uri字符串 String uri = "hdfs://master:9000"; // 创建文件系统对象 FileSystem fs = FileSystem.get(new URI(uri), conf, "root"); // 创建路径对象(指向目录或文件) Path path = new Path(uri + "/ied01/test2.txt"); // 创建文件系统数据字节输入流对象 FSDataInputStream in = fs.open(path); // 创建缓冲字符输入流对象,提高读取效率(字节流-->字符流-->缓冲流) BufferedReader br = new BufferedReader(new InputStreamReader(in)); // 定义行字符串 String nextLine = ""; // 通过循环读取缓冲字符输入流 while ((nextLine = br.readLine()) != null) { // 在控制台输出读取的行内容 System.out.println(nextLine); } // 关闭缓冲字符输入流 br.close(); // 关闭文件系统数据字节输入流 in.close(); // 关闭文件系统 fs.close(); } }
IOUtils
类简化代码read1_()
测试方法read2()
方法@Test public void read2() throws Exception { // 创建配置对象 Configuration conf = new Configuration(); // 设置数据节点主机名属性 conf.set("dfs.client.use.datanode.hostname", "true"); // 定义uri字符串 String uri = "hdfs://master:9000"; // 创建文件系统对象 FileSystem fs = FileSystem.get(new URI(uri), conf, "root"); // 创建路径对象(指向目录或文件) Path path = new Path(uri + "/ied01/test2.txt"); // 创建文件系统数据字节输入流对象 FSDataInputStream in = fs.open(path); // 创建文件字节输出流 FileOutputStream out = new FileOutputStream("download/exam.txt"); // 读取HDFS文件(靠输入流),写入本地文件(靠输出流) IOUtils.copyBytes(in, out, conf); // 关闭文件系统数据字节输入流 in.close(); // 关闭文件字节输出流 out.close(); // 关闭文件系统 fs.close(); // 提示用户文件下载成功 System.out.println("文件[" + path + "]下载到本地文件[download/exam.txt]!"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。