码迷,mamicode.com
首页 > 编程语言 > 详细

java 连接 kerberos 认证的 HBase 和 HDFS

时间:2017-08-30 18:28:14      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:str   actor   eof   hba   cat   length   out   ber   ++   

这是两个功能,都很简单就写一块了。。

简单到什么程度呢,简单到只贴代码就可以了。。。

HBase

package com.miras.data;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;

/**
 * @author sunzq
 * @since 2017/8/30
 */
public class HbaseMain {
    public static void main(String[] args) throws IOException {
        System.setProperty("java.security.krb5.conf", "/app/conf/krb5.conf");
        Configuration conf = HBaseConfiguration.create();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab("storm-miras", "/app/conf/storm.headless.keytab");
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf("hbase_krb"));
        Put put = new Put(Bytes.toBytes(2));
        put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("world"));
        table.put(put);
        table.close();
        connection.close();
    }
}

 

HDFS

package com.miras.data;

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

import java.io.IOException;

/**
 * @author sunzq
 * @since 2017/8/30
 */
public class HdfsMain {
    public static void main(String[] args) throws IOException {
        System.setProperty("java.security.krb5.conf", "/app/conf/krb5.conf");
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://node1:8020");
        conf.set("hadoop.security.authentication", "kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab("storm-miras", "/app/conf/storm.headless.keytab");
        FileSystem fs = FileSystem.get(conf);
        FileStatus[] fsStatus = fs.listStatus(new Path("/"));
        for (int i = 0; i < fsStatus.length; i++) {
            System.out.println(fsStatus[i].getPath().toString());
        }
    }
}

水了一篇

java 连接 kerberos 认证的 HBase 和 HDFS

标签:str   actor   eof   hba   cat   length   out   ber   ++   

原文地址:http://www.cnblogs.com/kischn/p/7454550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!