启动:start-hbase.sh

停止:stop-hbase.sh

进入shell:hbase shell

  状态:status

  创建表:create 'tableName', 'colFam1'

  查看表包含行数:list 'tableName'

  列出所有:scan 'tableName'

  插入:put 'tableName', 'rowName', 'colFam1:qualify', 'value'

  查询:get 'tableName', 'rowName'

  删除:delete 'tableName', 'rowName'

  禁用表:disable 'tableName'

  删除表:drop 'tableName'

  退出shell:exit


添加列族: java实现

disable 'tableName'

alter 'tableName', NAME=>'fam2'

enable 'tableName'

删除列族:alter 'tableName', {NAME=>'fam2', METHOD=>'delete'}
获取指定时间的值:get 'my','id1',{COLUMN=>'fam1:q1',TIMESTAMP=>1408261511642}
计数器:incr 'my','id2','fam1:age'
查询计数:get_counter 'my','id1','fam1:age',


HBaseHelper

package util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; /**
* Used by the book examples to generate tables and fill them with test data.
*/
public class HBaseHelper { private Configuration conf = null;
private HBaseAdmin admin = null; protected HBaseHelper(Configuration conf) throws IOException {
this.conf = conf;
this.admin = new HBaseAdmin(conf);
} public static HBaseHelper getHelper(Configuration conf) throws IOException {
return new HBaseHelper(conf);
} public boolean existsTable(String table)
throws IOException {
return admin.tableExists(table);
} public void createTable(String table, String... colfams)
throws IOException {
createTable(table, null, colfams);
} public void createTable(String table, byte[][] splitKeys, String... colfams)
throws IOException {
HTableDescriptor desc = new HTableDescriptor(table);
for (String cf : colfams) {
HColumnDescriptor coldef = new HColumnDescriptor(cf);
desc.addFamily(coldef);
}
if (splitKeys != null) {
admin.createTable(desc, splitKeys);
} else {
admin.createTable(desc);
}
} public void disableTable(String table) throws IOException {
admin.disableTable(table);
} public void dropTable(String table) throws IOException {
if (existsTable(table)) {
disableTable(table);
admin.deleteTable(table);
}
} public void fillTable(String table, int startRow, int endRow, int numCols,
String... colfams)
throws IOException {
fillTable(table, startRow, endRow, numCols, -1, false, colfams);
} public void fillTable(String table, int startRow, int endRow, int numCols,
boolean setTimestamp, String... colfams)
throws IOException {
fillTable(table, startRow, endRow, numCols, -1, setTimestamp, colfams);
} public void fillTable(String table, int startRow, int endRow, int numCols,
int pad, boolean setTimestamp, String... colfams)
throws IOException {
fillTable(table, startRow, endRow, numCols, pad, setTimestamp, false, colfams);
} public void fillTable(String table, int startRow, int endRow, int numCols,
int pad, boolean setTimestamp, boolean random,
String... colfams)
throws IOException {
HTable tbl = new HTable(conf, table);
Random rnd = new Random();
for (int row = startRow; row <= endRow; row++) {
for (int col = 0; col < numCols; col++) {
Put put = new Put(Bytes.toBytes("row-" + padNum(row, pad)));
for (String cf : colfams) {
String colName = "col-" + padNum(col, pad);
String val = "val-" + (random ?
Integer.toString(rnd.nextInt(numCols)) :
padNum(row, pad) + "." + padNum(col, pad));
if (setTimestamp) {
put.add(Bytes.toBytes(cf), Bytes.toBytes(colName),
col, Bytes.toBytes(val));
} else {
put.add(Bytes.toBytes(cf), Bytes.toBytes(colName),
Bytes.toBytes(val));
}
}
tbl.put(put);
}
}
tbl.close();
} public String padNum(int num, int pad) {
String res = Integer.toString(num);
if (pad > 0) {
while (res.length() < pad) {
res = "0" + res;
}
}
return res;
} public void put(String table, String row, String fam, String qual,
String val) throws IOException {
HTable tbl = new HTable(conf, table);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), Bytes.toBytes(val));
tbl.put(put);
tbl.close();
} public void put(String table, String row, String fam, String qual, long ts,
String val) throws IOException {
HTable tbl = new HTable(conf, table);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), ts,
Bytes.toBytes(val));
tbl.put(put);
tbl.close();
} public void put(String table, String[] rows, String[] fams, String[] quals,
long[] ts, String[] vals) throws IOException {
HTable tbl = new HTable(conf, table);
for (String row : rows) {
Put put = new Put(Bytes.toBytes(row));
for (String fam : fams) {
int v = 0;
for (String qual : quals) {
String val = vals[v < vals.length ? v : vals.length - 1];
long t = ts[v < ts.length ? v : ts.length - 1];
put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), t,
Bytes.toBytes(val));
v++;
}
}
tbl.put(put);
}
tbl.close();
} public void dump(String table, String[] rows, String[] fams, String[] quals)
throws IOException {
HTable tbl = new HTable(conf, table);
List<Get> gets = new ArrayList<Get>();
for (String row : rows) {
Get get = new Get(Bytes.toBytes(row));
get.setMaxVersions();
if (fams != null) {
for (String fam : fams) {
for (String qual : quals) {
get.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual));
}
}
}
gets.add(get);
}
Result[] results = tbl.get(gets);
for (Result result : results) {
for (KeyValue kv : result.raw()) {
System.out.println("KV: " + kv +
", Value: " + Bytes.toString(kv.getValue()));
}
}
}
}

Main

package test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes; import util.HBaseHelper; public class Main { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseHelper helper = HBaseHelper.getHelper(conf);
String tableName = "testtable"; if (helper.existsTable(tableName)) {
helper.dropTable(tableName);
}
helper.createTable(tableName, "colfam1"); HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes("row1"));
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val1"));
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
Bytes.toBytes("val2"));
table.put(put); } }

最新文章

  1. java常用工具
  2. !important使用
  3. [转载] 散列表(Hash Table)从理论到实用(中)
  4. 【应用笔记】【AN003】VC++环境下基于以太网的4-20mA电流采集
  5. Codeforces Round #366 (Div. 2) C 模拟queue
  6. div图片垂直居中 如何使div中图片垂直居中
  7. C++学习笔记(八):函数重载、函数指针和函数对象
  8. Unity重要的函数
  9. android adb shell 命令大全
  10. c#导入excel 绑定数据 repeat为例子
  11. innobackupex 备份 Xtrabackup 增量备份
  12. 【PAT】B1043 输出PATest(20 分)
  13. java证书
  14. Angular中form表单中input自动响应回车事件无效
  15. Excel中使用VBA访问Access数据库
  16. 浅谈DB2的四个隔离级别
  17. 面包旅行Android业务设计分析
  18. LVS原理详解以及部署
  19. 并排的两个div之间会有空隙
  20. asterisk ss7 ${CALLERID(rdnis)}变量为空问题

热门文章

  1. [NOI2003][bzoj1507] 文本编辑器 editor [splay]
  2. BZOJ2393 &amp; 1853 [Scoi2010]幸运数字 【搜索 + 容斥】
  3. input type file上传文件之后清空内容。
  4. 数学作业(codevs 2314)
  5. numeric 转换为数据类型 (null) 时出现算术溢出错误
  6. Cover
  7. CODEVS【3556】科技庄园
  8. C#生成高清缩略图的方法
  9. HBase shell 中的十六进制数值表示
  10. idea 快速生成代码的快捷键