场景
大数据平台的使用者用户(租户),在创建namespace时,根据其使用量为其配置可用空间大小,超过限制则禁止继续写入数据。
代码实现
public static void main(String[] args) {
//================创建hbase连接(开启kerberos认证)================
Connection connection =null;
Configuration hbaseConf = HBaseConfiguration.create();
//zookeeper连接地址 (xxx:2181)
hbaseConf.set("hbase.zookeeper.quorum",zookeeperUrl);
// hbaseConf.set("hbase.zookeeper.property.clientPort","2181");
//parent目录 (/hbase)
hbaseConf.set("zookeeper.znode.parent",znodeParent);
//重试次数
hbaseConf.set("hbase.client.retries.number","3");
//kerberos认证相关配置
hbaseConf.set("hbase.security.authentication","kerberos");
//keytab路径
hbaseConf.set("keytab.file",keytabPath);
//登录用户principal (用户名)
hbaseConf.set("kerberos.principal",user);
//主节点principal (kerberos kinit认证时显示该参数)
hbaseConf.set("hbase.master.kerberos.principal","hbase/_HOST@xxxxx");
hbaseConf.set("hbase.regionserver.kerberos.principal","hbase/_HOST@xxxxx");
//krb5路径
System.setProperty("java.security.krb5.conf",krbPath);
//应用配置
try {
UserGroupInformation.setConfiguration(hbaseConf);
UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(user, keytabPath);
UserGroupInformation.setLoginUser(ugi);
log.info("kerberos认证成功");
}catch (Exception e){
log.error("kerberos认证失败"+e.getMessage(),e);
}
try {
connection = ConnectionFactory.createConnection(hbaseConf);
} catch (IOException e) {
e.printStackTrace();
}
//================创建hbase连接完成================
//转换单位(省略)...
//XXXXXX
//配置限额
if("1".equals("1")){ //仅示例,判断省略
/**
* 参数解释
* size 此处配置空间限额大小,Long类型字节数,注意单位转换
* SpaceViolationPolicy.NO_WRITES 在达到限额后的限制策略
*/
QuotaSettings setQuota = QuotaSettingsFactory.limitNamespaceSpace(namespace, size, SpaceViolationPolicy.NO_WRITES);
try {
connection.getAdmin().setQuota(setQuota);
} catch (IOException e) {
e.printStackTrace();
}
}
else {
//删除限额
QuotaSettings removeQuota =QuotaSettingsFactory.removeNamespaceSpaceLimit(namespace);
try {
connection.getAdmin().setQuota(removeQuota);
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
//XXX
}
源码中的策略解释
package org.apache.hadoop.hbase.quotas;
import org.apache.yetus.audience.InterfaceAudience.Public;
@Public
public enum SpaceViolationPolicy {
//禁用表
DISABLE,
//不可写入和压缩
NO_WRITES_COMPACTIONS,
//不可写入,但可压缩
NO_WRITES,
//不可插入
NO_INSERTS;
private SpaceViolationPolicy() {
}
}
注:
由于HBase写入数据时,内存和磁盘可能同时有数据,此配额只能限制磁盘大小,无法限制内存中的数据大小,只有当内存数据刷写到磁盘后达到限额时才会触发,所以当快速写入数据时,实际表大小可能会超过限额。具体差额由hbase flush刷写配置相关。同时也由于此原因,数据一旦写入到了内存中即使超过限额最终也会写到磁盘,正常情况下不会丢失数据。