HBase可見(jiàn)性標(biāo)簽管理(Administration)

2018-05-03 14:03 更新

可見(jiàn)性標(biāo)簽管理(Administration)

管理(Administration)任務(wù)可以使用HBase Shell或Java API執(zhí)行。為了定義可見(jiàn)性標(biāo)簽并將標(biāo)簽與用戶(hù)關(guān)聯(lián),HBase Shell可能更簡(jiǎn)單。

  1. 定義可見(jiàn)性標(biāo)簽列表HBase Shell
    hbase> add_labels [ 'admin', 'service', 'developer', 'test' ]
    示例:
    public static void addLabels() throws Exception {
      PrivilegedExceptionAction<VisibilityLabelsResponse> action = new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
        public VisibilityLabelsResponse run() throws Exception {
          String[] labels = { SECRET, TOPSECRET, CONFIDENTIAL, PUBLIC, PRIVATE, COPYRIGHT, ACCENT,
              UNICODE_VIS_TAG, UC1, UC2 };
          try {
            VisibilityClient.addLabels(conf, labels);
          } catch (Throwable t) {
            throw new IOException(t);
          }
          return null;
        }
      };
      SUPERUSER.runAs(action);
    }
  2. 將標(biāo)簽與用戶(hù)關(guān)聯(lián):HBase Shell 
    hbase> set_auths 'service', [ 'service' ]
hbase> set_auths'testuser',['test']
hbase> set_auths'qa',['test','developer']
hbase> set_auths'@qagroup',['test']

Java API

public void testSetAndGetUserAuths() throws Throwable {
  final String user = "user1";
  PrivilegedExceptionAction<Void> action = new PrivilegedExceptionAction<Void>() {
    public Void run() throws Exception {
      String[] auths = { SECRET, CONFIDENTIAL };
      try {
        VisibilityClient.setAuths(conf, auths, user);
      } catch (Throwable e) {
      }
      return null;
    }
    ...
  1. 清除用戶(hù)的標(biāo)簽:HBase Shell 
    hbase> clear_auths 'service', [ 'service' ]
hbase> clear_auths'testuser',['test']
hbase> clear_auths'qa',['test','developer']
hbase> clear_auths'@qagroup',['test','developer']
    

Java API

...
auths = new String[] { SECRET, PUBLIC, CONFIDENTIAL };
VisibilityLabelsResponse response = null;
try {
  response = VisibilityClient.clearAuths(conf, auths, user);
} catch (Throwable e) {
  fail("Should not have failed");
  ...
}
  1. 將標(biāo)簽或表達(dá)式應(yīng)用于單元格:該標(biāo)簽僅適用于數(shù)據(jù)寫(xiě)入時(shí)。該標(biāo)簽與給定版本的單元格相關(guān)聯(lián)。HBase Shell 
    hbase> set_visibility 'user', 'admin|service|developer', { COLUMNS => 'i' }
hbase> set_visibility'user','admin | service',{COLUMNS =>'pii'}
hbase> set_visibility'user','test',{COLUMNS => ['i','pii'],F(xiàn)ILTER =>“(PrefixFilter('test'))”}

注意:HBase Shell支持將標(biāo)簽或權(quán)限應(yīng)用于單元格以進(jìn)行測(cè)試和驗(yàn)證支持,不應(yīng)將其用于生產(chǎn)使用,因?yàn)樗粫?huì)將標(biāo)簽應(yīng)用于尚不存在的單元格。應(yīng)用單元級(jí)別標(biāo)簽的正確方法是在存儲(chǔ)值時(shí)在應(yīng)用程序代碼中執(zhí)行此操作。

Java API

static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  Configuration conf = HBaseConfiguration.create();
  Connection connection = ConnectionFactory.createConnection(conf);
  Table table = NULL;
  try {
    table = TEST_UTIL.createTable(tableName, fam);
    int i = 1;
    List<Put> puts = new ArrayList<Put>();
    for (String labelExp : labelExps) {
      Put put = new Put(Bytes.toBytes("row" + i));
      put.add(fam, qual, HConstants.LATEST_TIMESTAMP, value);
      put.setCellVisibility(new CellVisibility(labelExp));
      puts.add(put);
      i++;
    }
    table.put(puts);
  } finally {
    if (table != null) {
      table.flushCommits();
    }
  }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)