日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在hadoop/hbase等代码中kinit

發布時間:2024/1/23 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在hadoop/hbase等代码中kinit 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在hadoop/hbase等代碼中kinit

@(HBASE)[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("example_user@netease.com", "/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會有超時時間,如果程序需要長時間運行,則需要定時去刷新一下,可以通過一個線程來實現。

這種應用場景常見于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("g18_us/scheduler", "/home/hadoop/keytab/g18_us.keytab");} catch (IOException e) {e.printStackTrace();}

總結

以上是生活随笔為你收集整理的在hadoop/hbase等代码中kinit的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。