java 连接 kerberos 认证的 HBase 和 HDFS

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

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

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());
        }
    }
}

水了一篇

posted @ 2017-08-30 17:48  之奇一昂  阅读(1741)  评论(0编辑  收藏  举报