如何实现hbase安全批量加载

如何实现hbase安全批量加载,hbase,Hbase,我已经在kerberos集群中的hbase中创建了一个大容量加载,其驱动程序类与此类似(正常工作): 现在我想实现安全批量加载,但似乎必须使用协处理器框架(hbase 1.0.0)来实现。有人能给我一个完整的示例,说明如何使用securebulkloadHFiles方法吗? 谢谢你的帮助我会回答我的问题: 要使此答案起作用,hbase中必须已经存在一个表,而且必须已经为导入生成HFile import java.util.ArrayList; import java.util.List; imp

我已经在kerberos集群中的hbase中创建了一个大容量加载,其驱动程序类与此类似(正常工作):

现在我想实现安全批量加载,但似乎必须使用协处理器框架(hbase 1.0.0)来实现。有人能给我一个完整的示例,说明如何使用securebulkloadHFiles方法吗?
谢谢你的帮助

我会回答我的问题:

要使此答案起作用,hbase中必须已经存在一个表,而且必须已经为导入生成HFile

import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.coprocessor.SecureBulkLoadClient;
import org.apache.hadoop.hbase.security.UserProvider;
import org.apache.hadoop.hbase.security.token.FsDelegationToken;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.security.UserGroupInformation;

String keyTab = "pathtokeytabfile";
String tableName = "tb_name";
String pathToHFile = "/tmp/tmpfiles/";
Configuration configuration = new Configuration();  

configuration.set("hbase.zookeeper.quorum","ZK_QUORUM");
configuration.set("hbase.zookeeper"+ ".property.clientPort","2181");
configuration.set("hbase.master","MASTER:60000");
configuration.set("hadoop.security.authentication", "Kerberos");
configuration.set("hbase.security.authentication", "kerberos");


//Obtaining kerberos authentication 

UserGroupInformation.setConfiguration(configuration);

UserGroupInformation.loginUserFromKeytab("here keytab", path to the key tab);

HBaseAdmin.checkHBaseAvailable(configuration);

System.out.println("HBase is running!");

HBaseConfiguration.addHbaseResources(configuration);    

Connection conn = ConnectionFactory.createConnection(configuration);

Table table = conn.getTable(TableName.valueOf(tableName));

HRegionInfo tbInfo = new HRegionInfo(table.getName());


//path to the HFiles that need to be loaded 

Path hfofDir = new Path(pathToHFile);

//acquiring user token for authentication 

UserProvider up = UserProvider.instantiate(configuration);

FsDelegationToken fsDelegationToken = new FsDelegationToken(up, "name of the key tab user");

fsDelegationToken.acquireDelegationToken(hfofDir.getFileSystem(configuration));

//preparing  for the bulk load

SecureBulkLoadClient secureBulkLoadClient = new SecureBulkLoadClient(table);

String bulkToken = secureBulkLoadClient.prepareBulkLoad(table.getName());

System.out.println(bulkToken);

//creating the family list (list of family names and path to the hfile corresponding to the family name)

final List<Pair<byte[], String>> famPaths = new ArrayList<>();

Pair p = new Pair();

//name of the family 
p.setFirst("nameofthefamily".getBytes());

//path to the HFile (HFile are organized in folder with the name of the family)
p.setSecond("/tmp/tmpfiles/INTRO/nameofthefilehere");

famPaths.add(p);

//bulk loading ,using the secure bulk load client

secureBulkLoadClient.bulkLoadHFiles(famPaths, fsDelegationToken.getUserToken(), bulkToken, tbInfo.getStartKey());

