开发者学堂课程【HBase 入门教程:HBase 代码_2】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/397/detail/5074
HBase 代码_2
一、创建查询通话详单项目示例
代码示例:
package com.sxt.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.junit.Before;
public class HBaseDemo {
public void begin ( ) throws Exceptiont{
Configuration conf = new Configuration ( );
HBaseAdmin hBaseAdmin;
HBaseAdmin hBaseAdmin;
HTable hTable;
String TN = “phone”;
@Before
public void begin () throws Exception {
configuration conf = new Configuration ( );
conf.set(“hbase.zookeeper.quorum”,”node1,node2,node3”);
//指定 zookeeper 集群
hBaseAdmin = new HBaseAdmin (conf);
//新的参数可以直接携带一个 String 类,变量名称中 familyname 是列族的名称
//初始化 HTable 对象 hTable = new htable(conf,TN)
}
@After
public void end () {
if (hBaseAdmin != null){
try {
hBaseAdmin.close ( );
}catch (IOException e){
e.printstackTrace ( );
}
}
if (hTable ! = null){
//释放
try{
hTable.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
@Test
public void createTbl throws Exception(){
if(hBaseAdmin.tableExists (TN)){
//判断表是否存在
hBaseAdmin.disableTable (TN);
//如果存在
hBaseAdmin.deleteTable(TN);
//如果表存在则进行数据删除
}
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN);
//创建一个新的表名
//返回参数 tablename
//添加关于表的描述
HColumnDescriptor family = new HColumnDescriptor ("cf1");
//familyname 表示列族的名称
//如果需要查询的字段很多,列行非常多,建议建立的 habse 不要
过2到3个列族,否则无法进行支撑。列族有一个限制,最佳范围
1到3个。
Hbase 官网介绍:
Chapter 35. On the number of column families
HBase currently does not do well with anything above two or
three column families so keep thenumber of column families in
your schema low.
(HBase目前不能很好地处理超过2 - 3个列族的情况,所以请将你的模式中的列族数量保持在较低的水平。)Currently,
flushing and compactions are done
on a perRegion basis so if one column family is carrying the
bulk of the data bringing on flushes, the adjacentfamilies will
also be flushed even though the amount of data they carry is
small. When many columnfamilies exist the flushing and
compaction interaction can make for a bunch of needless i/o
(To beaddressed by changing flushing and compaction to work
on a per column family basis). For moreinformation on
compactions, see Compaction.
Try to make do with one column family if you can in your
schemas. Only introduce a second and thirdcolumn family in
the case where data access is usually column scoped;i.e. you
query one columnfamily or the other but usually not both at the
one time.
35.1. Cardinality of ColumnFamilies
Where multiple ColumnFamilies exist in a single table, be aware
of the cardinality (i.e. number ofrows).If ColumnFamilyA has 1
million rows and ColumnFamilyB has 1 billion rows,
ColumnFamilyA'sdata will likely be spread across many, many
regions (and RegionServers). This makes mass scans
forColumnFamilyA less efficient.
//在创建表时必须指定列族,指定列族中含有 addFamily
family.setBlockCacheEnabled (true);
//读取缓存
family.setInMemory (true);
//是否加载
family.setMaxvVersions(1);
//最大版本数默认为1
desc.addFamily (family);
//指定列族
hBaseAdmin.createTable (desc);
//TTL 对应 in time ToLive:当业务中,数据需要定时销毁时,就可以使用此函数。
}
//添加数据的方法
public void insert( )throws Exception{
string rowkey =””;
//定义 rowkey
//查询两种方式:
1.scan
2.get
Put put = new Put(rowkey.getBytes());
hTable.put (put);
}
}
//当在编写 hbase时,将 hbase 文件直接放置进文件夹也许会报错,需要用户手动指定 hbase 配置文件,需要指定zookeeper 集群。
进入后端输入 hbase shell 和 list 查看项目是否创建成功,显示如下:
输入 describe ‘phone’ 进行表明,出现如下:
表创建成功。