Maven pom.xml
添加依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.6.1</version>
</dependency>
创建表
import org.apache.hadoop.conf.Configuration;
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;
/**
* 创建表
*/
public class CreateTable {
/**
* 创建表
*
* @param tName 表名
* @param familyName 列族名
* @throws IOException .
*/
public void createTable(String tName, String familyName) throws IOException {
//创建HBase配置对象
Configuration conf = new Configuration();
//指定ZooKeeper集群地址
conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
//创建连接对象
Connection connection = ConnectionFactory.createConnection(conf);
//得到数据库管理员对象
Admin admin = connection.getAdmin();
//创建表描述,并指定表名
TableName tableName = TableName.valueOf(tName);
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
//创建列族描述
HColumnDescriptor familyDescriptor = new HColumnDescriptor(familyName);
//指定列族
tableDescriptor.addFamily(familyDescriptor);
//创建表
admin.createTable(tableDescriptor);
}
public static void main(String[] args) throws IOException {
new CreateTable().createTable("t1", "f1");
}
}
添加数据
import org.apache.hadoop.conf.Configuration;
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 {
/**
* 添加数据
*
* @param tName 表名
* @param familyName 列族名
* @throws IOException .
*/
public void putData(String tName, String familyName) throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
Connection connection = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf(tName);
Table table = connection.getTable(tableName);
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name"), Bytes.toBytes("xiaoming"));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age"), Bytes.toBytes("20"));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address"), Bytes.toBytes("beijing"));
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name2"), Bytes.toBytes("xiaoming2"));
put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age2"), Bytes.toBytes("22"));
put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address2"), Bytes.toBytes("beijing2"));
Put put3 = new Put(Bytes.toBytes("row3"));
put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name"), Bytes.toBytes("xiaoming3"));
put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age3"), Bytes.toBytes("25"));
put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address3"), Bytes.toBytes("beijing3"));
table.put(put);
table.put(put2);
table.put(put3);
table.close();
}
public static void main(String[] args) throws IOException {
new PutData().putData("t1", "f1");
/***********************************************************/
new CreateTable().createTable("t2","f2");
new PutData().putData("t2", "f2");
/***********************************************************/
}
}
查询数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 查询数据
*/
public class GetData {
/**
* 查询数据
*
* @param tName 表名
* @param rowKeys rowKeys
* @throws IOException .
*/
public void getData(String tName, String... rowKeys) throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
Connection connection = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf(tName);
Table table = connection.getTable(tableName);
for (String rowKey : rowKeys) {
Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
//获得当前单元格所属的列族名称
String family = new String(CellUtil.cloneFamily(cell));
//获得当前单元格所属的列名称
String qualifier = new String(CellUtil.cloneQualifier(cell));
//获取当前单元格的列值
String value = new String(CellUtil.cloneValue(cell));
System.out.println(rowKey + " column=" + family + ":" + qualifier + ",value=" + value);
}
}
}
public static void main(String[] args) throws IOException {
new GetData().getData("t1", "row1", "row2", "row3");
new GetData().getData("t2", "row1", "row2", "row3");
}
}
删除数据
import org.apache.hadoop.conf.Configuration;
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.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 删除数据
*/
public class DeleteData {
/**
* 删除数据
*
* @param tName 表名
* @param rowKeys rowKeys
* @throws IOException .
*/
public void deleteData(String tName, String... rowKeys) throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
Connection connection = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf(tName);
Table table = connection.getTable(tableName);
for (String rowKey : rowKeys) {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
table.close();
}
public static void main(String[] args) throws IOException {
new DeleteData().deleteData("t1", "row1", "row2", "row3");
}
}
删除表
import org.apache.hadoop.conf.Configuration;
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;
/**
* 删除表
*/
public class DeleteTable {
/**
* 删除表
*
* @param tNames 表名列表
* @throws IOException .
*/
public void deleteTable(String... tNames) throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
for (String tName : tNames) {
TableName tableName = TableName.valueOf(tName);
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
public static void main(String[] args) throws IOException {
new DeleteTable().deleteTable("t1", "t2");
}
}