导包
<dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.4.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> </dependency> </dependencies>
注意:
1、import org.apache.hadoop.hbase.util.Bytes;
用到该类下的 Bytes.toString() 将byte-->String
2、一般用 CellUtil 获取数据,而非scan
将一条数据所有的cell列举出来,使用CellUtil从每一个cell中取出数据,不需要考虑每条数据的结构
rs.listCells()
CellUtil.cloneValue(cell)
package com.shujia;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Deemo02API {
Connection conn;
Admin admin;
@Before
public void createconn() throws IOException {
//1.创建一个配置文件
Configuration conf = HBaseConfiguration.create();
//配置ZK的地址,通过ZK可以找到HBase
conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
//2.创建连接
conn = ConnectionFactory.createConnection(conf);
//3.如果需要对表结构操作 getAdmin
// 对数据进行操作 getTable
admin = conn.getAdmin();
}
/**
* 1.list 列出所有的表
*/
@Test
public void list() throws IOException {
TableName[] tableNames = admin.listTableNames();
for (TableName name : tableNames) {
System.out.println(name.getNameAsString());
}
}
/**
* 2.put 添加数据
*/
@Test
public void put() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Put put = new Put("0002".getBytes());
//相当于插入一列(一个cell)数据
put.addColumn("cf1".getBytes(), "name".getBytes(), "李四".getBytes());
put.addColumn("cf1".getBytes(), "age".getBytes(), "28".getBytes());
put.addColumn("cf1".getBytes(), "phone".getBytes(), "1888887".getBytes());
testAPI.put(put);
}
/**
* 3.get 根据rowkey获取一条数据
*/
@Test
public void get() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get = new Get("0002".getBytes());
Result rs = testAPI.get(get);
//获取rk
byte[] rk = rs.getRow();
System.out.println(rk);
System.out.println(Bytes.toString(rk));
//获取value
byte[] name = rs.getValue("cf1".getBytes(), "name".getBytes());
System.out.println(name);
System.out.println(Bytes.toString(name));
}
/**
* 4.create table 建表
* students info
*/
@Test
public void createtable() throws IOException {
HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
HColumnDescriptor info = new HColumnDescriptor("info");
student.addFamily(info);
admin.createTable(student);
}
/**
* 5.drop table 删除表
*/
@Test
public void droptable() throws IOException {
//判断表是否存在
TableName test1 = TableName.valueOf("test1");
if (admin.tableExists(test1)) {
admin.disableTable(test1);
admin.deleteTable(test1);
}
}
/**
* 6.modify table 修改表结构
* 针对test表 将其cf1列簇的TTL设置为10000 并增加一个新的列簇info
*/
@Test
public void modifyTable() throws IOException {
TableName test = TableName.valueOf("test");
//获取表原有的结构
HTableDescriptor tableDescriptor = admin.getTableDescriptor(test);
//在表原有的结构中 修改列簇属性
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
//遍历表中原有的列簇
for (HColumnDescriptor cf : columnFamilies){
//对原有的cf1列簇进行修改
if("cf1".equals(cf.getNameAsString())){
cf.setTimeToLive(10000);
}
}
//增加新的列簇
HColumnDescriptor info = new HColumnDescriptor("info");
tableDescriptor.addFamily(info);
admin.modifyTable(test,tableDescriptor);
}
/**
* 7.putAll 读取学生信息数据并写入HBase的student表
*/
@Test
public void putAll() throws IOException {
/**
* 读取学生信息
*/
BufferedReader br = new BufferedReader(new FileReader("data/students.txt"));
//与HBase中的student表建立连接
Table student = conn.getTable(TableName.valueOf("student"));
String line;
//用集合进行批量写入
ArrayList<Put> puts = new ArrayList<Put>();
int batchsize=10;
while((line=br.readLine())!=null){
//写入HBase
String[] split = line.split(",");
String id = split[0];
String name = split[1];
String age = split[2];
String gender = split[3];
String clazz = split[4];
//id作为rowkey
Put put = new Put(id.getBytes());
byte[] info = "info".getBytes();
//添加单元格(列簇,列名,值)
put.addColumn(info,"name".getBytes(),name.getBytes());
put.addColumn(info,"age".getBytes(),age.getBytes());
put.addColumn(info,"gender".getBytes(),gender.getBytes());
put.addColumn(info,"clazz".getBytes(),clazz.getBytes());
//每条数据都会执行依次,效率很慢
// student.put(put);
//将每个Put对象加入puts集合
puts.add(put);
//当puts集合的大小同batchsize大小一致时,则调用HTable的put方法批量写入
if(puts.size()==batchsize){
student.put(puts);
//清空集合
puts.clear();
}
}
//当batchsize的大小同数据条数不成正比的时候,可能会造成最后几条数据不能写入
//手动去判断puts集合是否为空,不为空则将其写入HBase
if(!puts.isEmpty()){
student.put(puts);
}
br.close();
}
/**
* 8.scan 获取一组数据
* 读取student表
*/
@Test
public void getscan() throws IOException {
Table student = conn.getTable(TableName.valueOf("student"));
//scan可以指定rowkey的范围进行查询,或者是限制返回的条数
Scan scan = new Scan();
scan.withStartRow("1500100100".getBytes());
scan.withStopRow("1500100111".getBytes());
ResultScanner scanner = student.getScanner(scan);
for (Result rs : scanner) {
//获取rowkey
String id = Bytes.toString(rs.getRow());
//获取值 (列簇,列名)
String name = Bytes.toString(rs.getValue("info".getBytes(), "name".getBytes()));
String age = Bytes.toString(rs.getValue("info".getBytes(), "age".getBytes()));
String gender = Bytes.toString(rs.getValue("info".getBytes(), "gender".getBytes()));
String clazz = Bytes.toString(rs.getValue("info".getBytes(), "clazz".getBytes()));
System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);
}
}
/**
*9. CellUtil 直接获取一条数据所有cell
*
*/
@Test
public void CellUtil() throws IOException {
Table student = conn.getTable(TableName.valueOf("student"));
//scan可以指定rowkey的范围进行查询,或者是限制返回的条数
Scan scan = new Scan();
scan.withStartRow("1500100100".getBytes());
scan.withStopRow("1500100111".getBytes());
scan.setLimit(5);
for (Result rs : student.getScanner(scan)) {
//将一条数据所有的cell列举出来
//使用CellUtil从每一个cell中取出数据
//不需要考虑每条数据的结构
String id = Bytes.toString(rs.getRow());
System.out.print(id+" ");
List<Cell> cells = rs.listCells();
for (Cell cell : cells) {
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(val+" ");
}
System.out.println();
}
}
@After
public void close() throws IOException {
//用完记得关闭连接
admin.close();
conn.close();
}
}