Hbase shell操作table
建表
- 直接创建
ns3 是namespace,emp是表,base_info是列簇
hbase(main):037:0> create 'ns3:emp','base_info'
shell界面会有结果提示,如下,表示操作的结果创建了一个叫做emp的表
=> Hbase::Table - ns3:emp
- 使用变量
hbase(main):036:0> t = create 'ns3:employee','base_info'
- 创建多个列族名
hbase(main):040:0> create 'ns3:emp1','f1','f2','f3'
- 创建表时同时指定列族属性
hbase(main):043:0> create 'ns3:emp2',
{NAME=>'f1',VERSIONS'10=>3,TTL=>2592000,BLOCKCACHE=>TRUE}
- 创建表同时指定分片
hbase(main):050:0> create 'ns3:emp=3', 'f1', SPLITS => ['10', '20', '30', '40']
查看表信息
hbase(main):007:0> describe 'ns3:emp1'
Table ns3:emp1 is ENABLED
ns3:emp1
COLUMN FAMILIES DESCRIPTION
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEE
P_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', CO
MPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '6
5536', REPLICATION_SCOPE => '0'}
列出所有表
hbase(main):008:0> list
TABLE
AK_POPULATION
SYSTEM:CATALOG
SYSTEM:FUNCTION
SYSTEM:MUTEX
SYSTEM:SEQUENCE
SYSTEM:STATS
Student
修改表的属性
hbase(main):011:0> alter 'csdn:emp',{NAME=>'f1',BLOOMFILTER=>'rowcol'}
Updating all regions with the new schema...
1/1 regions updated.
hbase(main):012:0> describe 'csdn:emp'
COLUMN FAMILIES DESCRIPTION
{NAME => 'base_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => '
FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'f1', BLOOMFILTER => 'ROWCOL', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FORE
VER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.1070 seconds
增加列族
//逗号不能省略
hbase(main):013:0> alter 'csdn:emp' {NAME=>'f2'}
SyntaxError: (hbase):13: syntax error, unexpected tLCURLY
alter 'csdn:emp' {NAME=>'f2'}
^
hbase(main):014:0> alter 'csdn:emp', {NAME=>'f2'}
Updating all regions with the new schema...
//用逗号隔开
hbase(main):015:0> alter 'csdn:emp','f3'
Updating all regions with the new schema...
//可以同时增加多个列族
hbase(main):016:0> alter 'csdn:emp','f4','f5'
Updating all regions with the new schema...
删除列族
hbase(main):018:0> alter 'csdn:emp','delete'=>'f1'
删除表
先禁用才能删除,直接删除是删除不了的~
hbase(main):003:0> disable 'csdn:emp'
0 row(s) in 2.4470 seconds
hbase(main):004:0> drop 'csdn:emp'
0 row(s) in 1.3730 seconds
Hbase table DDL scala编程
首先创建工具类备用
object HbaseUtil {
private val conf = new Configuration()
conf.set("hbase.zookeeper.quorum", "mypc01:2181,mypc02:2181,mypc03:2181")
private val connection: Connection = ConnectionFactory.createConnection(conf)
var admin: Admin = _
val table: Table = null
def getAdmin: Admin = {
admin = connection.getAdmin
admin
}
def closeAdmin(): Unit = {
admin.close()
}
def getTable(name: String): Table = {
//获取tableName对象
val tableName: TableName = TableName.valueOf(name)
//从连接获取Table对象
val table: Table = connection.getTable(tableName)
table
}
def closeTable(): Unit = {
table.close()
}
}
创建表格
object CreateTable extends App {
private val name: TableName = TableName.valueOf("csdn:students")
//新建一个表描述符
private val tabldedesc = new HTableDescriptor(name)
//新建一个列族描述符
private val cf = new HColumnDescriptor("f1")
//对列族设置布隆过滤器
cf.setBloomFilterType(BloomType.ROWCOL)
cf.setInMemory(true)
//设置列族保存的版本数
cf.setVersions(1,3)
//设置列族单元格的过期时间
cf.setTimeToLive(24*60*60)
//列族绑定表
tabldedesc.addFamily(cf)
//admin创建表格
HbaseUtil.getAdmin.createTable(tabldedesc)
}
修改表的列族信息
上面的列族,表都是new的,这里是需要获取!
object AlterCF extends App {
private val name: TableName = TableName.valueOf("csdn:students")
//利用admin对象获取表描述符
private val tdescriptor: HTableDescriptor = HbaseUtil.getAdmin.getTableDescriptor(name)
//从表对象上获取列族描述符
private val hdescriptor: HColumnDescriptor = tdescriptor.getFamily(Bytes.toBytes("f1"))
//修改列族
hdescriptor.setBloomFilterType(BloomType.ROW)
hdescriptor.setTimeToLive(24*60)
//提交修改
HbaseUtil.getAdmin.modifyColumn(name,hdescriptor)
}
向表中添加新的列族
新建一个列族调用admin对象的addColumn方法就可以了.这里的column实际指的是CF,就是列族
object AddCF extends App {
private val tableName: TableName = TableName.valueOf("csdn:students")
private val tdescriptor: HTableDescriptor = HbaseUtil.getAdmin.getTableDescriptor(tableName)
private val hdescriptor = new HColumnDescriptor("f2")
hdescriptor.setMaxVersions(5)
HbaseUtil.getAdmin.addColumn(tableName,hdescriptor)
}
获取表的描述信息
其实是获取列族的描述信息
object TabldeDesc extends App {
private val tableName: TableName = TableName.valueOf("csdn:students")
private val tdescriptor: HTableDescriptor = HbaseUtil.getAdmin.getTableDescriptor(tableName)
private val families: Array[HColumnDescriptor] = tdescriptor.getColumnFamilies
for(x<-families){
println(s"cf: ${x.getNameAsString} blommfilter: ${x.getBloomFilterType} live: ${x.getTimeToLive}")
}
}
删除表格
要先禁用才能删除表格~
object deleteTable extends App {
private val name: TableName = TableName.valueOf("csdn:students")
if(HbaseUtil.getAdmin.tableExists(name)){
if(!HbaseUtil.getAdmin.isTableDisabled(name)) HbaseUtil.getAdmin.disableTable(name)
}
HbaseUtil.getAdmin.deleteTable(name)
}
总结
- 表格的DDL操作,其实主要是表和列族的相关操作,以及列族的属性是比较丰富的,可以修改!
- 认清三大对象,Admin,HTableDescriptor对象以及HColumnDescriptor对象,认识这三个对象,其他都好办了~