hbaseなどのコードにおけるkinit

2452 ワード

hadoop/hbaseなどのコードでkinit
@(ブログ記事)[hadoop,hbase,storm,kafka]
(一)javaコードにおけるkinitの方法
hadoopのUserGroupInformation 1、Set Kerberos login with the UserGroupInformation APIを使用する:
import org.apache.hadoop.security.UserGroupInformation;
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();  
//  ,   set()   zk   。
conf.addResource("/path","hbase-site.xml");
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);

2、Login with a keytab by calling the UserGroupInformation API:
UserGroupInformation.loginUserFromKeytab("[email protected]", "/path/to/example_user.keytab");

完全なコード:
public class HBaseKerberosDemo {

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

        Configuration conf = HBaseConfiguration.create();
        //  ,   set()   zk   。
        conf.addResource("/path","hbase-site.xml");
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(args[0], args[1]);

        Connection connection = ConnectionFactory.createConnection(conf);

        Table tbl = connection.getTable(TableName.valueOf(args[2]));
        Put put = new Put(Bytes.toBytes("r1"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("c1"), Bytes.toBytes("v1"));
        tbl.put(put);
        tbl.close();
        connection.close();

    }   
}

(二)タイミングリフレッシュ
kinitにはタイムアウト時間があり、プログラムが長時間実行される必要がある場合は、タイミングを決めてリフレッシュする必要があり、1つのスレッドで実現できます.
このようなアプリケーションシーンはstormによく見られ、stormではこのタイミング機能を実現するためにTickTupleが提供される.タイミングrefreshを必要としないことが実証され、stormがhbaseを操作する必要があるboltのprepare()メソッドに以下のコードが追加された.
    Configuration config = HBaseConfiguration.create();
    //        ,              ,        hbase-site.xml。
    config.addResource(new Path("/home/hadoop/conf/hbase", "hbase-site.xml"));
    config.set("hadoop.security.authentication", "Kerberos");
    UserGroupInformation.setConfiguration(config);
    try {
    UserGroupInformation.loginUserFromKeytab("[email protected]", "/path/to/example_user.keytab");
    } catch (IOException e) {
        e.printStackTrace();
    }