安装篇
1.上传hbase安装包(测试使用的是hbase1.2.6)
2.解压(tar -zxvf hbse…… -C /usr/local)
3.配置hbase集群,要修改3个文件(首先zk集群已经安装好了)
注意:要把hadoop的hdfs-site.xml和core-site.xml 放到hbase/conf下
3.1修改hbase-env.sh
export JAVA_HOME=/usr/java/jdk1.7.0_55
//告诉hbase使用外部的zk
export HBASE_MANAGES_ZK=false
vim hbase-site.xml
hbase.rootdir
hdfs://hadoop01:9000/hbase
hbase.cluster.distributed
true
hbase.zookeeper.quorum
hadoop01:2181,hadoop02:2181,hadoop03:2181
vim regionservers
hadoop01
hadoop02
hadoop03
3.2拷贝hbase到其他节点
scp -r /hadoop01/hbase-0.96.2-hadoop2/ hadoop02:/usr/local/
4.将配置好的HBase拷贝到每一个节点并同步时间。
5.启动所有的hbase
分别启动zk
./zkServer.sh start
启动hadoop集群
start-dfs.sh
启动hbase,在主节点上运行:
start-hbase.sh
6.通过浏览器访问hbase管理页面
192.168.1.201:16010
7.为保证集群的可靠性,要启动多个HMaster
hbase-daemon.sh start master
链接:https://pan.baidu.com/s/14Q6z6bdDa-WgMu5TbOUgzA 密码:dzxd
操作数据
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
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.client.Table;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestData {
/*
*对数据的增删改查以及过滤器的使用
*/
/**
* //获取连接
*/
Configuration conf = null;
Connection connect = null;
Table table = null;
@Before
public void init() throws IOException {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
connect = ConnectionFactory.createConnection(conf);
}
/**
* 增加数据
* @throws Exception
*/
@Test
public void testadd() throws Exception {
table = connect.getTable(TableName.valueOf("t_user"));
//设置rowkey
Put put = new Put("001".getBytes());
//增加列族、列、值
Put put1 = put.addColumn("f3".getBytes(), "name".getBytes(), "lihailong".getBytes());
Put put2 = put.addColumn("f3".getBytes(), Bytes.toBytes("age"),Bytes.toBytes("18"));
Put put00 = new Put("002".getBytes());
Put put3 = put00.addColumn("f3".getBytes(), "name".getBytes(), "lihailong1".getBytes());
Put put4 = put00.addColumn("f3".getBytes(), Bytes.toBytes("age"),Bytes.toBytes("10"));
Put put01 = new Put("003".getBytes());
Put put5 = put01.addColumn("f3".getBytes(), "name".getBytes(), "lihailong2".getBytes());
Put put6 = put01.addColumn("f3".getBytes(), Bytes.toBytes("age"),Bytes.toBytes("180"));
//放到list中
List<Put> list = new ArrayList();
list.add(put1);
list.add(put2);
list.add(put3);
list.add(put4);
list.add(put5);
list.add(put6);
//增加数据
table.put(list);
}
/**
* 删除数据
* @throws IOException
*/
@Test
public void testDelete() throws IOException {
table = connect.getTable(TableName.valueOf("test_user"));
//删除哪一个key
Delete delete = new Delete(Bytes.toBytes("001"));
//删除哪个列、哪个属性
delete.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"));
//删除
table.delete(delete);
}
/**
* 数据查询
* @throws IOException
*/
@Test
public void getData() throws IOException {
table = connect.getTable(TableName.valueOf("t_user"));
//查询rowkey为001的数据
Get get = new Get(Bytes.toBytes("001"));
//调用查询的方法
Result result = table.get(get);
//获取查询到的value
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));
}
/**
*
* 多行查询
* @throws IOException
*
*/
@Test
public void getScan() throws IOException {
table = connect.getTable(TableName.valueOf("t_user"));
//创建新的扫描
Scan scan = new Scan();
//包含头不包含尾//不设置的话,全表扫描
scan.setStartRow(Bytes.toBytes("001"));
scan.setStopRow(Bytes.toBytes("002"));
//获取多行查询的结果
ResultScanner scanner = table.getScanner(scan);
//遍历所得到的值
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));
}
}
/**
* 列值过滤器
* @throws IOException
*
*/
@Test
public void testSingleColumnValueFilter() throws IOException {
//获取控制数据的对象
table = connect.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
//列值过滤器,后面的值分别是列族、列、如何比较、属性值
SingleColumnValueExcludeFilter clo = new SingleColumnValueExcludeFilter(Bytes.toBytes("f3"), Bytes.toBytes("age"), CompareOp.EQUAL, Bytes.toBytes("18"));
//给scan设置列值过滤器
scan.setFilter(clo);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));
}
}
/**
*
* 列名前缀过滤器
* @throws IOException
*/
@Test
public void testColumnPrefixFilter() throws IOException {
table = connect.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
//列名前缀
ColumnPrefixFilter prefix = new ColumnPrefixFilter(Bytes.toBytes("name"));
//给scan设置列值过滤器
scan.setFilter(prefix);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
}
}
/**
* 多个列值前缀过滤器
* @throws IOException
*/
@Test
public void testColumnPrefixFilterMore() throws IOException {
table = connect.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
//多个列值前缀,增加多个列值前缀
byte[][] prefixs = new byte[][] {Bytes.toBytes("name"),Bytes.toBytes("age")};
MultipleColumnPrefixFilter prefix = new MultipleColumnPrefixFilter(prefixs);
//给scan设置列值过滤器
scan.setFilter(prefix);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));
}
}
/**
* rowKey过滤器
* @throws IOException
*/
@Test
public void testRowFilter() throws IOException {
//获取控制数据的对象
table = connect.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
//rowKey过滤器,找到与后面正则表达式相等的rowkey
RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^001"));
//给scan设置列值过滤器
scan.setFilter(rowFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));
}
}
/**
* 过滤器列表,多个过滤器混合起来使用
* @throws IOException
*/
@Test
public void testFilterList() throws IOException {
table = connect.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
//里面的方法设置是过滤器全部使用
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^001")));
filterList.addFilter(new ColumnPrefixFilter(Bytes.toBytes("name")));
//给scan设置列值过滤器
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("name"));
System.out.println(new String(value));
/*byte[] value1 = result.getValue(Bytes.toBytes("f3"), Bytes.toBytes("age"));
System.out.println(new String(value1));*/
}
}
@After
public void end() throws IOException {
table.close();
connect.close();
}
}
操作表
这里写代码片
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellComparator.RowComparator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Test;
public class TestAdmin {
/**
* 整体都是一个思路:
* 操作表的话connect.getAdmin
* 操作数据的话,connect.getTable
* 之后返回的对象,调用增删改查等方法(里面需要什么数据就创建对象,添加数据)
*
*/
/**
* 创建表
* @throws Exception
*/
@Test
public void testCreateTable() throws Exception {
// hadoop里面的conf,会加载 hdfs-site.xml core-site.xml,mapred-site.xml ....
// hadoop 里面的配置文件 ,不会加载 hbase-site.xml
Configuration conf = HBaseConfiguration.create(); //会自动加载hbase-site.xml
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
//获取连接
Connection connect = ConnectionFactory.createConnection(conf);
//从连接中获取表管理对象
Admin admin = connect.getAdmin();
//表描述
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("t_user"));
//列族描述
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("base_info");
HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("family");
//
hTableDescriptor.addFamily(hColumnDescriptor);
hTableDescriptor.addFamily(hColumnDescriptor2);
//创建table
admin.createTable(hTableDescriptor);
//关闭资源
admin.close();
connect.close();
}
/**
* 删除表
* @throws Exception
*/
@Test
public void testDeleteTable() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
//获取连接
Connection connect = ConnectionFactory.createConnection(conf);
//从连接获取表管理对象
Admin admin = connect.getAdmin();
//先设为不可用
admin.disableTable(TableName.valueOf("t_user"));
//删除表
admin.deleteTable(TableName.valueOf("t_user"));
admin.close();
connect.close();
}
/**
* 修改表定义
* @throws IOException
*/
@Test
public void testAlterTable() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
//获取连接
Connection connect = ConnectionFactory.createConnection(conf);
//从连接获取表管理对象
Admin admin = connect.getAdmin();
//表描述
HTableDescriptor user = new HTableDescriptor(TableName.valueOf("t_user"));
//列族描述
HColumnDescriptor f3 = new HColumnDescriptor("f3");
user.addFamily(f3);
//修改表定义
admin.modifyTable(TableName.valueOf("t_user"),user );
admin.close();
connect.close();
}