| 名称 |
命令表达式 |
| 创建表 |
create '表名称', '列族名称1','列族名称2','列族名称N' |
| 添加记录 |
put '表名称', '行名称', '列名称:', '值' |
| 查看记录 |
get '表名称', '行名称' |
| 查看表中的记录总数 |
count '表名称' |
| 删除记录 |
delete '表名' ,'行名称' , '列名称' |
| 删除一张表 |
先要屏蔽该表,才能对该表进行删除,第一步 disable '表名称' 第二步 drop '表名称' |
| 查看所有记录 |
scan "表名称" |
| 查看某个表某个列中所有数据 |
scan "表名称" , {COLUMNS=>'列族名称:列名称'} |
| 更新记录 |
就是重写一遍进行覆盖 |
### 八、HBASE Shell的DDL操作(数据库操作语言))
* 1、创建表
>create 'users','user_id','address','info'
表users,有三个列族user_id,address,info
* 2、列出全部表
>list
* 3、得到表的描述
>describe 'users'
* 4、创建表
>create 'users_tmp','user_id','address','info'
* 5、删除表
>disable 'users_tmp'
>drop 'users_tmp'
-------------------------------其他操作----------------------------------
* 其他操作
#$HBASE_HOME/bin/hbase shell
……
>quit
>exists 'users'
>is_enabled 'users'
>is_disabled 'users
### 九、HBASE Shell的DML操作(数据操作语言)
* 1、添加记录
put 'users','xiaoming','info:age','24';
put 'users','xiaoming','info:birthday','1987-06-17';
put 'users','xiaoming','info:company','alibaba';
put 'users','xiaoming','address:contry','china';
put 'users','xiaoming','address:province','zhejiang';
put 'users','xiaoming','address:city','hangzhou';
put 'users','zhangyifei','info:birthday','1987-4-17';
put 'users','zhangyifei','info:favorite','movie';
put 'users','zhangyifei','info:company','alibaba';
put 'users','zhangyifei','address:contry','china';
put 'users','zhangyifei','address:province','guangdong';
put 'users','zhangyifei','address:city','jieyang';
put 'users','zhangyifei','address:town','xianqiao';
* 2、获取一条记录
* 1.取得一个id的所有数据
>get 'users','xiaoming'
* 2.获取一个id,一个列族的所有数据
>get 'users','xiaoming','info'
* 3.获取一个id,一个列族中一个列的所有数据
get 'users','xiaoming','info:age'
* 3、更新记录
>put 'users','xiaoming','info:age' ,'29'
>get 'users','xiaoming','info:age'
>put 'users','xiaoming','info:age' ,'30'
>get 'users','xiaoming','info:age'
* 4、获取单元格数据的版本数据
>get 'users','xiaoming',{COLUMN=>'info:age',VERSIONS=>1}
>get 'users','xiaoming',{COLUMN=>'info:age',VERSIONS=>2}
>get 'users','xiaoming',{COLUMN=>'info:age',VERSIONS=>3}
* 5、获取单元格数据的某个版本数据
〉get 'users','xiaoming',{COLUMN=>'info:age',TIMESTAMP=>1364874937056}
* 6、全表扫描
>scan 'users'
* 7、删除xiaoming值的'info:age'字段
>delete 'users','xiaoming','info:age'
>get 'users','xiaoming'
* 8、删除整行
>deleteall 'users','xiaoming'
* 9、统计表的行数
>count 'users'
* 10、清空表
>truncate 'users'
### 十、HBASE的Java_API
* hbase操作必备
//hbase操作必备
private static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");
//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.quorum", "hadoop0");
return conf;
}
* 创建一张表
//创建一张表
public static void create(String tableName, String columnFamily) throws IOException{
HBaseAdmin admin = new HBaseAdmin(getConfiguration());
if (admin.tableExists(tableName)) {
System.out.println("table exists!");
}else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success!");
}
}
* 添加一条记录
//添加一条记录
public static void put(String tableName, String row, String columnFamily,
String column, String data) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Put p1 = new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(p1);
System.out.println("put'"+row+"',"+columnFamily+":"+column+"','"+data+"'");
}
* 读取一条记录
//读取一条记录
public static void get(String tableName, String row) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
System.out.println("Get: "+result);
}
* 显示所有数据
//显示所有数据
public static void scan(String tableName) throws IOException{
HTable table = new HTable(getConfiguration(), tableName);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Scan: "+result);
}
}
* 删除表
//删除表
public static void delete(String tableName) throws IOException{
HBaseAdmin admin = new HBaseAdmin(getConfiguration());
if(admin.tableExists(tableName)){
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Delete "+tableName+" 失败");
}
}
System.out.println("Delete "+tableName+" 成功");
}
* 主函数
public static void main(String[] args) throws IOException {
String tableName="hbase_tb";
String columnFamily="cf";
HBaseTestCase.create(tableName, columnFamily);
HBaseTestCase.put(tableName, "row1", columnFamily, "cl1", "data");
HBaseTestCase.get(tableName, "row1");
HBaseTestCase.scan(tableName);
HBaseTestCase.delete(tableName);
}
### 十一、练习——详单入库
* 1、题目要求(一)
* HBASE表定义为:
>create 'wlan_log', 'cf'
* 源文件数据增加一个字段rowkey
RowKey设计:
msisdn:日期时间串(yyyyMMddHHmmss)