|
@ -1,11 +1,10 @@
|
|
|
package com.yihu.base.hbase.config;
|
|
|
|
|
|
import com.yihu.base.hbase.HBaseHelper;
|
|
|
import org.apache.hadoop.hbase.HBaseConfiguration;
|
|
|
import org.apache.hadoop.hbase.TableName;
|
|
|
import org.apache.hadoop.hbase.client.*;
|
|
|
import org.apache.hadoop.hbase.security.User;
|
|
|
import org.apache.hadoop.hbase.util.Bytes;
|
|
|
import org.apache.hadoop.security.UserGroupInformation;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@ -15,7 +14,10 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.hadoop.hbase.HbaseTemplate;
|
|
|
import org.springframework.data.hadoop.hbase.TableCallback;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* @author Sand
|
|
@ -24,11 +26,11 @@ import java.util.*;
|
|
|
*/
|
|
|
@Configuration
|
|
|
@ConfigurationProperties(prefix = "hadoop")
|
|
|
public class HbaseConfig{
|
|
|
public class HbaseConfig {
|
|
|
private static Logger logger = LoggerFactory.getLogger(HbaseConfig.class);
|
|
|
private Map<String, String> hbaseProperties = new HashMap<>();
|
|
|
|
|
|
public Map<String, String> getHbaseProperties(){
|
|
|
public Map<String, String> getHbaseProperties() {
|
|
|
return this.hbaseProperties;
|
|
|
}
|
|
|
|
|
@ -38,7 +40,7 @@ public class HbaseConfig{
|
|
|
@Bean
|
|
|
public org.apache.hadoop.conf.Configuration configuration() {
|
|
|
Set<String> keys = new HashSet<>(hbaseProperties.keySet());
|
|
|
for (String key : keys){
|
|
|
for (String key : keys) {
|
|
|
String value = hbaseProperties.remove(key);
|
|
|
key = key.replaceAll("^\\d{1,2}\\.", "");
|
|
|
|
|
@ -54,25 +56,23 @@ public class HbaseConfig{
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public HbaseTemplate hbaseTemplate(org.apache.hadoop.conf.Configuration configuration){
|
|
|
public HbaseTemplate hbaseTemplate(org.apache.hadoop.conf.Configuration configuration) {
|
|
|
HbaseTemplate hbaseTemplate = new HbaseTemplate();
|
|
|
hbaseTemplate.setConfiguration(configuration);
|
|
|
|
|
|
try
|
|
|
{
|
|
|
logger.info("set System property for hbase ---",user);
|
|
|
try {
|
|
|
logger.info("set System property for hbase ---", user);
|
|
|
System.setProperty("HADOOP_USER_NAME", user);
|
|
|
String tableName = "HealthProfile";
|
|
|
//覆盖默认的配置文件
|
|
|
org.apache.hadoop.conf.Configuration.addDefaultResource("core-site.xml");
|
|
|
org.apache.hadoop.conf.Configuration.addDefaultResource("hbase-site.xml");
|
|
|
Connection connection = ConnectionFactory.createConnection(configuration);
|
|
|
logger.info("Hbase createConnection finished---",connection.getConfiguration());
|
|
|
logger.info("Hbase createConnection finished---", connection.getConfiguration());
|
|
|
Admin admin = connection.getAdmin();
|
|
|
boolean ex = admin.tableExists(TableName.valueOf(tableName));
|
|
|
//判断是否存在
|
|
|
if(ex)
|
|
|
{
|
|
|
if (ex) {
|
|
|
hbaseTemplate.execute(tableName, new TableCallback<Object>() {
|
|
|
@Override
|
|
|
public Object doInTable(HTableInterface table) throws Throwable {
|
|
@ -85,12 +85,16 @@ public class HbaseConfig{
|
|
|
}
|
|
|
admin.close();
|
|
|
connection.close();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
logger.info("Hbase createConnection failure",ex.getMessage());
|
|
|
} catch (Exception ex) {
|
|
|
logger.info("Hbase createConnection failure", ex.getMessage());
|
|
|
}
|
|
|
return hbaseTemplate;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
public HBaseHelper hBaseHelper(HbaseTemplate hbaseTemplate) {
|
|
|
HBaseHelper hBaseHelper = new HBaseHelper(hbaseTemplate);
|
|
|
return hBaseHelper;
|
|
|
}
|
|
|
}
|