文章目录
1, hbase java api
a, 使用kerberos认证: UserGroupInformation.loginUserFromKeytab(kerberosUser, keytabPath)
import org.apache.hadoop.security.UserGroupInformation;
2, 具体代码
- maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0</version>
</dependency>
import com.google.protobuf.ServiceException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
public class TestKerberosHbase {
public static void main(String[] args) throws IOException, ServiceException {
// 注意加载:hbase-site.xml等配置文件到resources目录下,并打入jar包
System.setProperty("java.security.krb5.realm","CDH.COM");
System.setProperty("java.security.krb5.kdc","cdh-node2");
Configuration conf = HBaseConfiguration.create();
UserGroupInformation.setConfiguration(conf);
final String user = "hbase/cdh-node1@CDH.COM";
final String keyPath = "/hbase.keytab";
UserGroupInformation.loginUserFromKeytab(user, keyPath);
HTable table = new HTable(conf, "t1");
System.out.println("tablename:" + new String(table.getTableName()));
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println(r.toString());
}
}
}