HBase 数据入库(1)-创建链接

本文介绍了一个基于HBase的API实现类,包括连接配置、表和命名空间的创建等核心功能。通过该API可以方便地进行HBase数据库的操作。

HBase是一个分布式的、面向列的开源数据库,HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库。另一个不同的是HBase基于列的而不是基于行的模式。

HBase 基础 API

这里我编写了一个Hbase的基础API使用的类。类似与sqldao:

创建链接HBase使用的是Zookeeper

public class HBasesql 
{
	private String zookeeperHosts = null;  // zookeeper ip 
	private String zookeeperPort = null;
	private Configuration conf = null;
	private Connection conn = null;
	private Admin hbaseAdmin = null;
	
	public HBasesql(String zookeeperHosts, String zookeeperPort)
	{
		this.zookeeperHosts = zookeeperHosts;
		this.zookeeperPort = zookeeperPort;
	}
 }
public boolean ConnecHbase()
{
       try
       {
           conf = HBaseConfiguration.create();
           conf.set("hbase.zookeeper.quorum",zookeeperHosts);
           conf.set("hbase.zookeeper.property.clientPort", zookeeperPort);
           conf.set("zookeeper.znode.parent","/hbase-unsecure"); // 适用hdp集群
           conf.set("hbase.client.retries.number", "3"); // 链接hbase,失败重连次数
           conf.set("hbase.client.pause", "50"); // 重试间隔 单位:ms
           conf.set("hbase.rpc.timeout", "1800000"); // 提高RPC通信时长
           conf.set("zookeeper.session.timeout", "180000");
           conf.set("hbase.client.scanner.caching", "1000"); // 设置Scan缓存
           // 创建Connection
           conn = ConnectionFactory.createConnection(conf);
           // 创建 Admin
           hbaseAdmin = conn.getAdmin();
           return true;
       }
       catch(Exception e)
       {
           e.printStackTrace();
           return false;
       }
}

关闭Hbase链接

public void close()
{
	try
	{
		if(hbaseAdmin != null)hbaseAdmin.close();
		if(conn != null)conn.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}
创建命名空间

public boolean CreateNamespace(String namespace)
{
	try
	{
                // 创建命名空间
                hbaseAdmin.createNamespace(NamespaceDescriptor.create(namespace).build());
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}
创建表

public boolean CreateTable(String tablename, String columnFamily)
{
	TableName tName = TableName.valueOf(tablename);
	try
	{
		if(!hbaseAdmin.tableExists(tName))
		{
			HTableDescriptor newHTD = new HTableDescriptor(tName);
			HColumnDescriptor newHCD = new HColumnDescriptor(columnFamily);
			newHCD.setMaxVersions(1); // 设置数据保留版本
			newHTD.addFamily(newHCD);
			return true;
		}
		else
		{
			return true;
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值