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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;
public class HbaseTest {
public static Configuration conf;
static {
//使用HBaseConfiguration的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.93.101");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
@Test
public void isTableExist() throws IOException {
//在HBase中管理、访问表需要先创建HBaseAdmin对象
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
// HBaseAdmin admin = new HBaseAdmin(conf);
boolean flag = admin.tableExists(TableName.valueOf("student"));
System.out.println("表已经存在:"+flag);
}
@org.junit.Test
public void createTable() throws Exception {
//在HBase中管理、访问表需要先创建HBaseAdmin对象
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (!admin.tableExists(TableName.valueOf("test:teacher"))){
TableDescriptorBuilder bulilder=
TableDescriptorBuilder.newBuilder(TableName.valueOf("test:teacher"));
ColumnFamilyDescriptorBuilder cfdb=
ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes());
cfdb.setMaxVersions(20);
ColumnFamilyDescriptor cfd=cfdb.build();
bulilder.setColumnFamily(cfd);
TableDescriptor td=bulilder.build();
//4。指向相关DDL操作
admin.createTable(td);
System.out.println("表创建成功");
}else {
System.out.println("表已经存在!");
}
}
@org.junit.Test
public void insert() throws IOException {
//在HBase中管理、访问表需要先创建HBaseAdmin对象
Connection conn = ConnectionFactory.createConnection(conf);
HTable hTable = (HTable) conn.getTable(TableName.valueOf("test:teacher"));
Put put = new Put("1001".getBytes());
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("30"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("phone"),Bytes.toBytes("321312"));
hTable.put(put);
}
/*
得到某一行的数据
*/
@org.junit.Test
public void getInfoRow() throws Exception {
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("student"));
Get get = new Get("1001".getBytes());
get.setTimestamp(1573197358407L);//显示指定时间戳的版本
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print("行键: " + Bytes.toString(result.getRow()));
System.out.print("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("列: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.print("时间戳: " + cell.getTimestamp());
System.out.println();
}
}
@org.junit.Test
public void getALLInfoScan() throws Exception {
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//得到rowkey
System.out.print("行键: " + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.print("列族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("列: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println();
}
}
}
@org.junit.Test
public void getAllInfoMap() throws IOException {
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("student"));
Get get = new Get(Bytes.toBytes("1001"));
//通过行健得到列族的信息表
Result result = table.get(get);
//通过行健得到列族的映射信息
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> m : map.entrySet()) {
//拿到列族的信息
String cf_key = new String(m.getKey());
NavigableMap<byte[], NavigableMap<Long, byte[]>> qv = m.getValue();
for (Map.Entry<byte[], NavigableMap<Long, byte[]>> m1 : qv.entrySet()) {
//拿到时间戳的信息
String qf_key = new String(m1.getKey());
NavigableMap<Long, byte[]> vv = m1.getValue();
for (Map.Entry<Long, byte[]> vvs : vv.entrySet()) {
long version = vvs.getKey();
//拿到值的信息
String cell_value = new String(vvs.getValue());
System.out.println("列族:" + cf_key + " 列名:" + qf_key + " " +
" 版本:" + version + " cell值:" + cell_value);
}
}
}
}
}
使用java程序操作hbase数据库
最新推荐文章于 2022-12-01 16:55:32 发布