与hive类似,Hbase的连接可以使用命令行和Java API,另外Hbase还可以使用thrift和rest进行连接。
命令行
在使用hbase shell命令打开hbase交互界面之前使用kinit命令进行认证即可。
HBase native APIs
同hive连接一样,http://192.168.2.206:8090/x/ZIAU,需添加hadoop.security.authentication的Kerberos设置和使用UserGroupInformation进行认证。
package com.EXAMPLE.hbasetest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.hbase.client.HTable;
public class Hbasetest {
static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "test-1");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hadoop.security.authentication", "Kerberos");
conf.set("hbase.security.authentication", "kerberos");
conf.set("hbase.master.kerberos.principal", "hbase/test-1@EXAMPLE.COM");
conf.set("hbase.regionserver.kerberos.principal", "hbase/_HOST@EXAMPLE.COM");
//上面一行必须和服务器中hbase-site.xml文件中的配置一样,这里principal的FQDN部分必须写成"_HOST",原因在于regionserver在test-1和test-2上都有布置
UserGroupInformation.setConfiguration(conf);
System.out.println("Principal Authentication: ");
final String user = "jing/test-1@JUNIORCHINA.COM";
final String keyPath = "/home/jing/jing.keytab";
try {
UserGroupInformation.loginUserFromKeytab(user, keyPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void putMap(Map<String, Object> map, Result result, String tablename, String name)
throws UnsupportedEncodingException {
byte[] b = null;
b = result.getValue(Bytes.toBytes(tablename), Bytes.toBytes(name));
if (b != null) {
String bstr = new String(b, "UTF-8");
map.put(name, bstr);
} else {
map.put(name, null);
}
}
public static void main(String[] args) throws IOException {
String tableName = "EXAMPLE";
String rowKey = "EXAMPLE";
Get get = new Get(Bytes.toBytes(rowKey));
HTable table = new HTable(conf, Bytes.toBytes(tableName));// 获取表
Result result = table.get(get);
Map<String, Object> map = new HashMap<String, Object>();
putMap(map, result, "tablename", "name");
for (int i = 0; i < 10; i++) {
System.out.println(map.get("name"));
}
table.close();
}
}
Dependency
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.6-cdh5.3.0</version>
</dependency>
Thrift
thrift提供了各种语言对hbase的访问,下面以java为例
package com.example.thrifttest;
import org.apache.hadoop.hbase.thrift.generated.Hbase;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class Thrifttest {
static {
System.out.println("Principal Authentication: ");
final String user = "jing/test-1@EXAMPLE.COM";
final String keyPath = "/home/jing/jing.keytab";
try {
UserGroupInformation.loginUserFromKeytab(user, keyPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String byteBufferToString(ByteBuffer buffer) {
CharBuffer charBuffer = null;
try {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
buffer.flip();
return charBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private void start() {
try {
TTransport socket = new TSocket("192.168.x.xxx", 9090);
TProtocol protocol = new TBinaryProtocol(socket, true, true);
Hbase.Client client = new Hbase.Client(protocol);
socket.open();
System.out.println("open");
try {
System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) {
System.out.println("find:" + byteBufferToString(name));
}
} catch (Exception e) {
e.printStackTrace();
}
socket.close();
System.out.println("close");
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thrifttest c = new Thrifttest();
c.start();
}
}
错误
在之前使用Hbase Java API时运行时报错:
javax.security.sasl.SaslException: GSS initiate failed [Caused by org.apache.hadoop.ipc.RemoteException(javax.security.sasl.SaslException): GSS initiate failed]
原因在于代码中
conf.set("hbase.regionserver.kerberos.principal", "hbase/_HOST@EXAMPLE.COM");
这一行_HOST被写成了test-1,手动指定了regionserver。
可以参见
http://stackoverflow.com/questions/36265416/not-able-to-communicate-with-kerberos-enabled-hbase
以及
http://stackoverflow.com/questions/23561736/java-client-for-secure-hbase