1. 启动与停止
(1) 启动主节点进行 <hbase_home>/bin/hbase-daemon.sh start master
(2) 启动从节点进程 <hbase_home>/bin/hbase-daemon.sh start regionserver
停止进程,即将对于节点上启动命令的start改成stop
<hbase_home>/bin/hbase-daemon.sh stop regionserver
2. 进入hbase客户端
<hbase_home>/bin/hbase shell
3.基本命令
创建表 :
create "表名","列簇1","列簇2"
查询表 : list
添加数据 : put "表名","行键","列簇:列","值"
查询表所有数据 : scan "表名"
删除数据 : delete "表名","行键","列簇:列"
删除表 表失效:disable "tableName"
删除表:drop "tableName"
8. java api查询操作
maven依赖,根据自己hbase的版本导入对应的依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.24-hadoop2</version>
</dependency>
java代码
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Text {
/**
* 过滤器实现了类介绍
* 行键过滤 RowFilter
* 列簇名过滤 FamilyFilter
* 列过滤 QualifierFilter
* 值过滤 ValueFilter
*/
public static void main(String[] args) throws IOException {
//分页查询
String page = getPage("00003", 2);//分页查询传入起始位置与返回数量
System.out.println("下一个起始页码" + page);
}
/**
*
* @param lastRowkey 起始行键
* @param page 页码
* @return 下一次查询的起始行键
* @throws IOException
*/
public static String getPage(String lastRowkey, Integer page) throws IOException {
//配置参数
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "zookeeper_ip");
conf.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConf.set("zookeeper.znode.parent", "/hbase");//1.4.9需要指定zk路径
//创建查询类
Scan scan = new Scan();
//定义过滤器
Filter filter1 = new ValueFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*"));
Filter filter2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^c"));
Filter filter3 = new PageFilter(page);//查询条数
//过滤器组合
FilterList filterlist = new FilterList();
filterlist.addFilter(filter1);
filterlist.addFilter(filter2);
filterlist.addFilter(filter3);
//组合查询
scan.setFilter(filterlist);
scan.setStartRow(lastRowkey.getBytes());//分页查询使用的key为下一个其实位置,需要添加0
HTable table = new HTable(conf, "查询表名");
ResultScanner scanner = table.getScanner(scan);//获取所有结果集
String format = " %-25s%-18s%-30s%-5s";//输出格式
System.out.println(String.format("%-25s%-18s", "ROW", "COLUMN+CELL"));//格式化输出
for (Result res : scanner) {//获取一行数据
for (Cell cell : res.listCells()) {//获取各个列的值
String row = Bytes.toString(CellUtil.cloneRow(cell));//行键
String value = "value=" + Bytes.toString(CellUtil.cloneValue(cell));//值
String family = Bytes.toString(CellUtil.cloneFamily(cell));//列簇
String col = Bytes.toString(CellUtil.cloneQualifier(cell));//列名
String column = "column=" + family + ":" + col;//列簇与列
String timestamp = "timestamp=" + cell.getTimestamp();//时间戳
System.out.println(String.format(format, row, column, timestamp, value));//格式化输出
}
lastRowkey = Bytes.toString(res.getRow()) + "0";//给下一次查询起始rowkey位置赋值
System.out.println("----------------------------------");
}
//防止程序异常,这里需要try-catch关闭连接
scanner.close();
table.close();
//返回下一次查询的起始行键,用于翻页
return lastRowkey;
}
}
翻页查询的起始位置设置
老版本api startRow表示介于下一个与上一个之间的行键
final byte[] postFox = new byte[] { 0x00 };
byte[] startRow = Bytes.add("当前rowkey".getBytes(), postFox);
新版本api
scan.withStartRow("当前行键",false);//false表示不包含这个行键
运行结果如下:
9.新版api hbase2.1.0
入库
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "主机ip");
conf.set("zookeeper.znode.parent", "/hbase");
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("表名称"));
Put put = new Put(Bytes.toBytes("行键"));
put.addColumn(Bytes.toBytes("列簇"), Bytes.toBytes("列"), Bytes.toBytes("value值"));
table.put(put);
table.close();
查询
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "mini1");
Scan scan = new Scan();
Connection connection = ConnectionFactory.createConnection(conf);
//定义过滤器
FilterList filterlist = new FilterList();
scan.setFilter(filterlist);
Table table = connection.getTable(TableName.valueOf("tbone"));
//new HTable(conf, "tbone");
ResultScanner scanner = table.getScanner(scan);//获取所有结果集
String format = " %-25s%-18s%-30s%-5s";//输出格式
System.out.println(String.format("%-25s%-18s", "ROW", "COLUMN+CELL"));//格式化输出
for (Result res : scanner) {//获取一行数据
for (Cell cell : res.listCells()) {//获取各个列的值
String row = Bytes.toString(CellUtil.cloneRow(cell));//行键
String value = "value=" + Bytes.toString(CellUtil.cloneValue(cell));//值
String family = Bytes.toString(CellUtil.cloneFamily(cell));//列簇
String col = Bytes.toString(CellUtil.cloneQualifier(cell));//列名
String column = "column=" + family + ":" + col;//列簇与列
String timestamp = "timestamp=" + cell.getTimestamp();//时间戳
System.out.println(String.format(format, row, column, timestamp, value));//格式化输出
}
System.out.println("----------------------------------");
}
//防止程序异常,这里需要try-catch关闭连接
scanner.close();
table.close();
创建表
Admin admin = conn.getAdmin();
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("mytest"));
//创建列簇
HColumnDescriptor columnDescriptor1 = new HColumnDescriptor("F");
columnDescriptor1.setVersions(1, 5); //设置列簇版本从1到5
columnDescriptor1.setTimeToLive(24 * 60 * 60); //秒
//绑定关系
descriptor.addFamily(columnDescriptor1);
//创建表
admin.createTable(descriptor);