如果需使用API来进行权限的管理,可考虑如下实例:
使用Java API访问启用Kerberos的服务示例
环境准备:
配置Kerberos客户端
配置默认路径:/etc/krb5.conf (Linux) 或者 C://Windows/krb5.ini (Windows)
或者
添加Java程序启动参数 -Djava.security.krb5.conf=<your_krb5_conf_path> 来指定Kerberos客户端配置
文件路径
确保客户端时间与集群时间同步
NOTE: client的机器时间和集群时间的时间差需在5分钟之内。
1. 程序的 Classpath 中加入配置文件目录(hbase-site.xml, core-site.xml)时,使用UserGroupInformation.loginUserFromKeytab 登录:
Example 1. 访问Hyperbase示例
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.security.UserGroupInformation;
public class HBaseSecureTest {
public static void main(String[] args) throws IOException {
UserGroupInformation.loginUserFromKeytab(“hbase/tw-node2125”,
“/etc/hyperbase1/hbase.keytab”);①
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
hBaseAdmin.createTable(new HTableDescriptor(“t1”));
}
}
① "/etc/hyperbase1/hbase.keytab"表示客户端的加密文件所在地址,而非服务端的,所以需要根据具体
的客户端地址加以修改;“hbase/tw-node2125”中前者表示用户名,后者表示集群的任意一台机器,
可以不写。
2.当 Classpath 中没有加入配置文件目录(hbase-site.xml, core-site.xml)时,需要按如下方式在代码里设置一些配置项。
Example 2. 访问Hyperbase示例
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.security.UserGroupInformation;
public class HBaseSecureTest {
public static void main(String[] args) throws IOException {
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set(“hbase.zookeeper.quorum”,
“172.16.2.125,172.16.2.126,172.16.2.127”); ①
HBASE_CONFIG.set(“hbase.master.kerberos.principal”,
“hbase/_HOST@TDH”);
HBASE_CONFIG.set(“hbase.regionserver.kerberos.principal”,
“hbase/_HOST@TDH”);
HBASE_CONFIG.set(“hbase.security.authentication”, “kerberos”);
HBASE_CONFIG.set(“hadoop.security.authentication”, “kerberos”);
HBASE_CONFIG.set(“zookeeper.znode.parent”, “/hyperbase1”);
Configuration conf = HBaseConfiguration.create(HBASE_CONFIG);
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(“hbase/tw-node2125”,
“/etc/hyperbase1/hbase.keytab”);
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
hBaseAdmin.createTable(new HTableDescriptor(“t1”));
}
}
① 以下四条命令中的参数都是固定写法。
3.使用不同的 UserGroupInformation 对象执行不同的代码段
UserGroupInformation ugi1 =
UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal1,
keytab1);
UserGroupInformation ugi2 =
UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal2,
keytab2);
ugi1.doAs(new PrivilegeExceptionAction {
override T run() {
action1;
return T;
}
);
ugi2.doAs(new PrivilegeExceptionAction {
override T run() {
action2;
return T;
}
);
4.Hyperbase 操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
}
/**
public static void delRecord (String tableName, String rowKey) throws
IOException{
HTable table = new HTable(conf, tableName);
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println(“del recored " + rowKey + " ok.”);
}
/**
具体参考文档下载:链接:http://pan.baidu.com/s/1i5K* 56 密码:mbdy
如果需使用API来进行权限的管理,可考虑如下实例:
使用Java API访问启用Kerberos的服务示例
环境准备:
配置Kerberos客户端
配置默认路径:/etc/krb5.conf (Linux) 或者 C://Windows/krb5.ini (Windows)
或者
添加Java程序启动参数 -Djava.security.krb5.conf=<your_krb5_conf_path> 来指定Kerberos客户端配置
文件路径
确保客户端时间与集群时间同步
NOTE: client的机器时间和集群时间的时间差需在5分钟之内。
1. 程序的 Classpath 中加入配置文件目录(hbase-site.xml, core-site.xml)时,使用UserGroupInformation.loginUserFromKeytab 登录:
Example 1. 访问Hyperbase示例
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.security.UserGroupInformation;
public class HBaseSecureTest {
public static void main(String[] args) throws IOException {
UserGroupInformation.loginUserFromKeytab(“hbase/tw-node2125”,
“/etc/hyperbase1/hbase.keytab”);①
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
hBaseAdmin.createTable(new HTableDescriptor(“t1”));
}
}
① "/etc/hyperbase1/hbase.keytab"表示客户端的加密文件所在地址,而非服务端的,所以需要根据具体
的客户端地址加以修改;“hbase/tw-node2125”中前者表示用户名,后者表示集群的任意一台机器,
可以不写。
2.当 Classpath 中没有加入配置文件目录(hbase-site.xml, core-site.xml)时,需要按如下方式在代码里设置一些配置项。
Example 2. 访问Hyperbase示例
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.security.UserGroupInformation;
public class HBaseSecureTest {
public static void main(String[] args) throws IOException {
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set(“hbase.zookeeper.quorum”,
“172.16.2.125,172.16.2.126,172.16.2.127”); ①
HBASE_CONFIG.set(“hbase.master.kerberos.principal”,
“hbase/_HOST@TDH”);
HBASE_CONFIG.set(“hbase.regionserver.kerberos.principal”,
“hbase/_HOST@TDH”);
HBASE_CONFIG.set(“hbase.security.authentication”, “kerberos”);
HBASE_CONFIG.set(“hadoop.security.authentication”, “kerberos”);
HBASE_CONFIG.set(“zookeeper.znode.parent”, “/hyperbase1”);
Configuration conf = HBaseConfiguration.create(HBASE_CONFIG);
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(“hbase/tw-node2125”,
“/etc/hyperbase1/hbase.keytab”);
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
hBaseAdmin.createTable(new HTableDescriptor(“t1”));
}
}
① 以下四条命令中的参数都是固定写法。
3.使用不同的 UserGroupInformation 对象执行不同的代码段
UserGroupInformation ugi1 =
UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal1,
keytab1);
UserGroupInformation ugi2 =
UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal2,
keytab2);
ugi1.doAs(new PrivilegeExceptionAction {
override T run() {
action1;
return T;
}
);
ugi2.doAs(new PrivilegeExceptionAction {
override T run() {
action2;
return T;
}
);
4.Hyperbase 操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
}
/**
public static void delRecord (String tableName, String rowKey) throws
IOException{
HTable table = new HTable(conf, tableName);
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println(“del recored " + rowKey + " ok.”);
}
/**
具体参考文档下载:链接:http://pan.baidu.com/s/1i5K* 56 密码:mbdy