我是靠谱客的博主 谦让老鼠,最近开发中收集的这篇文章主要介绍java kerberos HDFSFileService,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package filesysetm.core;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;

public class HDFSFileService {

        @Value("${filesystem.path}")
	protected String basePath;

	private Configuration conf;

	public HDFSFileService() throws IOException {
		super();
		conf = new Configuration();
	}

	public HDFSFileService(final String krbConfPath, String keytabPath, final String hdfsPath,
			final String hdfsKeberosUsername, final String domain) throws IOException {
		super();
		System.setProperty("java.security.krb5.conf", krbConfPath);
		conf = new Configuration();
		conf.set("hadoop.security.authentication", "kerberos");
		conf.set("fs.defaultFS", hdfsPath);
		UserGroupInformation.setConfiguration(conf);
		if ("admin".equalsIgnoreCase(hdfsKeberosUsername)) {
			keytabPath = keytabPath + File.separator + "sixshot.keytab";
		} else {
			keytabPath = keytabPath + File.separator + hdfsKeberosUsername.trim() + ".keytab";
		}
		UserGroupInformation.loginUserFromKeytab(hdfsKeberosUsername + "@" + domain, keytabPath);
	}

	public String save(final String filename, final InputStream inputStream) {
		OutputStream out = null;

		try {
			// //如果开启了Keberos安全,则登录
			// if(hdfsKeberosEnabled) {
			// UserGroupInformation.setConfiguration(conf);
			// UserGroupInformation.loginUserFromKeytab(hdfsKeberosUsername+"@COM",ktPath);
			// }

			Path path = new Path(getAbsolutePath(filename));
			FileSystem fs = path.getFileSystem(conf);
			out = fs.create(path);

			String md5 = copyBytesAndCheckMD5(inputStream, out, BUFFER_SIZE);
			return md5;
			// IOUtils.copyBytes(in, out, conf);
		} catch (IOException | NoSuchAlgorithmException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public InputStream read(final String filepath) {
		try {
			Path path = new Path(getAbsolutePath(filepath));
			FileSystem fs = path.getFileSystem(conf);
			return fs.open(path);
		} catch (IllegalArgumentException | IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean delete(final String filepath) {
		try {
			Path path = new Path(getAbsolutePath(filepath));
			FileSystem fs = path.getFileSystem(conf);
			return fs.delete(path, true);
		} catch (IllegalArgumentException | IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean deleteDir(final String path) {
		// TODO Auto-generated method stub
		return false;
	}

	public String getAbsolutePath(final String relativePath) {
		if (relativePath.startsWith("hdfs")) {
			return relativePath;
		}
		return basePath + relativePath;
	}

	public boolean mkdir(final String filepath) {
		try {
			Path path = new Path(getAbsolutePath(filepath));
			FileSystem fs = path.getFileSystem(conf);
			return fs.mkdirs(path);
		} catch (IllegalArgumentException | IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean exist(final String filepath) {
		try {
			Path path = new Path(getAbsolutePath(filepath));
			FileSystem fs = path.getFileSystem(conf);
			return fs.exists(path);
		} catch (IllegalArgumentException | IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public OutputStream create(final String filename) {
		try {
			Path path = new Path(getAbsolutePath(filename));
			FileSystem fs = path.getFileSystem(conf);
			OutputStream out = fs.create(path);
			return out;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

 

最后

以上就是谦让老鼠为你收集整理的java kerberos HDFSFileService的全部内容,希望文章能够帮你解决java kerberos HDFSFileService所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部