java API 访问带有Kerberos认证的HBase时window服务器的配置

首先第一步,是确定的代码运行在window服务器还是linux服务器下, 如果是window服务器下,首先要检测window服务器是否可以连接的通 hadoop平台的zk (2181端口) (telnet xxx.xxx.xxx.xxx 2181)确定连接无误的情况下,进行连接hbase服务器的验证操作(60020端口) (telnet xxx.xxx.xxx.xxx 60020)两者都通的话。下一步就是去的认证了。

首先,安装Kerberos客户端 , 安装windows版本的Kerberos客户端( 链接:http://pan.baidu.com/s/1pLdIA5l 密码:e82j) ,然后进行创建配置文件 -- krb5.ini。
/**
[libdefaults]
default_realm = 登录用户
dns_lookup_kdc = false
dns_lookup_realm = false
ticket_lifetime = 86400
renew_lifetime = 604800
forwardable = true
default_tgs_enctypes = des3-cbc-sha1
default_tkt_enctypes = des3-cbc-sha1
permitted_enctypes = des3-cbc-sha1
udp_preference_limit = 1
kdc_timeout = 3000

[realms]
FHC = {
kdc = zookeeper的服务器器地址
admin_server =zookeeper的服务器器地址
kdc = zookeeper的服务器器地址
}

[domain_realm]
.example.com =
example.com =
*/
第三步 配置window服务器的系统环境变量
如 KRB5_CONFIG=d:\krb5.ini

最后就是 初始化 Kerberos 证书,keytab文件从hadoop服务器端可以下载到XXX.keytab
(kinit.exe –kt xxx.keytab xxx@xxx)
再次出现401则需要重新执行kinit命令 (Kerberos证书24小时超时)

之后就可以写个demo测试一下啦~

附demo代码:
//这一行在window服务器下必须要加,否则读取不到配置文件
System. setProperty("java.security.krb5.conf", krb5.ini文件的window下的路径 )
config = HBaseConfiguration.create();
//其中这些xml的配置文件来自于hbase服务器的配置文件,将他们拷下来放在相应的目录下在代码里
指定他们的路径
config.addResource("E:\\HbaseConf\\hbase-site.xml");
config.addResource("E:\\HbaseConf\\core-site.xml");
config.addResource("E:\\HbaseConf\\hdfs-site.xml");
config.addResource("E:\\HbaseConf\\ssl-clinet.xml");
UserGroupInformation.setConfiguration(config);
try{
UserGroupInformation.loginUserFromKeytab("xxx", "C:\\xxx.keytab" );
htable = new HTable(config, "表名");
Put put = new Put(Bytes.toBytes("TESTROW0004"));
put.setWriteToWAL(false);
put.add(Bytes.toBytes("INFO"), Bytes.toBytes("TESTCOLUMN"),
Bytes.toBytes("TESTVALUES0004") );
htable.put(put);
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("INFO"));
ResultScanner rs = htable.getScanner(scan);
for (Result r : rs) {
String rowkey = Bytes.toString(r.getRow());
String rowvalues = Bytes.toString( r.getValue( Bytes.toBytes("INFO"),
Bytes.toBytes( "TESTCOLUMN")) );
System.out.println("== rowkey :"+ rowkey +" rowvalues :" +rowvalues);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if( htable != null ) htable.close();
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值