Java开发~Eclipse访问集群hdfs的配置和参考

·1、准备工作

将hadoop-eclipse-plugin-2.6.0.jar放于eclipse的plugin目录下

(hadoop-eclipse-kepler-plugin-2.2.0.jar、hadoop-eclipse-kepler-plugin-2.4.1.jar也建议放进去)

2、配置

解压hadoop-2.6.0-cdh5.4.7.tar到目录下,配置环境变量如下:

3、相关依赖

将以下几个文件放到%HADOOP_HOME%\bin目录下:

4、配置文件

将集群中的配置文件放到%HADOOP_HOME%\conf目录下,主要文件如下:

5、创建工程

启动eclipse,建立MapReduce Project,程序demo代码如下:

public class accessHDFS {
	private static final Log log = LogFactory.getLog(accessHDFS.class);

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration();
		conf.set("hadoop.security.authentication", "Kerberos");
		System.setProperty("java.security.krb5.realm", "BIGDATAS.COM");
		System.setProperty("java.security.krb5.kdc", "secnamenode");
		UserGroupInformation.setConfiguration(conf);
        //keytab文件及用户名根据分配权限修改
		UserGroupInformation.loginUserFromKeytab("jia@BIGDATAS.COM",
				"F:\\hadoop\\hadoop-2.6.0\\conf\\jia.keytab");

		conf.set("fs.default.name", "hdfs://namenode:8020");
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path("/user/jia/write111.txt");
		//文件列表
		listDir(conf,"/user");
		//读取文件内容
		/*FileSystem hdfs = FileSystem.get(conf);
		  InputStream in = null;
		  try{
		  in = hdfs.open(path);
		  IOUtils.copyBytes(in,System.out,4096,true);
		  }finally{
		  IOUtils.closeStream(in);
		  }*/
		//写文件
		/*
		 * FSDataOutputStream out = fs.create(path); out.writeBytes(
		 * "da jia hao,cai shi zhen de hao!\nda jia hao,cai shi zhen de hao");
		 * fs.close();
		 */
        //读文件
		/*if (fs.exists(path)) {
			FSDataInputStream is = fs.open(path);
			FileStatus status = fs.getFileStatus(path);
			byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];
			is.readFully(0, buffer);
			System.out.println(String.valueOf(status.getLen()));
			is.close();
			fs.close();
			System.out.println(buffer.toString());
		}*/
	}

	public static void listDir(Configuration conf, String dir) throws IOException {
		FileSystem fs = FileSystem.get(conf);
		FileStatus files[] = fs.listStatus(new Path(dir));
		for (FileStatus file : files) {
			System.out.println(file.getPath());
		}
	}

}

6、执行

在eclipse中添加环境变量如下,value即conf所在目录:

欢迎关注交流:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

runqu

你的鼓励是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值