前提条件,启动hadoop集群,zookeeper集群,Hbase集群,访问hbase的web页面
<!--添加hbase相关依赖-->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.2.1</version>
</dependency>
package ning.test.hadoop;
import lombok.SneakyThrows;
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 org.junit.Before;
import org.junit.Test;
/**
* 操作hbase java-API
*/
public class HbaseTest {
private static Admin admin;
private static Connection connection;
@Before
@SneakyThrows
public void initHbase() {
System.out.println("------------->>>>>>>>before<<<<<<<<-----------------");
Configuration configuration = HBaseConfiguration.create();
//configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "192.168.72.136");
//集群配置↓
configuration.set("hbase.master", "192.168.72.136,192.168.72.137,192.168.72.138");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}
@Test
@SneakyThrows
public void createTable() {
String tableNmae = "table_dong";
String[] cols = new String[]{"information", "contact", "meet", "teacher"};
TableName tableName = TableName.valueOf(tableNmae);
if (admin.tableExists(tableName)) {
System.out.println("表已存在!");
} else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for (String col : cols) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("----------------->>>>>>>>>>finish<<<<<<<<<<-----------------");
}
}
@Test
@SneakyThrows
public void insertData() {
TableName tablename = TableName.valueOf("table_dong");
Put put = new Put(("ning-" + "123456789").getBytes());
//参数:1.列族名 2.列名 3.值
put.addColumn("information".getBytes(), "age".getBytes(), "1".getBytes());
put.addColumn("contact".getBytes(), "gender".getBytes(), "男".getBytes());
put.addColumn("meet".getBytes(), "phone".getBytes(), "15076500954".getBytes());
put.addColumn("teacher".getBytes(), "name".getBytes(), "张老师".toString().getBytes());
Table table = connection.getTable(tablename);
table.put(put);
}
//获取原始数据
@Test
@SneakyThrows
public void getTableData() {
Table table = connection.getTable(TableName.valueOf("table_dong"));
Scan scan = new Scan();
ResultScanner resutScanner = table.getScanner(scan);
for (Result result : resutScanner) {
System.out.println("scan: " + result);
}
}
//查询指定表名中所有的数据
@Test
@SneakyThrows
public void getAllData() {
String tableName = "table_dong";
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner results = table.getScanner(new Scan());
for (Result result : results) {
String id = new String(result.getRow());
System.out.println("rowKey值:" + new String(result.getRow()));
for (Cell cell : result.rawCells()) {
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
//String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("colName------>>>>" + colName + "<<<<------");
System.out.println("value------>>>>" + value + "<<<<------");
}
}
}
//根据rowKey进行查询
@Test
@SneakyThrows
public void getDataByRowKey() {
String rowKey = "ning-123456789";
//String rowKey = "user-a782d04b-8264-4118-8576-af2e8f2c349a";
Table table = connection.getTable(TableName.valueOf("table_dong"));
Get get = new Get(rowKey.getBytes());
//先判断是否有此条数据
if (!get.isCheckExistenceOnly()) {
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(value);
}
}
}
//查询指定cell内容
@Test
@SneakyThrows
public void getCellData() {
String tableName = "table_dong";
String rowKey = "ning-123456789";
String family = "meet";
String col = "phone";
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
if (!get.isCheckExistenceOnly()) {
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(col));
Result res = table.get(get);
byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
System.out.println(Bytes.toString(resByte));
} else {
System.out.println("暂无数据");
}
}
//删除表名下,对应的rowKey下的值,或者删除rowKey
@Test
@SneakyThrows
public void deleteByRowKey() {
String tableName = "table_dong";
String rowKey = "ning-123456789";
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
//删除指定列
//delete.addColumns(Bytes.toBytes("meet"), Bytes.toBytes("email"));
table.delete(delete);
}
//删除表
@Test
@SneakyThrows
public void deleteTable() {
String tableName = "table_dong";
TableName tablename = TableName.valueOf(tableName);
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println("删除操作执行完毕!");
}
}