pom文件配置:
cloudera
https://repository.cloudera.com/artifactory/cloudera-repos/
org.apache.hadoop
hadoop-client
2.6.0-mr1-cdh5.14.0
org.apache.hbase
hbase-client
1.2.0-cdh5.14.0
org.apache.hbase
hbase-server
1.2.0-cdh5.14.0
junit
junit
4.12
test
org.testng
testng
6.14.3
test
org.junit.jupiter
junit-jupiter-api
RELEASE
compile
org.apache.maven.plugins
maven-compiler-plugin
3.0
1.8
UTF-8
org.apache.maven.plugins
maven-shade-plugin
2.2
package
shade
*:*
META-INF/*.SF
META-INF/*.DSA
META-INF/*/RSA
代码:
package com.czxy.demo1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author kismet
* @date 2019-12-17 15:29
*/
public class Hbase01 {
/**
* 创建一个新的表myuser,列族为f1,f2
* @throws IOException
*/
public static void createTable() throws IOException {
//创建连接
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
//实例admin管理员 创建 删除hbase表需要使用的admin
Admin admin = connection.getAdmin();
//创建表 表名:myuser
TableName myuser = TableName.valueOf("myuser2");
//设置表结构
HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
//创建列族为f1,f2
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
//建立表与列族间的关系
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
admin.createTable(hTableDescriptor);
admin.close();
//关闭连接
connection.close();
}
/**
* 向myuser表中添加一条数据,rowkey为0001,在列族f1里添加列id,name,age, 在列族f1里添加列address,phone
* @throws IOException
*/
public static void addDatas() throws IOException {
//创建连接
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
Connection connection = ConnectionFactory.createConnection(conf);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0001".getBytes());
//添加列
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("张三"));
put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));
put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("158741025aa"));
//插入数据
myuser.put(put);
//关闭表
myuser.close();
}
/**
* 向myuser表中添加多条数据
* @throws IOException
*/
public static void putdatas() throws IOException {
//创建连接
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
Connection connection = ConnectionFactory.createConnection(conf);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0002".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0005".getBytes());
put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0006".getBytes());
put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));
List listPut = new ArrayList();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuser.put(listPut);
myuser.close();
}
/**
*查询主键rowkey为0003的人
* @throws IOException
*/
public static void searchData() throws IOException {
//创建连接
Configuration configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//指定表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//设置需要读取的数据(rowkey)
Get get = new Get("0004".getBytes());
//读取数据,一个result 一行数据
Result result = myuser.get(get);
//遍历数据
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//所属的列族
// System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
//所属的列
// System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
//值
// System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id")||Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
connection.close();
}
/**
* 按照rowkey查询指定列族下面的指定列的值
* @throws IOException
*/
public static void searchdata2() throws IOException {
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Get get = new Get("0003".getBytes());
get.addColumn("f1".getBytes(),"name".getBytes());
Result result = myuser.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
connection.close();
}
/**
*通过startRowKey和endRowKey进行扫描
* @throws IOException
*/
public static void scanrowkey() throws IOException {
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
scan.setStartRow("0002".getBytes());
scan.setStopRow("0006".getBytes());
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id")||Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+":"+Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+":"+Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
connection.close();
}
/**
* 全表扫描
* @throws IOException
*/
public static void scanrow() throws IOException {
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan =new Scan();
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id")||Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+":"+Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+":"+Bytes.toString(CellUtil.cloneFamily(cell))+"-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
connection.close();
}
public static void main(String[] args) throws IOException {
// createTable();
// addDatas();
// putdatas();
// searchData();
// searchdata2();
// scanrowkey();
// scanrow();
}
}