【 Hbase 】 【 03 】 자바 조작 habsedemo
7985 단어 hbase
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 getAllTableNames(String connectionAddress,String pattern) throws Exception;
public boolean createSnapShot(String connectionAddress,String tableName) throws Exception;
public List 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 getAllTableNames(String connectionAddress, String regex) throws Exception {
List tableNameList = new ArrayList();
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 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 getAllSnapShot(String connectionAddress, String pattern) throws Exception {
List snapShotList = new ArrayList();
Connection connection = null;
try {
connection = getConnection(connectionAddress);
List 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 패키지 도입
org.apache.hbase
hbase-client
1.2.4
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【 Hbase 】 【 03 】 자바 조작 habsedemo1.HbaseSnapShot 2.HbaseService 3.HbaseServiceImpl 4. pom 파일 jar 패키지 도입...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.