进入HBase shell的命令:./hbase shell
相关shell操作如下所示:
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
admin.createNamespace(NamespaceDescriptor.create("ns").build();
//create tableDesc, with namespace name "ns" and table name "table02"
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("ns:table02"));
tableDesc.setDurability(Durability.SYNC_WAL);
//设置一个Region中的Store文件的最大size
tableDesc.setMaxFileSize(512);
//设置Region内存中的memstore的最大值
tableDesc.setMemStoreFlushSize(512);
//设置写WAL日志的级别
tableDesc.setDurability(Durability.SYNC_WAL);
//add a column family "mycf"
HColumnDescriptor hcd = new HColumnDescriptor("cf");
//设置数据保存的最大时间
hcd.setTimeToLive(5184000);
//设置数据保存在内存中以提高响应速度
hcd.setInMemory(true);
//设置数据保存的最大版本数
hcd.setMaxVersions(10);
//设置数据保存的最小版本数(配合TimeToLive使用)
hcd.setMinVersions(5);
tableDesc.addFamily(hcd);
admin.createTable(tableDesc);
admin.close();
注意以下几点:
删除表之前需要先disable表。
相关shell操作如下所示:
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
String tablename = "ns:table02";
if(admin.tableExists(tablename)) {
try {
admin.disableTable(tablename);
admin.deleteTable(tablename);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
admin.close();
相关shell操作
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
String tablename = "ns:table02";
if(admin.tableExists(tablename)) {
try {
admin.disableTable(tablename);
//get the TableDescriptor of target table
HTableDescriptor newtd = admin.getTableDescriptor(Bytes.toBytes("ns:table02"));
//remove 3 useless column families
newtd.removeFamily(Bytes.toBytes("note"));
newtd.removeFamily(Bytes.toBytes("newcf"));
newtd.removeFamily(Bytes.toBytes("susinfo"));
//create HColumnDescriptor for new column family
HColumnDescriptor newhcd = new HColumnDescriptor("action_log");
newhcd.setMaxVersions(10);
newhcd.setKeepDeletedCells(true);
//add the new column family(HColumnDescriptor) to HTableDescriptor
newtd.addFamily(newhcd);
//modify target table struture
admin.modifyTable(Bytes.toBytes("ns:table02"),newtd);
admin.enableTable(tablename);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
admin.close();
流程如下:
相关shell操作如下:
插入行
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "ns:table02");
Put put = new Put(Bytes.toBytes("100001"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"),Bytes.toBytes("lion"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("shangdi"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("30"));
put.setDurability(Durability.SYNC_WAL);
table.put(put);
table.close();
更新行
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Put put = new Put(Bytes.toBytes("100001"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lee"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("longze"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("31"));
put.setDurability(Durability.SYNC_WAL);
table.put(put);
table.close();
从目标字符串中提取子串,作为行键,构建Put
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Put put = new Put(Bytes.toBytes("100001_100002"),7,6);
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("show"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("caofang"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("30"));
table.put(put);
table.close();
注意以下几点:
Delete类用于删除表中的一行数据,通过HTable.delete来执行该动作。
在执行Delete操作时,HBase并不会立即删除数据,而是对需要删除的数据打上一个“墓碑”标记,直到当Storefile合并时,再清除这些被标记上“墓碑”的数据。
如果希望删除整行,用行键来初始化一个Delete对象即可。如果希望进一步定义删除的具体内容,可以使用以下这些Delete对象的方法:
相关shell操作如下:
删除整行的所有列族、所有行、所有版本
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Delete delete = new Delete(Bytes.toBytes("000"));
table.delete(delete);
table.close();
删除指定列的最新版本
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Delete delete = new Delete(Bytes.toBytes("100003"));
delete.deleteColumn(Bytes.toBytes("info"),Bytes.toBytes("address"));
table.delete(delete);
table.close();
删除指定列的所有版本
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Delete delete = new Delete(Bytes.toBytes("100003"));
delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("address"));
table.delete(delete);
table.close();
删除指定列族中所有列的时间戳等于指定时间戳的版本数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Delete delete = new Delete(Bytes.toBytes("100003"));
delete.deleteFamilyVersion(Bytes.toBytes("info"),1405390959464L);
table.delete(delete);
table.close();
如果希望获取整行数据,用行键初始化一个Get对象就可以,如果希望进一步缩小获取的数据范围,可以使用Get对象的以下方法:
Get(byte[] row)
参数是行键
获取行键指定行的所有列族、所有列的最新版本数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Get get = new Get(Bytes.toBytes("100003"));
Result r = table.get(get);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
获取行键指定行中,指定列的最新版本数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Get get = new Get(Bytes.toBytes("100003"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
Result r = table.get(get);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
获取行键指定的行中,指定时间戳的数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:leetable");
Get get = new Get(Bytes.toBytes("100003"));
get.setTimeStamp(1405407854374L);
Result r = table.get(get);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
获取行键指定的行中,所有版本的数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:itable");
Get get = new Get(Bytes.toBytes("100003"));
get.setMaxVersions();
Result r = table.get(get);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell))+ " Time : " + cell.getTimestamp());
}
table.close();
Scan对象可以返回满足给定条件的多行数据。如果希望获取所有的行,直接初始化一个Scan对象即可。如果希望限制扫描的行范围,可以使用以下方法:
扫描表中的所有行的最新版本数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:itable");
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell))+ " Time : " + cell.getTimestamp());
}
table.close();
扫描指定行键范围,通过末尾加0,使得结果集包含StopRow
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:itable");
Scan s = new Scan();
s.setStartRow(Bytes.toBytes("100001"));
s.setStopRow(Bytes.toBytes("1000020"));
ResultScanner rs = table.getScanner(s);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell))+ " Time : " + cell.getTimestamp());
}
table.close();
返回所有已经被打上删除标记但尚未被真正删除的数据
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:itable");
Scan s = new Scan();
s.setStartRow(Bytes.toBytes("100003"));
s.setRaw(true);
s.setMaxVersions();
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell))+ " Time : " + cell.getTimestamp());
}
table.close();
结合过滤器,获取所有age在25到30之间的行
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "rd_ns:itable");
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
Bytes.toBytes("info"), Bytes.toBytes("age"), CompareOp.GREATER_OR_EQUAL, Bytes.toBytes("25") );
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("info"), Bytes.toBytes("age"), CompareOp.LESS_OR_EQUAL, Bytes.toBytes("30") );
filterList.addFilter(filter1);
filterList.addFilter(filter2);
Scan scan = new Scan();
scan.setFilter(filterList);
ResultScanner rs = table.getScanner(scan);
for (Cell cell : r.rawCells()) {
System.out.println(
"Rowkey : " +Bytes.toString(r.getRow()) + "Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
" Value : " + Bytes.toString(CellUtil.cloneValue(cell))+ " Time : " + cell.getTimestamp());
}
table.close();
注意: