一.基础环境:
本文默认了你已经有一点的java基础,本机环境已安装java、maven、ide,配置好了相关的环境变量,且已经有可用的hadoop环境,已经用idea新建一个java maven项目。
以上环境有没完成的,自行去百度完成。
二.pom.xml引入包:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
三.引入hadoop的配置文件:
将hadoop的配置文件core-site.xml、hdfs-site.xml考到本地resources目录下,
四. kerberos认证(如果需要的话):
【大数据笔记】- Hadoop Java kerberos认证_foxofwind的博客-CSDN博客
五.Hadoop API简介:
这篇文章写得挺好,大家可看下
六.上测试代码
package com.yixin;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.io.InputStream;
public class HdfsTest {
/**
* 创建HDFS文件
*/
public static void createFile(Configuration conf, String filePath, byte[] contBytes) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(filePath);
// 打开文件输出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contBytes);
outputStream.close();
fs.close();
System.out.println("HDFS文件创建成功!");
}
// 上传本地文件
public static void uploadFile(Configuration conf, String src, String dst) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(src); // 原路径
Path dstPath = new Path(dst); // 目标路径
// 调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
fs.copyFromLocalFile(false, srcPath, dstPath);
// 打印文件路径
System.out.println("Upload to " + conf.get("fs.default.name"));
System.out.println("------------list files------------" + "\n");
FileStatus[] fileStatus = fs.listStatus(dstPath);
for (FileStatus file : fileStatus) {
System.out.println(file.getPath());
}
fs.close();
}
/**
* HDFS文件重命名
*/
public static void rename(Configuration conf, String oldName, String newName) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean isok = fs.rename(oldPath, newPath);
if (isok) {
System.out.println("改名 成功!");
} else {
System.out.println("改名 失败");
}
fs.close();
}
// HDFS删除文件
public static void delete(Configuration conf, String filePath) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean isok = fs.deleteOnExit(path);
if (isok) {
System.out.println("删除 成功!");
} else {
System.out.println("删除 失败!");
}
fs.close();
}
// HDFS创建目录
public static void mkdir(Configuration conf, String path) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(path);
boolean isok = fs.mkdirs(srcPath);
if (isok) {
System.out.println("创建文件夹 成功!");
} else {
System.out.println("创建文件夹 失败!");
}
fs.close();
}
// 读取HDFS文件的内容
public static void readFile(Configuration conf, String filePath) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(filePath);
InputStream in = null;
try {
in = fs.open(srcPath);
// 复制到标准输出流
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
}
public static void main(String[] args) throws IOException {
// 创建配置文件
Configuration conf = new Configuration();
// kerberos认证,如果不需要kerberos可以注掉,我的集群是需要的
authKerberos(conf);
// 上传文件到HDFS
uploadFile(conf, "D:\\tes\\test.txt", "/tmp");
// HDFS创建文件
byte[] contBytes = "hello hadoop!你好,哈肚普!\n".getBytes();
createFile(conf, "/tmp/hello.txt", contBytes);
// HDFS文件重命名
rename(conf, "/tmp/hello.txt", "/tmp/helloHadoop.txt");
createFile(conf, "hello.txt", contBytes); //相对路径
// HDFS删除文件
// delete(conf, "hello.txt");
// 新建HDFS目录
mkdir(conf, "test1");
//删除HDFS目录
// delete(conf, "test1");
// 读取HDFS文件
readFile(conf, "/tmp/helloHadoop.txt");
}
private static void authKerberos(Configuration conf) throws IOException {
// kerberos配置文件路径。
String krb5File = "D:/code/maventest/src/main/resources/local/krb5.conf";
System.setProperty("java.security.krb5.conf", krb5File);
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
// 用户验证,第一参是执行用户,第二参是keytab文件路径。
UserGroupInformation.loginUserFromKeytab("dw"
, "D:/code/maventest/src/main/resources/local/dw_host.keytab");
}
}
运行结果:
Upload to hdfs://hdp-t7:8020
------------list files------------
hdfs://hdp-t7:8020/tmp/ambari-qa
hdfs://hdp-t7:8020/tmp/basese_info
hdfs://hdp-t7:8020/tmp/c.txt
hdfs://hdp-t7:8020/tmp/d.txt
hdfs://hdp-t7:8020/tmp/dataload
hdfs://hdp-t7:8020/tmp/dw
hdfs://hdp-t7:8020/tmp/dw__89c63710_eac0_41a1_982e_53a24499f5bf
hdfs://hdp-t7:8020/tmp/entity-file-history
hdfs://hdp-t7:8020/tmp/helloHadoop.txt
hdfs://hdp-t7:8020/tmp/hive
hdfs://hdp-t7:8020/tmp/hive_result
hdfs://hdp-t7:8020/tmp/ida8c07e91_date201020
hdfs://hdp-t7:8020/tmp/pblogconfig.txt
hdfs://hdp-t7:8020/tmp/presto_result
hdfs://hdp-t7:8020/tmp/tablespace
hdfs://hdp-t7:8020/tmp/test.txt
hdfs://hdp-t7:8020/tmp/tezsmokeinput
hdfs://hdp-t7:8020/tmp/tezsmokeoutput
HDFS文件创建成功!
改名 失败
HDFS文件创建成功!
创建文件夹 成功!
hello hadoop!你好,哈度普!
Process finished with exit code 0