1.初始化HDFS文件系统和关闭文件系统
/**
* 初始化HDFS文件系统
* @throws IOException 如果在初始化过程中发生I/O错误
*/
public static void init() throws IOException {
conf = new Configuration(); // 创建Hadoop配置对象
conf.set("fs.defaultFS", "hdfs://gxzy:9000"); // 设置默认的HDFS文件系统地址
fS = FileSystem.get(conf); // 获取HDFS文件系统对象
}
/**
* 关闭文件系统
* @throws IOException 如果在关闭文件系统过程中发生I/O错误
*/
public static void close() throws IOException {
if (fS != null) {
fS.close(); // 关闭HDFS文件系统连接
}
}
2.创建文件夹
/**
* 这个Java程序定义了一个名为`mkdir`的方法,用于在HDFS中创建目录。
*
* @param dir 要创建的目录路径。
* @throws IOException 如果在创建目录过程中发生I/O错误。
*/
public static void mkdir(String dir) throws IOException {
Path path = new Path(dir); // 创建目录的Path对象
// 判断是否存在该文件夹,如果不存在则创建,存在输出文件夹存在提示
if (fS.exists(path)) { // 检查目录是否已经存在
System.out.println("the directory is exists"); // 输出文件夹存在提示
return;
} else {
fS.mkdirs(path); // 在HDFS中创建目录
System.out.println("mkdir success"); // 输出创建成功提示
return;
}
}
3.编程实现列出HDFS文件目录
/**
* 这个Java程序定义了一个名为`listFiles`的方法,用于列出HDFS中指定目录下的文件列表。
*
* @param dir 要列出文件的目录路径。
* @return 目录下的文件列表,如果不是目录则返回相应的消息。
* @throws IOException 如果在列出文件列表过程中发生I/O错误。
*/
public static String listFiles(String dir) throws IOException {
String Filelist = ""; // 存储文件列表的字符串
Path path = new Path(dir); // 创建目录的Path对象
if (!fS.isDirectory(path)) { // 检查是否为目录
Filelist = "it is not a directory"; // 如果不是目录,赋予相应的消息给Filelist
return Filelist; // 返回消息
} else {
FileStatus fileStatus[] = fS.listStatus(path); // 获取指定目录下的文件状态
for (FileStatus file : fileStatus) { // 遍历文件状态数组
Filelist += file.getPath().getName() + "\n"; // 将文件名添加到Filelist中
}
return Filelist; // 返回文件列表
}
}
4.将文件从本地文件系统复制到HDFS(Hadoop分布式文件系统)
// 这个方法用于将文件从本地文件系统复制到HDFS(Hadoop分布式文件系统)
public static void copyFromLocal(String localFile, String hdfsDir) throws IOException {
// 创建表示本地文件的File对象
File local = new File(localFile);
// 检查本地文件是否存在
if (!local.exists()) {
System.out.println("本地源文件不存在");
return;
}
// 创建源文件的Path对象
Path sourcePath = new Path(localFile);
// 创建HDFS中目标目录的Path对象
Path dstPath = new Path(hdfsDir);
// 从源路径提取文件名
String fileName = sourcePath.getName();
// 创建HDFS中目标文件的Path对象
Path dstFilePath = new Path(hdfsDir + "/" + fileName);
// 检查HDFS中目标文件是否已经存在
if (fS.exists(dstFilePath)) {
System.out.println("目标HDFS文件已经存在");
return;
} else {
// 如果目标文件不存在,则将文件从本地文件系统复制到HDFS
fS.copyFromLocalFile(sourcePath, dstPath); // 关键代码:将文件从本地复制到HDFS
System.out.println("copyFromLocal 成功");
}
}
5.将HDFS中的文件复制到本地目录
/**
* 这个Java程序定义了一个名为`copyToLocal`的方法,用于将HDFS中的文件复制到本地目录。
* 方法接受两个参数:`hdfsFile`表示HDFS中文件的路径,`localDir`指定了文件将要复制到的本地目录。
*
* @param hdfsFile HDFS中要复制的文件路径。
* @param localDir 要复制到的本地目录路径。
* @throws IOException 如果在文件复制过程中发生I/O错误。
*/
public static void copyToLocal(String hdfsFile, String localDir) throws IOException {
// 创建HDFS文件的Path对象
Path hdfsPath = new Path(hdfsFile);
// 创建本地目录的Path对象
Path localPath = new Path(localDir);
// 检查HDFS文件是否存在
if (fS.exists(hdfsPath) == false) {
System.out.println("the source hdfs file is not exists");
return;
}
// 创建本地目录的File对象
File localD = new File(localDir);
// 通过将HDFS文件名附加到本地目录构建完整的本地文件路径
String localFile = localDir + "/" + hdfsPath.getName();
// 创建本地文件的File对象
File localF = new File(localFile);
// 检查本地目录是否存在
if (localD.exists() == false) {
System.out.println("the local dir is not exists");
return;
} else if (localF.exists() == true) {
System.out.println("the local dst file is already exists");
return;
} else {
// 将HDFS文件复制到本地目录
fS.copyToLocalFile(hdfsPath, localPath);
return;
}
}
6.删除文件或目录
/**
* 这个Java程序定义了一个名为`delete`的方法,用于删除HDFS中的目录。
* 方法接受一个参数:`hdfsDir`表示HDFS中要删除的目录路径。
*
* @param hdfsDir 要删除的HDFS目录路径。
* @throws IOException 如果在删除过程中发生I/O错误。
*/
public static void delete(String hdfsDir) throws IOException {
// 创建目录的Path对象
Path dirPath = new Path(hdfsDir);
// 检查目录是否存在
if (fS.exists(dirPath) == false) {
System.out.println("the dir is not exists");
return;
} else {
// 递归删除目录
fS.delete(dirPath, true);
System.out.println("delete success");
}
}
7.读取文件内容到content中
/**
* 这个Java程序定义了一个名为`open`的方法,用于从HDFS中打开并读取文件内容。
* 方法接受一个参数:`hdfsFile`表示HDFS中要打开的文件路径。
*
* @param hdfsFile 要打开的HDFS文件路径。
* @return 文件的内容,如果文件不存在或不是普通文件,则返回相应的消息。
* @throws IOException 如果在文件打开过程中发生I/O错误。
*/
public static String open(String hdfsFile) throws IOException {
String content = ""; // 用于存储文件内容的字符串
Path filePath = new Path(hdfsFile); // 创建文件的Path对象
// 检查文件是否存在
if (fS.exists(filePath) == false) {
content = "the hdfs path is not exists"; // 如果文件不存在,赋予相应的消息给content
return content; // 返回消息
}
// 检查是否为普通文件
else if (fS.isFile(filePath) == false) {
content = "it is not a file"; // 如果不是普通文件,赋予相应的消息给content
return content; // 返回消息
} else {
// 打开文件并创建输入流
FSDataInputStream in = fS.open(filePath);
InputStreamReader inputReader = new InputStreamReader(in); // 创建InputStreamReader以便读取输入流
BufferedReader buffer = new BufferedReader(inputReader); // 使用BufferedReader来逐行读取文件内容
String line;
while ((line = buffer.readLine()) != null) { // 逐行读取文件内容
content += line + "\n"; // 将每一行的内容添加到content中
}
return content; // 返回文件内容
}
}
8.用I/O对拷方式上传大文件,hdfsDir是上传的目标文件夹
/**
* 这个Java程序定义了一个名为`IOPut`的方法,用于将本地文件复制到HDFS中。
*
* @param localFile 本地文件路径。
* @param hdfsDir HDFS目标目录路径。
* @throws IOException 如果在文件复制过程中发生I/O错误。
*/
public static void IOPut(String localFile, String hdfsDir) throws IOException {
File localf = new File(localFile); // 创建本地文件对象
Path hdfsPath = new Path(hdfsDir); // 创建HDFS目标目录路径对象
Path hdfsFile = new Path(hdfsDir + "/" + localf.getName()); // 创建HDFS文件路径对象
if (!localf.exists() || !localf.isFile()) { // 检查本地文件是否存在且为普通文件
System.out.println("the local file is not exists or is not a file"); // 输出本地文件不存在或不是文件的提示
return;
} else if (!fS.exists(hdfsPath) || fS.exists(hdfsFile)) { // 检查HDFS目录是否存在且HDFS文件是否已经存在
System.out.println("the hdfspath is nor exists or file already exists"); // 输出HDFS目录不存在或者HDFS文件已经存在的提示
return;
} else {
FileInputStream inputStream = new FileInputStream(localf); // 创建本地文件输入流
FSDataOutputStream outputStream = fS.create(hdfsFile); // 在HDFS上创建输出流
IOUtils.copyBytes(inputStream, outputStream, 1024); // 将本地文件内容复制到HDFS文件
}
}
该方法接受两个参数:localFile
表示本地文件路径,hdfsDir
表示HDFS目标目录路径。
程序开始时创建了本地文件对象localf
和HDFS目标目录路径对象hdfsPath
,并根据本地文件名创建了HDFS文件路径对象hdfsFile
。然后通过一系列的条件判断,检查本地文件和HDFS目录的状态,以确定是否可以进行文件复制操作。
如果一切正常,程序将使用文件输入流FileInputStream
读取本地文件内容,并通过fS.create(hdfsFile)
在HDFS上创建输出流FSDataOutputStream
,最后通过IOUtils.copyBytes
将本地文件内容复制到HDFS文件。
关键代码:
关键代码包括localf = new File(localFile)
、hdfsPath = new Path(hdfsDir)
、hdfsFile = new Path(hdfsDir + "/" + localf.getName())
、inputStream = new FileInputStream(localf)
、outputStream = fS.create(hdfsFile)
和IOUtils.copyBytes(input, output, 1024)
。这些代码分别用于创建本地文件对象、HDFS目标目录路径对象、HDFS文件路径对象、本地文件输入流、在HDFS上创建输出流以及进行文件内容复制。
9.用I/O对拷方式下载大文件,local File是下载后的文件名
/**
* 定义了一个名为`IOGet`的方法,用于从HDFS中获取文件到本地。
*
* @param hdfsFile HDFS文件路径。
* @param localFile 本地目标文件路径。
* @throws IOException 如果在文件获取过程中发生I/O错误。
*/
public static void IOGet(String hdfsFile, String localFile) throws IOException {
Path hdfsPath = new Path(hdfsFile); // 创建HDFS文件路径对象
File localf = new File(localFile); // 创建本地目标文件对象
if (!fS.exists(hdfsPath) || fS.isDirectory(hdfsPath)) { // 检查HDFS文件是否存在且不是目录
System.out.println("the hdfsFile is not exists or it is not a file"); // 输出HDFS文件不存在或者不是文件的提示
return;
} else {
FSDataInputStream inputStream = fS.open(hdfsPath); // 打开HDFS文件输入流
FileOutputStream outputStream = new FileOutputStream(localf); // 创建本地文件输出流
IOUtils.copyBytes(inputStream, outputStream, 1024); // 将HDFS文件内容复制到本地文件
}
}
该方法接受两个参数:hdfsFile
表示HDFS文件路径,localFile
表示本地目标文件路径。
程序开始时创建了HDFS文件路径对象hdfsPath
和本地目标文件对象localf
。然后通过一系列的条件判断,检查HDFS文件的状态,以确定是否可以进行文件获取操作。
如果一切正常,程序将使用fS.open(hdfsPath)
打开HDFS文件输入流,并通过new FileOutputStream(localf)
创建本地文件输出流,最后通过IOUtils.copyBytes
将HDFS文件内容复制到本地文件。
关键代码:
关键代码包括hdfsPath = new Path(hdfsFile)
、localf = new File(localFile)
、inputStream = fS.open(hdfsPath)
、outputStream = new FileOutputStream(localf)
和IOUtils.copyBytes(input, output, 1024)
。这些代码分别用于创建HDFS文件路径对象、本地目标文件对象、打开HDFS文件输入流、创建本地文件输出流以及进行文件内容复制。
完整代码
package HDFS_test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileContextTestHelper.fileType;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.server.namenode.status_jsp;
import org.apache.hadoop.io.IOUtils;
public class HDFSOp {
public static Configuration conf;
public static FileSystem fS;
// 初始化HDFS文件系统
public static void init() throws IOException {
conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://gxzy:9000");
fS = FileSystem.get(conf);
}
// 关闭文件系统
public static void close() throws IOException {
if (fS != null) {
fS.close();
}
}
// 创建文件夹
public static void mkdir(String dir) throws IOException{
Path path=new Path(dir);
// 判断是否存在该文件夹,如果不存在则创建,存在输出文件夹存在提示
if(fS.exists(path)){
System.out.println("the directory is exists");
return;
}
else {
fS.mkdirs(path);
System.out.println("mkdir success");
return;
}
}
//listFile HDFS
public static String listFiles(String dir) throws IOException {
String Filelist="";
Path path=new Path(dir);
if(!fS.isDirectory(path)){
Filelist="it is not a directory";
return Filelist;
}
else{
FileStatus fileStatus[]=fS.listStatus(path);
for(FileStatus file:fileStatus){
Filelist+=file.getPath().getName()+"\n";
}
return Filelist;
}
}
//3.
public static void createFile(String fileName) throws IOException{
Path filepath=new Path(fileName);
//
if (fS.exists(filepath)){
System.out.println("file already exists");
return;
}
else{
fS.create(filepath);
System.out.println("create file success");
return;
}
}
//
public static void copyFromLocal(String localFile,String hdfsDir) throws IOException{
File local=new File(localFile);
//
if(local.exists()==false){
System.out.println("the local source file is not exists");
return;
}
Path sourcePath=new Path(localFile);
Path dstPath=new Path(hdfsDir);
String fileName=sourcePath.getName();
Path dstFilePath=new Path(hdfsDir+"/"+fileName);
//
if(fS.exists(dstFilePath)==true){
System.out.println("the dst hdfs file is already exists");
return;
}
else {
fS.copyFromLocalFile(sourcePath, dstPath);
System.out.println("copyFromLocal success");
}
}
public static void copyToLocal(String hdfsFile,String localDir) throws IOException{
Path hdfsPath=new Path(hdfsFile);
Path localPath=new Path(localDir);
if(fS.exists(hdfsPath)==false){
System.out.println("the source hdfs file is not exists");
return;
}
File localD=new File(localDir);
String localFile=localDir+"/"+hdfsPath.getName();
File localF=new File(localFile);
if(localD.exists()==false){
System.out.println("the local dir is not exists");
return;
}
else if(localF.exists()==true){
System.out.println("the local dst file is already exists");
return;
}
else{
fS.copyToLocalFile(hdfsPath, localPath);
return;
}
}
//
public static void delete(String hdfsDir) throws IOException{
Path dirPath=new Path(hdfsDir);
if(fS.exists(dirPath)==false){
System.out.println("the dir is not exists");
return;
}
else{
fS.delete(dirPath,true);
System.out.println("delete success");
}
}
//
public static String open(String hdfsFile) throws IOException{
String content="";
Path filePath=new Path(hdfsFile);
if(fS.exists(filePath)==false){
content="the hdfs path is not exists";
return content;
}
else if (fS.isFile(filePath)==false){
content="it is not a file";
return content;
}
else{
FSDataInputStream in =fS.open(filePath);
InputStreamReader inputReader=new InputStreamReader(in);
BufferedReader buffer=new BufferedReader(inputReader);
String line;
while((line=buffer.readLine())!=null){
content+=line+"\n";
}
return content;
}
}
//
public static void IOPut(String localFile,String hdfsDir) throws IOException{
File localf=new File(localFile);
Path hdfsPath=new Path(hdfsDir);
Path hdfsFile=new Path(hdfsDir+"/"+localf.getName());
//
if(!localf.exists()||!localf.isFile()){
System.out.println("the local file is not exists or is not a file");
return;
}
//
else if(!fS.exists(hdfsPath)||!fS.exists(hdfsFile)){
System.out.println("the hdfspath is nor exists or file already exists");
return;
}
else{
FileInputStream inputStream=new FileInputStream(localf);
FSDataOutputStream outputStream=fS.create(hdfsFile);
IOUtils.copyBytes(inputStream,outputStream,1024);
}
}
//
public static void IOGet(String hdfsFile,String localFile) throws IOException{
Path hdfsPath=new Path(hdfsFile);
File localf=new File(localFile);
if(!fS.exists(hdfsPath)||fS.isDirectory(hdfsPath)){
System.out.println("the hdfsFile is not exists or it is not a file");
return;
}
else{
FSDataInputStream inputStream=fS.open(hdfsPath);
FileOutputStream outputStream=new FileOutputStream(localf);
IOUtils.copyBytes(inputStream, outputStream, 1024);
}
}
//
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
init();
//mkdir("/user/root/qimojiayou");
//String filesString=listFiles("/user/root");
//System.out.println(filesString);
//createFile("/user/root/qimojiayou/hhh");
//copyFromLocal("/home/hadoop/c.txt","/user/root/qimojiayou" );
//copyToLocal("/user/root/qimojiayou/c.txt", "/home/hadoop");
//delete("/user/root/qimojiayou/c.txt");
//String contentString=open("/user/root/qimojiayou/c.txt");
//System.out.println(contentString);
//IOPut("/home/hadoop/MRData/product", "/user/root");
IOGet("/user/root/qimijiayou", "home/hadoop");
close();
}
}