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 + "不存在!");
} }