如何使用java连接hbase?
底层如何操作,我们并不关心;只期望提供以下几个方法
方法1:连接hbase;输入参数:hbase的ip,端口号;
方法2:namespace的crud;
方法3:table的crud;
方法4:记录的crud;
添加jar包
把%hbase_home%\lib\所有的jar包都加进来;
添加工具包:junit和log4j2
(hbase-2.2.2-src\hbase-2.2.2\hbaseserver\src\test\java\org\apache\hadoop\hbase\HBaseTestingUtility.java)
HbaseBaseTest
package com.jinghangzz.hbase.test;
import java.io.IOException;
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.HBaseAdmin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Hbase的测试类
* ~找到一个Configuration(配置文件的信息)
* ~得需要一个Connection,得需要一个对象就是Configuration
* ~得需要一个HbaseAdmin
* ~看到源码有一个方法叫createTable
* @author TeaBig
*/
public class HbaseBaseTest
{
/* 日志的笔 */
protected Logger logger = LogManager.getLogger() ;
/* 这个对象是玩hbase特别重要的 */
protected HBaseAdmin hBaseAdmin ;
protected Connection connection ;
/**
* 初始化的方法
*/
@Before
public void init()
{
/* 告诉Hbase,你的Hadoop搁哪了 */
System.setProperty("hadoop.home.dir", "E:\\帮助文档\\大数据\\hadoop-3.2.1");
/* 创建一个配置文件对象 */
Configuration conf = HBaseConfiguration.create();
/* 在hbase里面有一个配置文件hbase-site.xml(忽略)
* 这个对应 是hbase-site.xml里面的内容
* 键就是property-->name
* 值就是property-->value
* 您可以一个一个的设置进去
* 也可以直接把配置文件放到项目的classpath中,它会自动的扫描
* */
//conf.set(name, value);
conf.set("hbase.zookeeper.quorum", "node7-1:2181,node7-2:2181,node7-3:2181");
try
{
/* 获取一个Connection对象 */
connection = ConnectionFactory.createConnection(conf);
/* 通过connection获取到一个HbaseAdmin */
hBaseAdmin = (HBaseAdmin) connection.getAdmin() ;
this.logger.info("==init==hBaseAdmin:{}",hBaseAdmin);
} catch (IOException e)
{
this.logger.error("创建对象时失败了,",e);
}
}
/**
* 销毁的方法
*/
@After
public void close()
{
this.logger.info("==close=={}");
try
{
if(this.hBaseAdmin != null)
{
this.hBaseAdmin.close();
this.hBaseAdmin = null ;
}
} catch (IOException e)
{
this.logger.error("关闭时失败了,",e);
}
try
{
if(this.connection != null)
{
this.connection.close();
this.connection = null ;
}
} catch (IOException e)
{
this.logger.error("关闭时失败了,",e);
}
// this.logger.info("==close=={}" , this.connection.isClosed());
}
/**
* 执行的目标方法
*/
@Test
public void test()
{
try
{
/* list:列出所有的表 */
TableName[] tables = this.hBaseAdmin.listTableNames() ;
for (int i = 0; i < tables.length; i++)
{
TableName tableNameTemp = tables[i];
this.logger.info("==test==计数:{},名字:{}",i,tableNameTemp);
}
} catch (IOException e)
{
this.logger.error("测试出错了",e);
}
}
}
ClientCommandTest
package com.jinghangzz.hbase.test;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Get;
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.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.junit.Test;
/**
* 测试所有的客户端的命令
* @author TeaBig
*/
public class ClientCommandTest extends HbaseBaseTest
{
/* 数据库 */
private String nameSpace = "java" ;
/* 表名 */
private String tableName = "java:myTest";
/* 列族 */
private String cf = "cf" ;
/**
* list_namespace
*/
@Test
public void nameSpace_list()
{
/* list_namespace */
try
{
NamespaceDescriptor[] namespaceDescriptors = this.hBaseAdmin.listNamespaceDescriptors();
for (int i = 0; i < namespaceDescriptors.length; i++)
{
NamespaceDescriptor namespaceDescriptor = namespaceDescriptors[i];
String name = namespaceDescriptor.getName();
this.logger.info("==nameSpace_list==计数:{},名字:{}",i,name);
}
} catch (IOException e)
{
this.logger.error("报错了",e);
}
}
/**
* create_namespace ''
*/
@Test
public void nameSpace_create()
{
try
{
NamespaceDescriptor descriptor = NamespaceDescriptor.create(nameSpace).build();
/* create_namespace '' */
this.hBaseAdmin.createNamespace(descriptor);
this.logger.info("==createNamespace==");
} catch (IOException e)
{
this.logger.error("报错了",e);
}
}
/**
* table创建 ''
* create 'namespace:表名',列族'
*/
@Test
public void table_create()
{
try
{
/* 表的名字 */
TableName tableNameObj = TableName.valueOf(tableName);
/* 表的描述信息 */
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableNameObj);
/* 列族的描述信息 */
ColumnFamilyDescriptor familyDescriptor = ColumnFamilyDescriptorBuilder.of(cf);
/* 添加列族;单个列族 */
builder.setColumnFamily(familyDescriptor);
/* 获取表的描述信息 */
TableDescriptor desc = builder.build() ;
if(this.hBaseAdmin.tableExists(tableNameObj))
{
/* 表存在 */
/* 禁用表 */
this.hBaseAdmin.disableTable(tableNameObj);
/* 删除表 */
this.hBaseAdmin.deleteTable(tableNameObj);
}
/* create_namespace '' */
this.hBaseAdmin.createTable(desc);
this.logger.info("==table_create==");
} catch (IOException e)
{
this.logger.error("报错了",e);
}
}
/**
* 查看表
* list
*/
@Test
public void table_list()
{
try
{
/* list:列出所有的表 */
TableName[] tables = this.hBaseAdmin.listTableNames() ;
for (int i = 0; i < tables.length; i++)
{
TableName tableNameTemp = tables[i];
this.logger.info("==test==计数:{},名字:{}",i,tableNameTemp);
}
} catch (IOException e)
{
this.logger.error("测试出错了",e);
}
}
/**
* put '表名','rowKey','列族的属性','值'
*/
@Test
public void table_put()
{
try
{
/* 表的描述信息 */
TableName tableNameObj = TableName.valueOf(tableName);
/* 获取到表对象 */
Table table = this.connection.getTable(tableNameObj);
/* 获取表名字 */
this.logger.info("name:{}",table.getName().getNameAsString());
/* 行键(键) */
String rowKey = "02" ;
/* 插入数据的对象 */
Put put = new Put(rowKey.getBytes("UTF-8"));
/* 列族的属性
* 参数1:列族
* 参数2:属性
* 参数3:值
* */
put.addColumn(this.cf.getBytes("UTF-8"), "name".getBytes("UTF-8"), "lisi".getBytes("UTF-8"));
put.addColumn(this.cf.getBytes("UTF-8"), "age".getBytes("UTF-8"), "20".getBytes("UTF-8"));
put.addColumn(this.cf.getBytes("UTF-8"), "createTime".getBytes("UTF-8"), "2019-11-11 11:11:11".getBytes("UTF-8"));
/* 添加数据
* 参数如果是Put,那就是单条操作,
* 参数如果是List,那就是海量操作(批处理);
* */
table.put(put);
this.logger.info("执行完毕");
} catch (IOException e)
{
this.logger.error("测试出错了",e);
}
}
/**
* get '表名','rowKey'
*/
@Test
public void table_get()
{
try
{
/* 表的描述信息 */
TableName tableNameObj = TableName.valueOf(tableName);
/* 获取到表对象 */
Table table = this.connection.getTable(tableNameObj);
/* 获取表名字 */
this.logger.info("name:{}",table.getName().getNameAsString());
/* 行键(键) */
String rowKey = "02" ;
/* 条件,rowKey */
Get get = new Get(rowKey.getBytes("UTF-8"));
/* 根据rowKey查询数据
* */
Result result = table.get(get);
/* 取单个 */
byte[] valueByte = result.getValue(cf.getBytes("UTF-8"), "name".getBytes("UTF-8"));
this.logger.info("根据键,取单个列:{}",new String(valueByte,"UTF-8"));
/* 获取最新的值 */
Cell lastCell = result.getColumnLatestCell(cf.getBytes("UTF-8"), "name".getBytes("UTF-8"));
byte[] lastCells = lastCell.getValueArray() ;
this.logger.info("根据键,取单个列:{}",new String(lastCells,"UTF-8"));
} catch (IOException e)
{
this.logger.error("测试出错了",e);
}
}
/**
* scan '表名'
*/
@Test
public void table_scan()
{
try
{
/* 表的描述信息 */
TableName tableNameObj = TableName.valueOf(tableName);
/* 获取到表对象 */
Table table = this.connection.getTable(tableNameObj);
/* 获取表名字 */
this.logger.info("name:{}",table.getName().getNameAsString());
/* 获取所有数据 */
Scan scan = new Scan();
/* 扫描整张表 */
ResultScanner resultScan = table.getScanner(scan);
for (Iterator iterator = resultScan.iterator(); iterator.hasNext();)
{
Result result = (Result) iterator.next();
/* 取单个 */
byte[] nameByte = result.getValue(cf.getBytes("UTF-8"), "name".getBytes("UTF-8"));
this.logger.info("根据键,取单个列:{}",new String(nameByte,"UTF-8"));
byte[] ageByte = result.getValue(cf.getBytes("UTF-8"), "age".getBytes("UTF-8"));
this.logger.info("根据键,取单个列:{}",new String(ageByte,"UTF-8"));
byte[] createTimeByte = result.getValue(cf.getBytes("UTF-8"), "createTime".getBytes("UTF-8"));
this.logger.info("根据键,取单个列:{}",new String(createTimeByte,"UTF-8"));
}
} catch (IOException e)
{
this.logger.error("测试出错了",e);
}
}
}