1.HbaseSnapShot
package com.zhenzhen.demo.hbase.service.entity;
public class HbaseSnapShot {
private String name;
private String table;
private String createTime;
private String type;
private String version;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "HbaseSnapShot [name=" + name + ", table=" + table + ", createTime=" + createTime + ", type=" + type
+ ", version=" + version + "]";
}
}
2.HbaseService
package com.zhenzhen.demo.hbase.service;
import java.util.List;
import org.apache.hadoop.hbase.client.Connection;
import com.zhenzhen.demo.hbase.service.entity.HbaseSnapShot;
public interface HbaseService {
public Connection getConnection(String connectionAddress) throws Exception;
public boolean createHbaseTable(String connectionAddress,String tableName,String [] cfs) throws Exception;
public List<String> getAllTableNames(String connectionAddress,String pattern) throws Exception;
public boolean createSnapShot(String connectionAddress,String tableName) throws Exception;
public List<HbaseSnapShot> getAllSnapShot(String connectionAddress,String pattern) throws Exception;
}
3.HbaseServiceImpl
package com.zhenzhen.demo.hbase.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.protobuf.generated.HBaseProtos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zhenzhen.demo.hbase.service.HbaseService;
import com.zhenzhen.demo.hbase.service.entity.HbaseSnapShot;
public class HbaseServiceImpl implements HbaseService {
public static Logger logger = LoggerFactory.getLogger(HbaseServiceImpl.class);
public Connection getConnection(String connectionAddress) throws Exception {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", connectionAddress);
return ConnectionFactory.createConnection(configuration);
}
public boolean createHbaseTable(String connectionAddress, String tableNameStr,String [] cfs) throws Exception {
Connection connection = null;
try {
connection = getConnection(connectionAddress);
TableName tableName = TableName.valueOf(tableNameStr);
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String cf:cfs) {
HColumnDescriptor mycg = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(mycg);
}
connection.getAdmin().createTable(hTableDescriptor);
System.out.println("集群:"+connectionAddress+"创建表"+tableNameStr+"成功");
logger.info("集群:"+connectionAddress+"创建表"+tableNameStr+"成功");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("集群:"+connectionAddress+"创建表"+tableNameStr+"失败"+e.getMessage());
logger.error("集群:"+connectionAddress+"创建表"+tableNameStr+"失败"+e.getMessage());
return false;
}finally {
if(connection!=null) {
connection.close();
}
}
}
public List<String> getAllTableNames(String connectionAddress, String regex) throws Exception {
List<String> tableNameList = new ArrayList<String>();
Connection connection = null;
try {
connection = getConnection(connectionAddress);
TableName[] tableNames = connection.getAdmin().listTableNames(".*"+regex+".*");
for(TableName tableName:tableNames) {
tableNameList.add(tableName.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(connection!=null) {
connection.close();
}
}
return tableNameList;
}
public static void main(String[] args) throws Exception {
String connectionAddress = "master,slave1,slave2";
HbaseServiceImpl HbaseServiceImpl = new HbaseServiceImpl();
//HbaseServiceImpl.createHbaseTable(connectionAddress, "test1", new String[] {"cf"});
//HbaseServiceImpl.getAllTableNames(connectionAddress,".*.*");
// HbaseServiceImpl.createSnapShot(connectionAddress, "test1");
// HbaseServiceImpl.createSnapShot(connectionAddress, "atest1");
List<HbaseSnapShot> snapShotList = HbaseServiceImpl.getAllSnapShot(connectionAddress, "test");
for(HbaseSnapShot hbaseSnapShot:snapShotList) {
System.out.println(hbaseSnapShot);
}
}
public boolean createSnapShot(String connectionAddress, String tableNameStr) throws Exception {
Connection connection = null;
try {
connection = getConnection(connectionAddress);
TableName tableName = TableName.valueOf(tableNameStr);
//判断表是否存在,如果不存在抛出异常
if(!connection.getAdmin().tableExists(tableName)) {
throw new RuntimeException("集群"+connectionAddress+"表"+tableNameStr+"不存在无法创建快照");
}
String snapshotName = "snapshot-"+tableNameStr+"-"+DateUtil.formatDate(new Date(), "yyyyMMdd");
connection.getAdmin().snapshot(snapshotName, tableName);
System.out.println("集群:"+connectionAddress+"表"+tableNameStr+"创建快照成功"+snapshotName);
logger.info("集群:"+connectionAddress+"表"+tableNameStr+"创建快照成功"+snapshotName);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("集群:"+connectionAddress+"表"+tableNameStr+"创建快照失败"+e.getMessage());
logger.error("集群:"+connectionAddress+"表"+tableNameStr+"创建快照失败"+e.getMessage());
throw e;
}finally {
if(connection!=null) {
connection.close();
}
}
}
public List<HbaseSnapShot> getAllSnapShot(String connectionAddress, String pattern) throws Exception {
List<HbaseSnapShot> snapShotList = new ArrayList<HbaseSnapShot>();
Connection connection = null;
try {
connection = getConnection(connectionAddress);
List<HBaseProtos.SnapshotDescription> snapshots = connection.getAdmin().listSnapshots(".*"+pattern+".*");
for(HBaseProtos.SnapshotDescription snapshot:snapshots) {
HbaseSnapShot hbaseSnapShot = new HbaseSnapShot();
hbaseSnapShot.setName(snapshot.getName());
hbaseSnapShot.setTable(snapshot.getTable());
hbaseSnapShot.setType(snapshot.getType().toString());
hbaseSnapShot.setCreateTime(DateUtil.formatDate(new Date(snapshot.getCreationTime()), "yyyy-MM-dd HH:mm:ss"));
snapShotList.add(hbaseSnapShot);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(connection!=null) {
connection.close();
}
}
return snapShotList;
}
}
4.pom文件引入jar包
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.4</version>
</dependency>