4.HDFS应用_Java API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15014327/article/details/83033267

一.开发环境搭建

使用IDE工具(Eclipse或Idea)创建一个Maven工程,导入Hadoop依赖包。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.tl</groupId>
	<artifactId>Hdp-core</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Hdp-core</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<!-- 首先配置仓库的服务器位置,首选阿里云,也可以配置镜像方式,效果雷同 -->
	<repositories>
		<repository>
			<id>nexus-aliyun</id>
			<name>Nexus aliyun</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public</url>
		</repository>
	</repositories>
	<dependencies>
		<!-- 引入hadoop-cli-2.7.4依赖 -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.4</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>Hdp-core</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>

			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>assembly</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

二.代码

package cn.tl.hdfs;
/**
 * Hdfs Java工具类
 * @author lw
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
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 HdfsUtil {

	// 日志
	public static Logger logger = Logger.getLogger(HdfsUtil.class);

	// 集群环境配置
	public static Configuration conf = new Configuration();

	/**
	 * 在hdfs上创建目录
	 * 
	 * @param 创建目录
	 * @return
	 * @throws Exception
	 */
	public static boolean mkdir(String src) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
			logger.error("要创建的目录不能为空!");
			return false;
		}
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(src);
		boolean isSuccess = fs.mkdirs(path);
		return isSuccess;
	}

	/**
	 * 在hdfs上删除目录
	 * 
	 * @param 要刪除的目录
	 * @param 是否递归
	 * @return
	 * @throws Exception
	 */
	public static boolean rmdir(String src, boolean recursive) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
			logger.error("要删除的目录不能为空!");
			return false;
		}
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(src);
		boolean isSuccess = fs.delete(path, recursive);
		return isSuccess;
	}

	/**
	 * 在hdfs上删除文件
	 * 
	 * @param 要刪除的文件
	 * @return
	 * @throws Exception
	 */
	public static boolean rmFile(String src) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
			logger.error("文件名不能为空!");
			return false;
		}
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(src);
		if (fs.isDirectory(path)) {
			logger.error(src + ":是一个目录,请检查传入的参数!");
			return false;
		}
		boolean isSuccess = fs.delete(path);
		return isSuccess;
	}

	/**
	 * 向hdfs上传文件
	 * 
	 * @param 是否删除源文件
	 * @param 是否覆盖hdfs文件
	 * @param 源路径
	 * @param 目标路径
	 * @throws Exception
	 */
	public static void copyFromLocal(boolean delSrc, boolean overwrite, String src, String dst) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0 || StringUtils.isEmpty(dst)
				|| dst.trim().length() == 0) {
			logger.error("源文件名或目标文件名不能为空!");
			return;
		}
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(src);
		Path dstPath = new Path(dst);
		fs.copyFromLocalFile(delSrc, overwrite, srcPath, dstPath);
	}

	/**
	 * 从hdfs下载文件
	 * 
	 * @param 是否删除源文件
	 * @param 源路径
	 * @param 目标路径
	 * @throws Exception
	 */
	public static void copyToLocal(boolean delSrc, String src, String dst) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0 || StringUtils.isEmpty(dst)
				|| dst.trim().length() == 0) {
			logger.error("源文件名或目标文件名不能为空!");
			return;
		}
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(src);
		Path dstPath = new Path(dst);
		fs.copyToLocalFile(delSrc, srcPath, dstPath);
	}

	/**
	 * 列出该目录的文件
	 * 
	 * @param 要查看的目录
	 * @return
	 * @throws Exception
	 */
	public static FileStatus[] listFileStatus(String src) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
			logger.error("目录不能为空!");
			return null;
		}
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(src);
		return fs.listStatus(path);
	}

	/**
	 * 读文件
	 * 
	 * @param src
	 * @return
	 * @throws Exception
	 */
	public static byte[] readFile(String src) throws Exception {
		if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
			logger.error("文件名不能为空!");
			return null;
		}
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(src);
		FSDataInputStream in = fs.open(path);

		ByteArrayOutputStream out = new ByteArrayOutputStream();

		byte[] b = new byte[1024];
		int len = 0;
		while ((len = in.read(b)) != -1) {
			out.write(b, 0, len);
		}
		in.close();
		out.close();
		return out.toByteArray();
	}

	/**
	 * 写文件
	 * 
	 * @param src
	 * @param hdfs
	 * @param overwrite
	 * @return
	 */
	public static boolean writeFile(String src, String hdfs, boolean overwrite) {
		FSDataOutputStream out = null;
		try {
			if (StringUtils.isEmpty(hdfs) || hdfs.trim().length() == 0) {
				logger.error("hdfs文件名不能为空!");
				return false;
			}
			if (StringUtils.isEmpty(src) || src.trim().length() == 0) {
				logger.error("src文件名不能为空!");
				return false;
			}
			FileSystem fs = FileSystem.get(conf);

			Path path = new Path(hdfs);
			out = fs.create(path, overwrite);

			byte[] b = readFile(src);
			out.write(b);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_15014327/article/details/83033267
今日推荐