数据库操作:
表:
列修饰符:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
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.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class TestHbaseConnect {
private static Configuration config=null;//配置文件对象
private static Connection connection=null;//连接对象
private static Admin admin=null;//管理员对象,对表的相关操作
static {
try {
//1.获取hbase的配置文件对象
config=HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
//2.获取管理员对象
//HBaseAdmin admin=new HBaseAdmin(conf);
connection=ConnectionFactory.createConnection(config);
//3.创建管理员admin对象
admin=connection.getAdmin();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//1.测试表是否存在
//System.out.println(isTableExist("name:test01"));
//2.创建表测试
//createTable("HbaseAPI:HbaseAPI01", "info01","info02");
//3.删除表测试
//dropTable("HbaseAPI01");
//4.创建命名空间测试
//createNamespace("HbaseAPI");
//5.插入数据的测试
//putData("HbaseAPI:HbaseAPI01", "000001", "info01", "name", "pikce3_04");
//putData("HbaseAPI:HbaseAPI01", "000001", "info01", "name", "pikce3_05");
//putData("HbaseAPI:HbaseAPI01", "000004", "info01", "name", "pikce4");
//putData("HbaseAPI:HbaseAPI01", "000004", "info02", "name", "pikce5");
//6.获取单行数据
//getData("HbaseAPI:HbaseAPI01", "000001", "info01", "name");
//7.全表扫描
//scanTable("HbaseAPI:HbaseAPI01");
//8.测试删除数据
deleteData("HbaseAPI:HbaseAPI01", "000001", "info01", "name");
close();
}
/**
* 1. 判断表是否存在
*/
public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//判断表是否存在
boolean exists=admin.tableExists(TableName.valueOf(tableName));
return exists;
}
/**
* 2. 创建表
*/
public static void createTable(String tableName,String... cfs) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//1.判断是否存在列族信息
if (cfs.length<=0) {
System.out.println("请设置列族信息");
}
//2.判断表是否存在
if (isTableExist(tableName)) {
System.out.println("表已存在");
}
//3.创建表描述器
HTableDescriptor htableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
//4.循环添加列族信息
for (String cf: cfs) {
//5.创建列族描述器
HColumnDescriptor hColumnDescriptor =new HColumnDescriptor(cf);
//6.添加列族信息
htableDescriptor.addFamily(hColumnDescriptor);
}
//7.创建表,参数:表描述器
admin.createTable(htableDescriptor);
//创建表需要表描述器,创建表描述器之后需要天剑列族信息
}
/**
* 3. 删除表
*/
public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//1.判断表是否存在
if (isTableExist(tableName)==false) {
System.out.println(tableName+"表不存在");
return;
}
//2.禁用表
admin.disableTable(TableName.valueOf(tableName));
//3.删除表
admin.deleteTable(TableName.valueOf(tableName));
}
/**
* 4.创建命名空间
*/
public static void createNamespace(String ns) {
//1.创建命名空间描述器
NamespaceDescriptor namespaceDescriptor=NamespaceDescriptor.create(ns).build();
//2.创建一个命名空间
try {
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e) {//手动捕捉命名空间已存在的异常
System.out.println("命名空间已存在");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("测试!!!!!!");
}
/**
* 5.向表中插入数据
* tableName:表名
* rowkey:行键
* family:列族
* qualifier:列
* value:具体的数据
* @throws IOException
*/
public static void putData(String tableName,String rowkey,String family,String qualifier,String value) throws IOException {
//1.获取表对象
Table table=connection.getTable(TableName.valueOf(tableName));
//2.创建Put对象,参数是rowkey
//注意这里的put()对象rowkey参数只能是byte类型的,因为hbase底层是byte类型存储的
Put put=new Put(Bytes.toBytes(rowkey));
//3.给put对象赋值
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
//4.插入数据
table.put(put);
//5.关闭连接
table.close();
}
/**
* 6.从表中获取数据(Get)
* @throws IOException
*/
public static void getData(String tableName,String rowkey,String famliy,String qualifiler) throws IOException {
//1.获取表对象
Table table=connection.getTable(TableName.valueOf(tableName));
//2.创建Get对象
Get get=new Get(Bytes.toBytes(rowkey));
//2.1 获取指定的列族
get.addFamily(Bytes.toBytes(famliy));
//2.2获取指定的列族和列
get.addColumn(Bytes.toBytes(famliy), Bytes.toBytes(qualifiler));
//2.3获取数据的版本号
get.setMaxVersions(3);
//3.获取数据
Result result=table.get(get);
//4.解析result
for (Cell cell:result.rawCells()) {
//5.打印数据
System.out.println("family:"+Bytes.toString(CellUtil.cloneFamily(cell))+","+
"qualifiler:"+Bytes.toString(CellUtil.cloneQualifier(cell))+","+
"Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
//6.关闭表连接
table.close();
}
/**
* 6.1封装集合获取多条数据
* @throws IOException
*/
public static void getListData(String tableName,String rowkey1,String rowkey2,String family,String qualifier1,String qualifier2) throws IOException {
List<Get> list=new ArrayList<Get>();
//1.获取表对象
Table table=connection.getTable(TableName.valueOf(tableName));
//2.创建get对象
Get get1=new Get(rowkey1.getBytes());
get1.addColumn(Bytes.toBytes(family), qualifier1.getBytes());
Get get2=new Get(rowkey2.getBytes());
get2.addColumn(family.getBytes(), qualifier2.getBytes());
//3.设置可以获取的最大的版本
get1.setMaxVersions(3);
get2.setMaxVersions(3);
//4.添加get对象进入list集合
list.add(get1);
list.add(get2);
//5.获取返回值集合
Result[] results = table.get(list);
//6.遍历results对象,得到结果
for (Result result : results) {
for (Cell cell: result.rawCells()) {
System.out.println("family:"+Bytes.toString(CellUtil.cloneFamily(cell))+","+
"qualidier:"+Bytes.toString(CellUtil.cloneQualifier(cell))+","+
"value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
//7.关闭连接
table.close();
}
/**
* 7.从表中获取数据(Scan)
* @throws IOException
*/
public static void scanTable(String tableNanme) throws IOException {
//1.获取表对象
Table table=connection.getTable(TableName.valueOf(tableNanme));
//2.创建一个scan对象
Scan scan=new Scan();
//3.扫描全表
ResultScanner resultScanner=table.getScanner(scan);
//4.解析resultScanner
for (Result result : resultScanner) {//取出一个个的result
//5.解析result
for (Cell cell:result.rawCells()) {
//6.打印数据
System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))+","+"family:"+Bytes.toString(CellUtil.cloneFamily(cell))+","
+"qualifiler:"+Bytes.toString(CellUtil.cloneQualifier(cell))+","+
"Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
//7.关闭表连接
table.close();
}
/**
* 8.删除数据
*/
public static void deleteData(String tableName,String rowkey,String family,String qualifier) throws IOException {
//1.获取表对象
Table table=connection.getTable(TableName.valueOf(tableName));//
//2.构建删除对象
Delete delete=new Delete(Bytes.toBytes(rowkey));
//2.1设置删除的列
//delete.addColumn(family, qualifier);//如果传入的参数有时间戳,就给指定时间戳的版本打上删除标记;否则,只删除最新的版本
delete.addColumns(Bytes.toBytes(family),Bytes.toBytes(qualifier));//如果传入的参数有时间戳,就给删除小于等于这个时间戳之前版本打上删除标记的内容;如果没有传入参数时间戳,就给删除所有的版本打上删除标记
//2.2删除指定的列族
delete.addFamily(Bytes.toBytes(family));
//3.执行删除操作
table.delete(delete);
//4.关闭表连接
table.close();
}
/**
* 关闭资源和连接
* @throws IOException
*/
public static void close() throws IOException {
//关闭连接
if (admin!=null) {
admin.close();
}
if (connection!=null) {
connection.close();
}
}
}