一.导入核心jar包
hbase/lib/下的115个jar包
二.编写代码
package com.shsxt.ly;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HelloJdbc {
//声明Hbase链接
private HBaseAdmin hBaseAdmin;
private HTable hTable;
//表的名称声明
private static final String SXT_TABLE_NAME="teacher";
/**
* 配置文件
*
* @throws IOException
*/
@Before
public void init() throws IOException {
//设置配置文件
Configuration configuration = new Configuration(true);
//设置zookeeper集群
configuration.set("hbase.zookeeper.quorum", "bd1301:2181,bd1302:2181,bd1303:2181");
//获取数据库的链接
hBaseAdmin = new HBaseAdmin(configuration);
//获取指定表链接,还有表名
hTable = new HTable(configuration,SXT_TABLE_NAME);
}
/**
* 创建表
* 1.创建表对象
* 2.创建列族对象
* 3.将列族添加到表中
* 4.在数据库里创建表
*
* @throws IOException
*/
@Test
public void sxtCreateTable() throws IOException{
//创建表对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(SXT_TABLE_NAME));
//创建列族对象
HColumnDescriptor info = new HColumnDescriptor("info");
info.setMaxVersions(3);
HColumnDescriptor work = new HColumnDescriptor("work");
//将列族添加到列表
hTableDescriptor.addFamily(info);
hTableDescriptor.addFamily(work);
//在数据库中建表
hBaseAdmin.createTable(hTableDescriptor);
}
/**
* 添加数据(都是Byte[]格式)
* 1.创建Put对象,赋值rowkey
* 2.给put添加值,赋值列族,列,还有value
* 3.hTable插入数据
*
* @throws RetriesExhaustedWithDetailsException
* @throws InterruptedIOException
*/
@Test
public void sxtPut() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
//创建put对象 还有rowkey
Put put = new Put("t201901".getBytes());
//添加数据
put.add("info".getBytes(), "name".getBytes(), "yinwei".getBytes());
put.add("info".getBytes(), "age".getBytes(), "18".getBytes());
put.add("work".getBytes(),"java".getBytes(),"js".getBytes());
//插入数据
hTable.put(put);
}
/**
* 插入多个数据,与单个数据一样
* put可以放list
*
* @throws RetriesExhaustedWithDetailsException
* @throws InterruptedIOException
*/
@Test
public void sxtPuts() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
//声明List存放多次的插入对象
List<Put> list = new ArrayList<>();
for(int i = 10; i < 100; i++){
//创建put对象 声明rowkey
Put put = new Put(("t2019"+i).getBytes());
put.add("info".getBytes(), "name".getBytes(), ("yinwei" + i).getBytes());
put.add("info".getBytes(), "age".getBytes(), String.valueOf(18).getBytes());
put.add("work".getBytes(), "java".getBytes(), "javase".getBytes());
//将结果到list中
list.add(put);
}
//插入数据
hTable.put(list);
}
/**
* 获取数据
* 1.声明put对象 赋值rowkey
* ①.指定列族 列名 --> get.addColumn("work".getBytes(), "java".getBytes());
* get获取,返回result对象,只包含这个数据rowkey 列族 列的数据
*
* ②没有指定列族 列名
* get获取可以获取到全部(见下面的获取多条数据sxtGets()方法)
*
* @throws IOException
*/
@Test
public void sxtGet() throws IOException{
//创建get对象
Get get = new Get("t201910".getBytes());
//只获取指定(列族:列)的数据
get.addColumn("work".getBytes(), "java".getBytes());
//开始获取数据,get返回的是result数据
Result result = hTable.get(get);
//结果为 keyvalues={t201910/work:java/1574943779905/Put/vlen=6/mvcc=0}
System.out.println(result);
//获取这个rowkey的其他Cell
Cell cell = result.getColumnLatestCell("info".getBytes(), "name".getBytes());
//值为null
System.out.println(cell);
//cell是null
if (cell != null) {
//获取数据,没有
System.out.println(new String(CellUtil.cloneValue(cell)));
}
}
/**
* 获取多条数据
* 1.声明一个list
* 2.将get放到list
* 3.通过get获取数据 返回result的字节数组
* 4.遍历数组,可以获得数据
*
* @throws IOException
*/
@Test
public void sxtGets() throws IOException{
List<Get> list = new ArrayList<>();
for (int i = 88; i < 99; i++) {
//创建get对象
Get get = new Get(("t2019"+i).getBytes());
//将get放入list中
list.add(get);
}
//get获取数据返回result数组
Result[] results = hTable.get(list);
//遍历数组
for (Result result : results) {
//获取Cell
Cell cell = result.getColumnLatestCell("info".getBytes(), "name".getBytes());
if (cell != null) {
//获取数据 为yinwei88 yinwei89 yinwei90 yinwei91...
System.out.println(new String(CellUtil.cloneValue(cell)));
}
}
}
/**
* 扫描器
* 1.创建扫描器
* 2.设置列族 列名
* 3.设置rowkey的启终
* 4.获取结果ResultScanner
* 5.迭代获取数据
*
* @throws IOException
*/
@Test
public void sxtScan() throws IOException{
//创建扫描器
Scan scan = new Scan();
//指定列名和列族
scan.addColumn("info".getBytes(), "name".getBytes());
//设置开始的rowkey
scan.setStartRow("t201988".getBytes());
//设置结束的rowkey
scan.setStopRow("t201999".getBytes());
//获取扫描结果
ResultScanner scanner =hTable.getScanner(scan);
//获取迭代器 迭代result类
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result =iterator.next();
//获取结果 返回的是cell 用cellutils将其变为字节数组 然后再转为string
System.out.println(new String(CellUtil.cloneValue(result.getColumnLatestCell("info".getBytes(), "name".getBytes()))));
}
}
/**
* 关闭资源
*
* @throws IOException
*/
@After
public void destroy() throws IOException {
if(null != hTable){
hTable.close();
}
if(null != hBaseAdmin){
hBaseAdmin.close();
}
}
}