HDFS JAVA API开发的思路及工具类代码

本文详细介绍如何使用Java API进行HDFS开发,包括Maven项目的依赖配置、HDFS文件读写操作、目录文件列表获取等实用代码示例,以及本地文件操作的对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面刚刚介绍了hdfs的架构及运行原理,有想看的见https://blog.youkuaiyun.com/weixin_42231373/article/details/85005667

现在我们来说说如何使用java API 开发HDFS,下面是些简单的思路 后面会有代码

 

建项目时修改pom.xml 文件           增加Hadoop的依赖  强调一下我说的是maven项目,普通项目自行下载依赖并添加到类路径,如果不知道的话见之前的博客,略有说明 https://blog.youkuaiyun.com/weixin_42231373/article/details/84654576

  maven的依赖为

        <dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.4</version>
			<scope>provided</scope>
		</dependency>

获得日志对象                                  Logger logger = Logger.get(cls);

建立hdfs配置单例                          Configrutaion conf = new Configrutaion();

通过conf传递参数                          conf.set(name, value);                conf.get(name)

获得hdfs文件系统                          FileSystem fs = FileSystem.get(conf);

获得hdfs处理(输入输出流,各种操作)

   FSDataInputStream fsis = fs.open(new Path(“hdfs路径”));

实际处理逻辑      (读取输入流,写入 等)

开发完后打包上传,测试运行

下面是操纵HDFS文件具体的代码 以工具类的方式呈现

import java.io.IOException;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
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.log4j.Logger;

public class HdfsFileOperatorUtil {
	Logger loger = Logger.getLogger(HdfsFileOperatorUtil.class);
	static Configuration hadoopConf = new Configuration();

	public static byte[] readFileToByteArray(String filePath) throws Exception {
		byte[] result = null;
		if (filePath == null || filePath.trim().length() == 0) {
			throw new Exception("文件路径不对" + filePath + ",请检查");
		}
		FSDataInputStream hdfsIS = null;
		ByteArrayOutputStream baos = null;
		try {
			FileSystem fs = FileSystem.get(hadoopConf);
			Path hdfsPath = new Path(filePath);
			hdfsIS = fs.open(hdfsPath);
			byte[] b = new byte[65536];
			baos = new ByteArrayOutputStream();
			int flag = -1;
			while ((flag = hdfsIS.read(b)) != -1) {
				baos.write(b);
				// baos.write(flag);//不成功
				b = new byte[65536];
			}
			result = baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			CloseUtil.close(hdfsIS);
			CloseUtil.close(baos);
		}

		return result;
	}

	public static String readFileToString(String filePath) throws Exception {
		String result = null;
		result = new String(readFileToByteArray(filePath), "utf-8");
		return result;
	}

	public static boolean writeFile(byte[] content, String toFilePath)
			throws Exception {
		boolean isFinish = false;
		if (toFilePath == null || toFilePath.trim().length() == 0) {
			throw new Exception("文件路径不对" + toFilePath + ",请检查");
		}
		FSDataOutputStream hdfsOS = null;
		try {
			FileSystem fs = FileSystem.get(hadoopConf);
			Path hdfsPath = new Path(toFilePath);
			hdfsOS = fs.create(hdfsPath);
			hdfsOS.write(content);
			isFinish = true;
		} catch (Exception e) {
			isFinish = false;
			e.printStackTrace();
		} finally {
			CloseUtil.close(hdfsOS);
		}
		return isFinish;
	}

	/**
	 * 查看某个目录下的所有文件
	 * 
	 * @throws IOException
	 *
	 */

	public static String readFilesListToString(String filesPath)
			throws IOException {
		StringBuffer sb = new StringBuffer();
		FileSystem fs = FileSystem.get(hadoopConf);
		FileStatus[] fileStatuses = fs.listStatus(new Path(filesPath));
		for (FileStatus fileStatus : fileStatuses) {
			String path = fileStatus.getPath().toString();
			sb.append(path + " ");
		}

		return sb.toString();
	}

}

集群本地文件操作的工具类

package cn.tl.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import org.apache.commons.io.output.ByteArrayOutputStream;

public class LocalFileOperatorUtil {
	public static byte[] readFileToByte(String filePath) throws Exception{
		byte[] result = null;
		if (filePath == null || filePath.trim().length() == 0) {
			throw new Exception("文件路径不对" + filePath + ",请检查");
		}
		BufferedInputStream bis = null;
		ByteArrayOutputStream baos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(filePath));
			baos = new ByteArrayOutputStream();
			int reads = -1;
			while((reads = bis.read()) != -1){
				baos.write(reads);
			}
			result = baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			CloseUtil.close(baos);
			CloseUtil.close(bis);
		}
		
		return result;
	}

	public static String readFileToString(String filePath) throws Exception{
		String result = null;
		if (filePath == null || filePath.trim().length() == 0) {
			throw new Exception("文件路径不对" + filePath + ",请检查");
		}
		result = new String(readFileToByte(filePath), "utf-8");
		return result;
	}

}

附CloseUtil工具类

package cn.tl.util;

public class CloseUtil {

	public static void close(AutoCloseable obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

测试类

package cn.tl.dataCirculation;

import java.io.File;

import cn.tl.util.HdfsFileOperatorUtil;
import cn.tl.util.LocalFileOperatorUtil;

public class readHtml {

	public static void main(String[] args) throws Exception {
		String filePath = File.separator + "home" + File.separator + "zhangxin"
				+ File.separator + "index.html";// 本地路径
		String toFilePath = File.separator + "user" + File.separator
				+ "zhangxin" + File.separator + "index.html";// hdfs路径
		System.out.println("reading.....");
		byte[] readToByte = LocalFileOperatorUtil.readFileToByte(filePath);
		System.out.println("readEnd,writing....");
		boolean flag = HdfsFileOperatorUtil.writeFile(readToByte, toFilePath);
		String result = HdfsFileOperatorUtil.readFileToString(toFilePath);
		System.out.println(result);
		System.out.println("done");
		System.out.println(flag);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值