本文介紹在開啟hbase權(quán)限控制時,hbase shell、 shell、 jdbc代碼使用指南。
hbase版本: 1.2.0-cdh5.7.0
版本:4.8.2
文章目錄
1. 開啟hbase acl訪問控制
1.1 當(dāng)沒有開啟hbase acl訪問控制時,如果直接使用java代碼調(diào)用權(quán)限相關(guān)api會提示報錯:
org...hbase.on: hbase:acl
1.2 如果使用hbase shell操作,報錯信息如下:
hbase(main):002:0> scan ‘hbase:acl’
ROW +CELL
ERROR: table hbase:acl!
1.3 修改hbase-site.xml,添加下面配置項。重啟hbase啟用hbase acl
hbase.security.authentication
simple
hbase.security.authorization
true
hbase.coprocessor.region.classes
org.apache.hadoop.hbase.security.access.AccessController, org.apache.hadoop.hbase.security.token.TokenProvider
hbase.coprocessor.master.classes

org.apache.hadoop.hbase.security.access.AccessController
hbase.coprocessor.regionserver.classes
org.apache.hadoop.hbase.security.access.AccessController
hbase.security.exec.permission.checks
true
hfile.format.version
3
1.4 測試hbase acl權(quán)限是否生效
使用hbase()用戶登錄hbase客戶端> hbase shell
hbase(main):002:0> scan 'hbase:acl'
ROW COLUMN+CELL
0 row(s) in 0.1560 seconds
2. 配置hbase的映射為數(shù)據(jù)庫
2.1 修改hbase-site.xml,增加下面配置項并重啟bhase,修改后hbase 會映射為數(shù)據(jù)庫的
phoenix.schema.isNamespaceMappingEnabled
true

phoenix.schema.mapSystemTablesToNamespace
true
3. hbase shell常用操作
3.1 list //查看所有hbase表
3.2 //查看所有命名空間
3.3 ‘ns1’ //創(chuàng)建命名空間,相對于數(shù)據(jù)庫的
3.4 ‘ns1:t1’, ‘cf1’ //在命名空間ns1下創(chuàng)建hbase表t1,需要指定一個 ,否則報錯
3.5 put ‘ns1:t1’, ‘r1’, ‘cf1:c1’, ‘’ // 插入一行數(shù)據(jù)到ns1:t1表,為r1,列族cf1,字段為c1:
3.6 get ‘ns1:t1’, ‘r1’ //獲取ns1:t1表,為r1的數(shù)據(jù)
4. hbase shell授權(quán)操作
4.1 hbase acl有RWXCA共5種權(quán)限,分別對應(yīng)讀、寫、執(zhí)行、創(chuàng)建、管理
grant ‘user1’, ‘R’, ‘t1’ //對表t1給user1用戶授予’RWXCA’權(quán)限
4.2 在hbase shell輸入grant列表可以看到詳細(xì)的授權(quán)語法
hbase(main):001:0> grant
ERROR: First argument should be a String
Here is some help for this command:
Grant users specific rights.
Syntax : grant , [, <@namespace> [, [, [, ]]]
permissions is either zero or more letters from the set "RWXCA".

READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A')
Note: Groups and users are granted access in the same way, but groups are prefixed with an '@'
character. In the same way, tables and namespaces are specified, but namespaces are
prefixed with an '@' character.
For example:
hbase> grant 'bobsmith', 'RWXCA'
hbase> grant '@admins', 'RWXCA'
hbase> grant 'bobsmith', 'RWXCA', '@ns1'
hbase> grant 'bobsmith', 'RW', 't1', 'f1', 'col1'
hbase> grant 'bobsmith', 'RW', 'ns1:t1', 'f1', 'col1'
4.3 權(quán)限查看
‘db1:t1’ //查看該表上有權(quán)限的用戶
‘@db1’ //查看該有權(quán)限的用戶
scan ‘hbase:acl’ //掃描所有權(quán)限列表
5. shell操作
5.1 使用操作系統(tǒng)hbase用戶登錄客戶端grant 大數(shù)據(jù)平臺權(quán)限grant 大數(shù)據(jù)平臺權(quán)限,!table命令查看所有有權(quán)限的表
[hbase@vm71 ~]$ /usr/lib/phoenix/bin/sqlline.py
0: jdbc:phoenix:> !table
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+------------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | IMMUTABLE_ROWS | SALT_BUCKE |
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+------------+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | false | null |

| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | false | null |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | false | null |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | false | null |
| | | DDD | TABLE | | | | | | false | null |
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+------------+
0: jdbc:phoenix:>
5.2 如果要使用其它系統(tǒng)賬號執(zhí)行.py,需要根據(jù)報錯提示信息額外授權(quán),或者直接grant ‘user1’, ‘RWXCA’, ‘@’
6. 與hbase表的映射
6.1 項目構(gòu)造在hbase之上,元信息報錯命名空間下的、、MUTEX、、STATS表內(nèi)。所有表一定是hbase表,hbase表不一定是hbase表。
已存在的hbase表需要設(shè)置映射才能讀取到,否則提示找不到表
–建立映射
TABLE “t01” ( “ROW” key, “cf1”.“c1” );
7. 使用指定賬號執(zhí)行 jdbc代碼
// 使用代理用戶userxxx執(zhí)行查詢
String proxyUser = "userxxx";
String querySQL = "select * from t01";
UserGroupInformation ugi = UserGroupInformation.createRemoteUser(proxyUser);
ugi.doAs(new PrivilegedAction() {
@Override
public Void run() {
Connection conn = null;
try {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

Properties properties = new Properties();
properties.setProperty("phoenix.schema.mapSystemTablesToNamespace", "true");
properties.setProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
conn = DriverManager.getConnection(url, properties);
PreparedStatement statement = conn.prepareStatement(querySQL);
ResultSet rs = statement.executeQuery();
// print resultset
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
});
8. 存在的問題一欄 使用 jdbc創(chuàng)建表如果沒指定列族,列族 默認(rèn)是:表不同列族下可有同名的列,但是jdbc sql查詢出來沒有列族名 * from t1 沒權(quán)限的列也會查詢出來庫使用空串“”代替,比如查詢庫下的t1表: * from “”.“t1”庫名、表名必須使用雙引號包裹起來,否則會轉(zhuǎn)換為大寫的庫名、表名導(dǎo)致找不到表,比較蛋疼指定用戶操作hbase,4.8.2版本的..tor有bug,只有開啟了才能起效