【 Hbase 】 【 03 】 자바 조작 habsedemo

7985 단어 hbase
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 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

좋은 웹페이지 즐겨찾기