HBase
Region类似于表,当表的数据达到阈值时会分表(裂变,可能会裂变到其他的RegionServer中去)
Memstore与storefile
一个region由多个store组成,一个store对应一个CF(列族,多个列)
store包括位于内存中的memstore和位于磁盘的storefile写操作先写入memstore,当memstore中的数据达到某个阈值,hregionserver会启动flashcache进程写入storefile,每次写入形成单独的一个storefile。
当storefile文件的数量增长到一定阈值后,系统会进行合并(minor、major compaction),在合并过程中会进行版本合并和删除工作(majar),形成更大的storefile
当一个region所有storefile的大小和数量超过一定阈值后,会把当前的region分割为两个,并由hmaster分配到相应的regionserver服务器,实现负载均衡客户端检索数据,先在memstore找,找不到再找storefile
Region存储的是我们的表,Store是列族,最后落地到menStor,StoreFile
JAVA API操作HBase
创建表
public class Hbase {
HBaseAdmin admin;
String TN = "phone";
@Before
public void init() throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop103");
admin = new HBaseAdmin(conf);
}
@Test
public void creatTable()throws Exception{
if(admin.tableExists(TN)){
admin.disableTable(TN);
admin.deleteTable(TN);
}
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
HColumnDescriptor cf = new HColumnDescriptor("cf".getBytes());
desc.addFamily(cf);
admin.createTable(desc);
}
@After
public void destory()throws Exception{
if (admin!=null){
admin.close();
}
}
}
增加数据
public class Hbase {
HBaseAdmin admin;
HTable hTable;
String TN = "phone";
@Before
public void init() throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop103");
admin = new HBaseAdmin(conf);
hTable = new HTable(conf,TN.getBytes());
}
@Test
public void creatTable()throws Exception{
if(admin.tableExists(TN)){
admin.disableTable(TN);
admin.deleteTable(TN);
}
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
HColumnDescriptor cf = new HColumnDescriptor("cf".getBytes());
desc.addFamily(cf);
admin.createTable(desc);
}
@Test
public void insertDB()throws Exception{
String rowKey = "1";
Put put = new Put(rowKey.getBytes());
put.add("cf".getBytes(),"name".getBytes(),"xiaoming".getBytes());
put.add("cf".getBytes(),"age".getBytes(),"12".getBytes());
put.add("cf".getBytes(),"sex".getBytes(),"man".getBytes());
hTable.put(put);
}
@After
public void destory()throws Exception{
if (admin!=null){
admin.close();
}
}
}