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;
}
}