package com.imooc.hbase;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HBaseOp {
public HBaseOp() {
}
public static void main(String[] args) throws Exception{
//获取配置
Configuration conf= HBaseConfiguration.create();
//指定HBase使用zk的地址,多个用逗号隔开
conf.set("hbase.zookeeper.quorum","bigdata01:2181,bigdata02:2181,bigdata03:2181");
// 指定HBase在hdfs上的根目录
conf.set("hbase.rootdir","hdfs://bigdata01:9000/hbase");
// 创建HBase连接,负责HBase中数据的一些增删改查DML操作
Connection conn= ConnectionFactory.createConnection(conf);
// 添加数据
// put(conn);
//查询数据
// get(conn);
// 查询多版本的数据 conn getMoreVersion
// getMoreVersion(conn);
// 删除数据
// delete(conn);
// 获取管理权限,负责对HBase中的表进行操作DDL
Admin admin= conn.getAdmin();
admin.close();
conn.close();
// 创建表
// createTable(admin);
// 删除表,先禁用表
// deleteTable(admin);
}
private static void deleteTable(Admin admin) throws IOException {
admin.disableTable(TableName.valueOf("test"));
admin.deleteTable(TableName.valueOf("test"));
}
private static void createTable(Admin admin) throws IOException {
ColumnFamilyDescriptor familyDesc1=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"))
.setMaxVersions(3).build();
ColumnFamilyDescriptor familyDesc2=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("level"))
.setMaxVersions(2).build();
ArrayList<ColumnFamilyDescriptor> familyList= new ArrayList<ColumnFamilyDescriptor>();
familyList.add(familyDesc1);
familyList.add(familyDesc2);
// 获取TableDescriptor对象
TableDescriptor desc=TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
.setColumnFamilies(familyList).build();
admin.createTable(desc);
}
private static void delete(Connection conn) throws IOException {
Table table= conn.getTable(TableName.valueOf("student"));
Delete delete=new Delete(Bytes.toBytes("laowang"));
table.delete(delete);
table.close();
}
private static void getMoreVersion(Connection conn) throws IOException {
Table table= conn.getTable(TableName.valueOf("student"));
Get get =new Get(Bytes.toBytes("laowang"));
get.readAllVersions();
Result result=table.get(get);
List<Cell> columnCells=result.getColumnCells(Bytes.toBytes("info"),Bytes.toBytes("age"));
for (Cell cell:columnCells){
byte[] value_bytes=CellUtil.cloneValue(cell);
long timestamp=cell.getTimestamp();
System.out.println("值为:"+new String(value_bytes)+",时间戳:"+timestamp);
}
table.close();
}
private static void get(Connection conn) throws IOException {
Table table= conn.getTable(TableName.valueOf("student"));
Get get =new Get(Bytes.toBytes("laowang"));
Result result=table.get(get);
List<Cell> cells=result.listCells();
for (Cell cell:cells){
byte[] famaily_bytes= CellUtil.cloneFamily(cell);
byte[] column_bytes=CellUtil.cloneQualifier(cell);
byte[] value_bytes=CellUtil.cloneValue(cell);
System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
}
System.out.println("========================================================");
byte[] age_bytes= result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"));
System.out.println("age列的值"+new String(age_bytes));
table.close();
}
private static void put(Connection conn) throws IOException {
Table table= conn.getTable(TableName.valueOf("student"));
Put put=new Put(Bytes.toBytes("laowang"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("18"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("man"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("class"),Bytes.toBytes("A"));
table.put(put);
table.close();
}
}
hbase增删改查
猜你喜欢
转载自blog.csdn.net/weixin_45631815/article/details/121205953
今日推荐
周排行