System.out.println("Bulk Load Completed..");    
import java.util.ArrayList;
导入java.util.List;
导入org.apache.hadoop.conf.Configuration;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.hbase.HBaseConfiguration;
导入org.apache.hadoop.hbase.hregoninfo;
导入org.apache.hadoop.hbase.TableName;
导入org.apache.hadoop.hbase.client.Connection;
导入org.apache.hadoop.hbase.client.ConnectionFactory;
导入org.apache.hadoop.hbase.client.HBaseAdmin;
导入org.apache.hadoop.hbase.client.Table;
导入org.apache.hadoop.hbase.client.coprocessor.SecureBulkLoadClient;
导入org.apache.hadoop.hbase.security.UserProvider;
导入org.apache.hadoop.hbase.security.token.FsDelegationToken;
导入org.apache.hadoop.hbase.util.Pair;
导入org.apache.hadoop.security.UserGroupInformation;
String keyTab=“pathtokeytabfile”;
String tableName=“tb_name”;
字符串pathToHFile=“/tmp/tmpfiles/”;
配置=新配置();
set(“hbase.zookeeper.quorum”、“ZK_quorum”);
set(“hbase.zookeeper”+”.property.clientPort,“2181”);
设置(“hbase.master”,“master:60000”);
set(“hadoop.security.authentication”、“Kerberos”);
set(“hbase.security.authentication”、“kerberos”);
//获取kerberos身份验证
UserGroupInformation.setConfiguration(配置);
loginUserFromKeytab(“此处为keytab”,指向键选项卡的路径);
hbasadmin.checkHBaseAvailable(配置);
System.out.println(“HBase正在运行!”);
HBaseConfiguration.addHbaseResources(配置);
Connection conn=ConnectionFactory.createConnection(配置);
Table Table=conn.getTable(TableName.valueOf(TableName));
hregoninfo tbInfo=新的hregoninfo(table.getName());
//需要加载的HFiles的路径
路径hfofDir=新路径(路径文件);
//获取用于身份验证的用户令牌
UserProvider up=UserProvider.instantiate(配置);
FsDelegationToken FsDelegationToken=新的FsDelegationToken(up,“键选项卡用户的名称”);
acquireDelegationToken(hfofDir.getFileSystem(配置));
//准备散装货物
SecureBulkLoadClient SecureBulkLoadClient=新SecureBulkLoadClient(表);
String bulkToken=secureBulkLoadClient.prepareBulkLoad(table.getName());
System.out.println(bulkToken);
//创建族列表(族名称列表和与族名称对应的hfile的路径)
最终列表famPaths=newarraylist();
对p=新对();
//姓
p、 setFirst(“name of family.getBytes());
//HFile的路径(HFile组织在具有族名称的文件夹中)
p、 设置秒(“/tmp/tmpfiles/INTRO/nameoffilehere”);
添加(p);
//批量加载,使用安全批量加载客户端
secureBulkLoadClient.bulkLoadHFiles(famPaths、fsDelegationToken.getUserToken()、bulkToken、tbInfo.getStartKey());
System.out.println(“批量装载完成”);

hbase中已经实现了用于批量加载的cooprocessor,只需要客户端调用
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.coprocessor.SecureBulkLoadClient;
import org.apache.hadoop.hbase.security.UserProvider;
import org.apache.hadoop.hbase.security.token.FsDelegationToken;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.security.UserGroupInformation;

String keyTab = "pathtokeytabfile";
String tableName = "tb_name";
String pathToHFile = "/tmp/tmpfiles/";
Configuration configuration = new Configuration();  

configuration.set("hbase.zookeeper.quorum","ZK_QUORUM");
configuration.set("hbase.zookeeper"+ ".property.clientPort","2181");
configuration.set("hbase.master","MASTER:60000");
configuration.set("hadoop.security.authentication", "Kerberos");
configuration.set("hbase.security.authentication", "kerberos");


//Obtaining kerberos authentication 

UserGroupInformation.setConfiguration(configuration);

UserGroupInformation.loginUserFromKeytab("here keytab", path to the key tab);

HBaseAdmin.checkHBaseAvailable(configuration);

System.out.println("HBase is running!");

HBaseConfiguration.addHbaseResources(configuration);    

Connection conn = ConnectionFactory.createConnection(configuration);

Table table = conn.getTable(TableName.valueOf(tableName));

HRegionInfo tbInfo = new HRegionInfo(table.getName());


//path to the HFiles that need to be loaded 

Path hfofDir = new Path(pathToHFile);

//acquiring user token for authentication 

UserProvider up = UserProvider.instantiate(configuration);

FsDelegationToken fsDelegationToken = new FsDelegationToken(up, "name of the key tab user");

fsDelegationToken.acquireDelegationToken(hfofDir.getFileSystem(configuration));

//preparing  for the bulk load

SecureBulkLoadClient secureBulkLoadClient = new SecureBulkLoadClient(table);

String bulkToken = secureBulkLoadClient.prepareBulkLoad(table.getName());

System.out.println(bulkToken);

//creating the family list (list of family names and path to the hfile corresponding to the family name)

final List<Pair<byte[], String>> famPaths = new ArrayList<>();

Pair p = new Pair();

//name of the family 
p.setFirst("nameofthefamily".getBytes());

//path to the HFile (HFile are organized in folder with the name of the family)
p.setSecond("/tmp/tmpfiles/INTRO/nameofthefilehere");

famPaths.add(p);

//bulk loading ,using the secure bulk load client

secureBulkLoadClient.bulkLoadHFiles(famPaths, fsDelegationToken.getUserToken(), bulkToken, tbInfo.getStartKey());

System.out.println("Bulk Load Completed..");