大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
常见命令:
创新互联主要从事成都网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务和静,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
[root@hadoop01 ~]# hbase shell #进入HBASE客户端
hbase(main):001:0> help “dml” #获取一组命令的提示hbase(main):001:0> help "put" 获取一个单独命令的提示帮助
hbase(main):001:0> exit #退出客户端
#查看hbase中的所有表
hbase(main):001:0>list
创建表:
语法:create ‘表名’,’列簇1’,’ 列簇2’,’ 列簇3’....
# 例1:创建一张表,名称为user,该表有info和data两个列簇
hbase(main):001:0>create 'user' ,'info','data'
hbase(main):001:0>create 'user' ,{NAME=>'info'},{NAME=>'data'}
#例2:创建一张表叫做 user_info,包含两个列簇 base_info 和 extra_info,并且分别指定这两个列簇 的数据的版本数为 3 和 1
hbase(main):001:0>create 'user_info',{NAME=>'base_info',VERSIONS=>3},{NAME=>'extra_info',VERSIONS=>1}
查看表的详细信息
#desc 或者 describe
hbase(main):001:0>desc 'user_info'
hbase(main):001:0>describe 'user_info'
插入数据
#put table ,rowkey, 列簇:列 ,value
hbase(main):001:0>put 'user','rowkey01','info:age','18'
查询数据
#get
#获取 user 表中 row key 为 rk0001 的所有信息
hbase(main):001:0>get 'user' ,'rk0001'
#获取 user 表中 row key 为 rk0001,info 列簇的所有信息
hbase(main):001:0>get 'user','rk0001','info'
#获取 user 表中 row key 为 rk0001,info 列簇的 name、age 列标示符的信息
hbase(main):001:0>get 'user','rk0001','info:name','info:age'
#获取 user 表中 row key 为 rk0001,列簇为 info,版本号最新 5 个的信息
hbase(main):001:0>create 'USER','rk0001',{COLUMN=>'info',VERSIONS=>5}
#获取 user 表中 row key 为 rk0001,cell 的值为 zhangsan 的信息
hbase(main):001:0>get 'user', 'rk0001', {FILTER => "ValueFilter(=, 'binary:zhangsan')"}
#获取 user 表中 row key 为 rk0001,列标示符中含有 a 的信息
hbase(main):001:0>get 'user','rk0001',{FILTER=>"(QualifierFilter(=,'substring:a'))"}
查询数据
#scan
查询 user_info 表中的所有信息
hbase(main):001:0>scan 'user_info'
#查询 user_info 表中的指定列簇的所有信息
hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info'}
hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info:name'}
hbase(main):001:0>scan 'user_info',{COLUMNS=>['base_info','extra_info']}
#查询 user_info 表中的指定列簇为 base_info 的所有版本信息
hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info',VERSIONS=>5}
#查询 user 表中列簇为 info 和 data 且列标示符中含有 a 字符的信息
hbase(main):001:0>scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
#rowkey的范围查询
hbase(main):001:0>scan 'user_info', {COLUMNS => 'base_info', STARTROW => 'baiyc_20150716_0003', ENDROW =>
'baiyc_20150716_0006'}
#查询 user 表中 rowkey 以 rk 字符开头的
hbase(main):001:0>scan 'user',{FILTER=>"PrefixFilter('rk')"}
#查询 user_info 表中指定时间戳范围的数据
hbase(main):001:0>scan 'user_info',{TIMERANGE=>[1540882871681,1540882888540]}
删除数据
#delete
#删除记录
hbase(main):001:0>delete 'user', 'rk0001'
#删除字段
hbase(main):001:0>delete 'user','rk0001','info:name'
#删除 user 表 rowkey 为 rk0001,列标示符为 info:name,timestamp 为 1392383705316 的数据
hbase(main):001:0>delete 'user','rk0001','info:name',1392383705316
修改表结构
#alter
#添加两个列簇 f2 和 f3
hbase(main):001:0>alter 'user_info',NAME=>'f2'
hbase(main):001:0>alter 'user_info',NAME=>'f2'
#删除一个列簇 f1
hbase(main):001:0>alter 'user_info',NAME=>'f1',METHOD=>'delete'
#将 user_info 表的 base_info 列簇版本号改为 5
hbase(main):001:0>alter 'user_info',NAME=>'base_info',VERSIONS=>5
清空表
#truncate
#清空 user 表中的数据
hbase(main):001:0>truncate 'user_info'
停用表/启用表
#disable 和 enable
hbase(main):001:0>disable 'user'
hbase(main):001:0>enable 'user'
删除表
#drop
#删除一张表
hbase(main):001:0>disable 'user #需要先停用这张表,在删除
hbase(main):001:0>drop 'user'
这里实现基本的增删改查:
pom依赖
org.apache.hbase
hbase-client
1.2.6
org.apache.hbase
hbase-server
1.2.6
代码实现:
package com.zy.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import java.io.IOException;
public class HbaseTest {
// 声明静态配置
static Configuration conf = null;
private static final String ZK_CONNECT_STR = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", ZK_CONNECT_STR);
}
/**
* 1.创建表
*/
@Test
public void creatTable() {
//表名
String tableName = "stu_info";
//列簇名
String[] cf = {"base_info", "extra_info"};
try {
//创建HBaseAdmin对象
HBaseAdmin admin = new HBaseAdmin(conf);
//创建表的描述信息对象
HTableDescriptor des = new HTableDescriptor(tableName);
for (int i = 0; i < cf.length; i++) {
des.addFamily(new HColumnDescriptor(cf[i]));
}
if (admin.tableExists(tableName)) {
System.out.println("table Exists!");
System.exit(0);
} else {
admin.createTable(des);
System.out.println("create table Success!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 为表添加数据
*/
@Test
public void addData() {
//表名
String tableName = "user_info";
//行键
String rowkey = "rk0011";
//列簇
String cf = "base_info";
Put put = new Put(rowkey.getBytes());
try {
//创建操作hbase表的对象
HTable table = new HTable(conf, tableName.getBytes());
//列簇 列 值
put.addColumn(cf.getBytes(), "name".getBytes(), "zs".getBytes());
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据 rwokey 查询
*/
@Test
public void getResult() {
//表名
String tableName = "user_info";
//行键
String rowKey = "rk0001";
Get get = new Get(Bytes.toBytes(rowKey));
try {
//创建操作hbase表的对象
HTable table = new HTable(conf, tableName.getBytes());
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("rowkey:" + kv.getRow()); //行键
System.out.println("family" + kv.getFamily()); //列簇
System.out.println("qualifier" + kv.getQualifier()); //列
System.out.println("value:" + new String(kv.getValue())); //值
System.out.println("Timestamp" + kv.getTimestamp()); //时间戳
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 遍历查询 hbase 表
*/
@Test
public void getResultScann() {
String tableName = "user_info";
Scan scan = new Scan();
try {
HTable table = new HTable(conf, tableName.getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result rs : scanner) {
for (KeyValue kv : rs.list()) {
System.out.println("rowkey:" + kv.getRow()); //行键
System.out.println("family" + kv.getFamily()); //列簇
System.out.println("qualifier" + kv.getQualifier()); //列
System.out.println("value:" + new String(kv.getValue())); //值
System.out.println("Timestamp" + kv.getTimestamp()); //时间戳
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 遍历查询 hbase 表 指定rowkey的查询范围
*/
@Test
public void getResultScannRange() {
String tableName = "user_info";
Scan scan = new Scan();
scan.setStartRow("rk001".getBytes());
scan.setStopRow("rk005".getBytes());
try {
HTable table = new HTable(conf, tableName.getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result rs : scanner) {
for (KeyValue kv : rs.list()) {
System.out.println("rowkey:" + kv.getRow()); //行键
System.out.println("family" + kv.getFamily()); //列簇
System.out.println("qualifier" + kv.getQualifier()); //列
System.out.println("value:" + new String(kv.getValue())); //值
System.out.println("Timestamp" + kv.getTimestamp()); //时间戳
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询表中的某一列
*/
@Test
public void getResultByColumn() {
String tableName = "user_info"; //表
String rowkey = "rk0001"; //行键
String cf = "base_info"; //列簇
String column = "name"; //列
try {
HTable table = new HTable(conf, tableName.getBytes());
Get get = new Get(rowkey.getBytes());
get.addColumn(cf.getBytes(), column.getBytes());
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("rowkey:" + kv.getRow()); //行键
System.out.println("family" + kv.getFamily()); //列簇
System.out.println("qualifier" + kv.getQualifier()); //列
System.out.println("value:" + new String(kv.getValue())); //值
System.out.println("Timestamp" + kv.getTimestamp()); //时间戳
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 更新表中的某一列,因为在hbase中可以以时间戳定义多个版本的值
* 所有,更新操作就是put操作
*/
@Test
public void updateTable() {
String tableName = "user_info"; //表
String rowkey = "rk0001"; //行键
String cf = "base_info"; //列簇
String column = "name"; //列
String value = "ll"; //值
try {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Put put = new Put(rowkey.getBytes());
put.addColumn(cf.getBytes(), column.getBytes(), value.getBytes());
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询某列数据的多个版本
*/
@Test
public void getResultByVersion() {
String tableName = "user_info"; //表
String rowkey = "rk0001"; //行键
String cf = "base_info"; //列簇
String column = "name"; //列
int version = 5; //hbase的列的版本
try {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Get get = new Get(rowkey.getBytes());
get.addColumn(cf.getBytes(), column.getBytes());
get.setMaxVersions(version);
Result result = table.get(get);
if (result.list() != null) {
for (KeyValue kv : result.list()) {
System.out.println("rowkey:" + kv.getRow()); //行键
System.out.println("family" + kv.getFamily()); //列簇
System.out.println("qualifier" + kv.getQualifier()); //列
System.out.println("value:" + new String(kv.getValue())); //值
System.out.println("Timestamp" + kv.getTimestamp()); //时间戳
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除指定的列
*/
@Test
public void deleteColumn() {
String tableName = "user_info"; //表
String rowkey = "rk0001"; //行键
String cf = "base_info"; //列簇
String column = "name"; //列
try {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete delete = new Delete(rowkey.getBytes());
delete.addColumn(cf.getBytes(), column.getBytes());
table.delete(delete);
System.out.println(cf + ":" + column + "is deleted!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除指定rowKey的所有列
*/
@Test
public void deleteAllColumn() {
String tableName = "user_info"; //表
String rowkey = "rk0001"; //行键
try {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteAll = new Delete(rowkey.getBytes());
table.delete(deleteAll);
System.out.println("all columns are deleted!");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 删除表
*/
@Test
public void deleteTable(){
String tableName = "user_info"; //表
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName.getBytes());
admin.deleteTable(tableName.getBytes());
System.out.println(tableName + "is deleted!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
详细的源代码:http://down.51cto.com/data/2457979