HBase第四天——HBase API 操作
自己的话:我愿平东海,身沉心不改;
大海无平期,我心无绝时。
HBase的java代码开发
一、环境准备
新建项目后在 pom.xml 中添加依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
二、HBaseAPI
1. 获取 Configuration 对象、
public static Configuration conf;
static{
//使用 HBaseConfiguration 的单例方法实例化
//创建hbase配置文件
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//创建hbase数据库连接对象
//org.apache.hadoop.conf.Configuration
Connection connection = ConnectionFactory.createConnection(configuration);
}
2.判断表是否存在
public static boolean isTableExist(String tableName) throws
MasterNotRunningException,
ZooKeeperConnectionException, IOException{
//在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
//Connection connection = ConnectionFactory.createConnection(conf);
//HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
HBaseAdmin admin = new HBaseAdmin(conf);
return admin.tableExists(tableName);
}
3.创建表
package com.dfzy.day01;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
//创建表dashuju
public class CreateUser {
public static void main(String[] args) throws IOException {
//创建hbase配置文件
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//创建hbase数据库连接对象
//org.apache.hadoop.conf.Configuration
Connection connection = ConnectionFactory.createConnection(configuration);
//获取一个用户
Admin admin = connection.getAdmin();
//创建表的描述对象
//创建表名对象
TableName tableName = TableName.valueOf("dashuju");
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
hTableDescriptor.addFamily(new HColumnDescriptor("f1"));
hTableDescriptor.addFamily(new HColumnDescriptor("f2"));
admin.createTable(hTableDescriptor);
//关闭资源
admin.close();
connection.close();
}
}
4.向表中添加数据(单条)
package com.dfzy.day01;
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.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//添加数据
public class PutData {
public static void main(String[] args) throws IOException {
//创建配置文件对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
//创建hbase数据库连接
Connection connection = ConnectionFactory.createConnection(configuration);
//创建表对象
TableName tableName = TableName.valueOf("dashuju");
Table table = connection.getTable(tableName);
//添加数据
//创建put对象
Put put = new Put("rk001".getBytes());
put.addColumn("f1".getBytes(),"name".getBytes(),"zhangsan".getBytes());
put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(20));
put.addColumn("f2".getBytes(),"address".getBytes(),"diyu".getBytes());
put.addColumn("f2".getBytes(),"phone".getBytes(),"8888888888".getBytes());
table.put(put);
table.close();
connection.close();
}
}
5.向表中添加数据(批量)
package com.dfzy.day01;
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.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//批量添加数据
public class BatchData {
public static void main(String[] args) throws IOException {
//创建数据库连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table table = connection.getTable(TableName.valueOf("dashuju"));
//添加数据
//创建put集合
List<Put> list = new ArrayList<>();
//创建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.add(put);
list.add(put2);
list.add(put3);
list.add(put4);
list.add(put5);
list.add(put6);
table.put(list);
table.close();
connection.close();
}
}
6.查询数据
package com.dfzy.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//获取单条数据
public class GetData {
public static void main(String[] args) throws IOException {
//创建数据库连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表名对象
Table table = connection.getTable(TableName.valueOf("dashuju"));
//创建get对象
Get get = new Get("rk001".getBytes());
Result result = table.get(get);
//遍历
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] value = cell.getValue();
//判断列名
if ("age".equals(Bytes.toString(cell.getQualifier()))){
System.out.println(Bytes.toInt(value));
}else{
System.out.println(Bytes.toString(value));
}
}
}
}
7.获取所有数据
package com.dfzy.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//通过scan进行全表扫描
public class ScanData {
public static void main(String[] args) throws IOException {
//创建数据库连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//创建表对象
Table table = connection.getTable(TableName.valueOf("dashuju"));
//创建扫描器对象
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
//获取到每一行数据
for (Result result : scanner) {
//获取单元格数据
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//获取列族
System.out.println(Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),cell.getFamilyLength()));
//获取列名
System.out.println(Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),cell.getQualifierLength()));
//获取列值
System.out.println(Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),cell.getValueLength()));
}
}
}
}
8.获取某几行的范围查询
按照rowkey进行范围查询
package com.dfzy.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//rowkey的范围查询
public class ScanStartRowData {
public static void main(String[] args) throws IOException {
//创建数据库连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//创建表对象
Table table = connection.getTable(TableName.valueOf("dashuju"));
//创建扫描器对象
Scan scan = new Scan();
scan.setStartRow("0002".getBytes());
scan.setStopRow("0005".getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
//单元格数组
Cell[] cells = result.rawCells();
//遍历
for (Cell cell : cells) {
System.out.println(Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),cell.getValueLength()));
}
}
table.close();
connection.close();
}
}
9.获取某一行指定“列族:列”的数据
按照rowkey查询指定列族下面的指定列的值
package com.dfzy.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//通过rowkey查询指定列族下面的指定列的值
public class GetFamilyData {
public static void main(String[] args) throws IOException {
//创建数据库连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//创建表对象
Table table = connection.getTable(TableName.valueOf("dashuju"));
//创建get对象
Get get = new Get("rk001".getBytes());
get.addColumn("f1".getBytes(),"name".getBytes());
//开始查询数据
Result result = table.get(get);
//获取数据
Cell[] cells = result.rawCells();
//遍历
for (Cell cell : cells) {
System.out.println(Bytes.toString(cell.getValue()));
}
}
}
10.根据rowkey删除数据
public void deleteByRowKey() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Delete delete = new Delete("0001".getBytes());
myuser.delete(delete);
myuser.close();
}
11.删除表
public static void dropTable(String tableName) throws
MasterNotRunningException,
ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(isTableExist(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表" + tableName + "删除成功!");
}else{
System.out.println("表" + tableName + "不存在!");
} }