HBase可見性標簽管理(Administration)

2018-05-03 14:03 更新

可見性標簽管理(Administration)

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

  1. 定義可見性標簽列表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. 將標簽與用戶關聯(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. 清除用戶的標簽: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. 將標簽或表達式應用于單元格:該標簽僅適用于數(shù)據(jù)寫入時。該標簽與給定版本的單元格相關聯(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支持將標簽或權限應用于單元格以進行測試和驗證支持,不應將其用于生產(chǎn)使用,因為它不會將標簽應用于尚不存在的單元格。應用單元級別標簽的正確方法是在存儲值時在應用程序代碼中執(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();
    }
  }

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號