/**
* 进行kerberos认证获取连接
* @return
* @throws IOException
* @throws InterruptedException
*/
private static Connection getConnection() throws IOException, InterruptedException {
String user = "bdp/[email protected]";
String keytabPath = "D:\\bdp.keytab";
String realm = "NBDP.COM";
String kdc = "host165";
System.setProperty("sun.security.krb5.debug", "false");
System.setProperty("java.security.krb5.kdc", kdc);
System.setProperty("java.security.krb5.realm", realm);
final Configuration conf = HBaseConfiguration.create();
//设置连接信息
conf.set("hbase.zookeeper.quorum", "172.21.72.165");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.client.retries.number", "2");
//设置kerberos认证配置
conf.set("hadoop.security.authentication", "kerberos");
conf.set("hadoop.security.authorization", "true");
conf.set("hbase.security.authentication", "kerberos");
conf.set("hbase.security.authorization", "true");
conf.set("hbase.master.kerberos.principal", "hbase/_HOST@" + realm);
conf.set("hbase.regionserver.kerberos.principal", "hbase/_HOST@" + realm);
conf.set("hbase.thrift.kerberos.principal", "hbase/_HOST@" + realm);
UserGroupInformation.setConfiguration(conf);
UserGroupInformation userGroupInformation = UserGroupInformation.loginUserFromKeytabAndReturnUGI(user,
keytabPath);
UserGroupInformation.setLoginUser(userGroupInformation);
boolean hasKerberosCredentials = userGroupInformation.hasKerberosCredentials();
if (!hasKerberosCredentials) {
throw new RuntimeException("认证失败!");
}
Connection connection = userGroupInformation.doAs(new PrivilegedExceptionAction() {
@Override
public Connection run() throws Exception {
//获取连接
Connection connection = ConnectionFactory.createConnection(conf);
return connection;
}
});
return connection;
}
/**
* 创建hbase命名空间
* @param connection
* @throws IOException
*/
public static void createSchema(Connection connection) throws IOException {
String namespace = "smart_test";
Admin admin = connection.getAdmin();
NamespaceDescriptor[] namespaceDescriptorList = admin.listNamespaceDescriptors();
for (int i = 0; i < namespaceDescriptorList.length; i++) {
System.out.println("当前存在的命名空间是:" + namespaceDescriptorList[i].getName());
}
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
admin.createNamespace(namespaceDescriptor);
}
/**
* 创建HBASE表
* @param connection
* @throws IOException
*/
public static void createTable(Connection connection) throws IOException {
Admin admin = connection.getAdmin();
TableName myuser = TableName.valueOf("smart_test:user");
//添加列族
HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
HColumnDescriptor info = new HColumnDescriptor("info");
HColumnDescriptor course = new HColumnDescriptor("course");
hTableDescriptor.addFamily(info);
hTableDescriptor.addFamily(course);
//创建表
admin.createTable(hTableDescriptor);
}
/**
* 删除删除hbase表
* @param connection
* @throws IOException
*/
public static void deleteTable(Connection connection) throws IOException {
Admin admin = connection.getAdmin();
admin.disableTable(TableName.valueOf("smart_test:user"));
admin.deleteTable(TableName.valueOf("smart_test:user"));
}
/**
* 单条记录插入数据
* @param connection
* @throws IOException
*/
public static void insert(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf("student"));
Put put = new Put(Bytes.toBytes("7"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小杜"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("19"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("洛溪大路二街403"));
table.put(put);
}
/**
* 批量插入数据
* @param connection
* @throws IOException
*/
public static void batchInsert(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf("student"));
List list = new ArrayList();
Put put = new Put(Bytes.toBytes("8"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小黄"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("17"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("广州市会江镇"));
list.add(put);
Put put2 = new Put(Bytes.toBytes("9"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小杜"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(20));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("洛溪大路中心二街2栋403"));
list.add(put2);
table.put(list);
}
/**
* 根据rowKey进行查询
* @param connection
* @param rowKey
* @throws IOException
*/
public static void queryByRowKey(Connection connection, String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf("student"));
Get get = new Get("1".getBytes());
Result result = table.get(get);
KeyValue[] raw = result.raw();
for (KeyValue kv : raw) {
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " = ");
System.out.print(new String(kv.getValue()));
System.out.print(" timestamp = " + kv.getTimestamp() + "\n");
}
}
/**
* 批量查询
* @param connection
* @throws Exception
*/
public static void batchQuery(Connection connection) throws Exception {
Table table = connection.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.setStartRow("1".getBytes());
scan.setStopRow("9".getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] rowKeyBytes = row.getRow();
byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());
String rowKey = Bytes.toString(rowKeyBytes);
String name = Bytes.toString(nameBytes);
String age = Bytes.toString(ageBytes);
String address = Bytes.toString(addressBytes);
System.out.print(rowKey);
System.out.print(name);
System.out.print(age);
System.out.print(address);
System.out.println();
}
}
/**
* 把查询结果封装成Map
* @param connection
* @throws Exception
*/
public static void batchQueryMap(Connection connection) throws Exception {
List
/**
* 删除记录
* @param connection
* @throws IOException
*/
public static void deleteByRowKey(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf("student"));
Delete del = new Delete(Bytes.toBytes("9"));
table.delete(del);
}
/**
* RowFilter过滤器查询
* @param connection
* @throws Exception
*/
public static void batchQueryRowFilter(Connection connection) throws Exception {
//匹配以1开头的ROWKEY
RowFilter rowFilter = new RowFilter(RowFilter.CompareOp.EQUAL, new RegexStringComparator("^1"));
Table table = connection.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.setStartRow("1".getBytes());
scan.setStopRow("9".getBytes());
scan.setFilter(rowFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] rowKeyBytes = row.getRow();
byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());
String rowKey = Bytes.toString(rowKeyBytes);
String name = Bytes.toString(nameBytes);
String age = Bytes.toString(ageBytes);
String address = Bytes.toString(addressBytes);
System.out.print(rowKey);
System.out.print(name);
System.out.print(age);
System.out.print(address);
System.out.println();
}
}
/**
* @param connection
* @throws Exception
*/
public static void batchQueryFilter(Connection connection) throws Exception {
//多列值前缀过滤器
byte[][] prefixes = new byte[][] { Bytes.toBytes("name"), Bytes.toBytes("age"), Bytes.toBytes("address") };
MultipleColumnPrefixFilter multipleColumnPrefixFilter = new MultipleColumnPrefixFilter(prefixes);
//单列值过滤器,过滤出地址包含luoxi的
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"),
Bytes.toBytes("address"), CompareOp.EQUAL, Bytes.toBytes("luoxi"));
FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
filterList.addFilter(singleColumnValueFilter);
filterList.addFilter(multipleColumnPrefixFilter);
Table table = connection.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.setStartRow("1".getBytes());
scan.setStopRow("9".getBytes());
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] rowKeyBytes = row.getRow();
byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());
String rowKey = Bytes.toString(rowKeyBytes);
String name = Bytes.toString(nameBytes);
String age = Bytes.toString(ageBytes);
String address = Bytes.toString(addressBytes);
System.out.print(rowKey);
System.out.print(name);
System.out.print(age);
System.out.print(address);
System.out.println();
}
}