api 原生hbase_hbase之java api实战一

相关接口文档:

第1步:创建maven项目

创建项目hbase

pom.xml:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.scitc

hbase

0.0.1-SNAPSHOT

jar

hbase

http://maven.apache.org

UTF-8

2.1.8

org.apache.hbase

hbase-server

${hbase.version}

org.apache.hbase

hbase-client

${hbase.version}

jdk.tools

jdk.tools

1.8

system

${JAVA_HOME}/lib/tools.jar

junit

junit

3.8.1

test

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

maven-assembly-plugin

jar-with-dependencies

make-assembly

package

single

第2步:HbaseOperation

HbaseOperation.java代码如下:

package com.scitc.hbase;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.*;

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;

/**

* @author geiliHe

* @date 2020-06-09

*/

public class HbaseOperation {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

public static TableDescriptor tableDescriptor;

//建立连接

public static void init(){

configuration  = HBaseConfiguration.create();

configuration.set("hbase.rootdir","hdfs://192.168.56.110:9000/hbase");

configuration.set("hbase.zookeeper.quorum", "master,slave1,slave2");

configuration.set("hbase.zookeeper.property.client", "2181");

try{

connection = ConnectionFactory.createConnection(configuration);

admin = connection.getAdmin();

}catch (IOException e){

e.printStackTrace();

}

}

//关闭连接

public static void close(){

try{

if(admin != null){

admin.close();

}

if(null != connection){

connection.close();

}

}catch (IOException e){

e.printStackTrace();

}

}

/**1:创建表-pass

* @param tableName   表名

* @param colFamily   列族

* @throws IOException

*/

public static void createTable(String tableName,String[] colFamily) throws IOException {

init();

TableName mytable = TableName.valueOf(tableName);

if(admin.tableExists(mytable)){

System.out.println("talbe is exists!");

}else {

//表描述器构造器

TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(mytable);

//创建集合用于存放列描述对象

List families = new ArrayList<>();

//将每个列族对应的ColumnFamilyDescriptor对象添加到集合中

for(String cols:colFamily) {

families.add(ColumnFamilyDescriptorBuilder.newBuilder(cols.getBytes()).build());

}

tableDescriptor = tdb.setColumnFamilies(families).build();

//创建表

admin.createTable(tableDescriptor);;

}

close();

}

/**

* 2:实现单个数据插入-pass

* 修改数和插入数据一样,不会覆盖原来版本,通过时间戳区别

* @param tableName

* @param rowKey

* @param colFamily

* @param col

* @param val

* @throws IOException

*/

public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

table.put(put);

table.close();

close();

}

/**

* 3-1:通过行键-列族-列:获取单个数据-pass

* @param tableName

* @param rowKey

* @param colFamily

* @param col

* @throws IOException

*/

public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Get get = new Get(rowKey.getBytes());

get.addColumn(colFamily.getBytes(),col.getBytes());

Result result = table.get(get);

showCell(result);

table.close();

close();

}

/**

* 3-2:通过行键获取值-pass

* @param tableName

* @param rowKey

* @throws IOException

*/

public static void getDataByRowkey(String tableName,String rowKey)throws  IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

//获得一行

Get get = new Get(Bytes.toBytes(rowKey));

Result set = table.get(get);

Cell[] cells = set.rawCells();

System.out.print("查询结果");

for (Cell cell: cells){

System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()) + "::" +

Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));

}

table.close();

close();

}

/**

* 4:给制定表添加列族

* @param tableName

* @param familyName

* @throws IOException

*/

public static void addColumnFamily(String tableName, String familyName) throws IOException {

init();

TableName tn = TableName.valueOf(tableName);

// 通过新增的familyName来构建ColumnFamilyDescriptor对象

ColumnFamilyDescriptor columnFamily = ColumnFamilyDescriptorBuilder.newBuilder(familyName.getBytes()).build();

admin.addColumnFamily(tn, columnFamily);

close();

}

/**

* 5:删除单个数据-pass

* @param tableName

* @param rowKey

* @param colFamily

* @param col

* @throws IOException

*/

public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(rowKey.getBytes());

//删除指定列族

//delete.addFamily(Bytes.toBytes(colFamily));

//删除指定列

//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));

table.delete(delete);

table.close();

close();

}

/**

* 格式化输出获取的数据

* @param result

*/

public static void showCell(Result result){

Cell[] cells = result.rawCells();

for(Cell cell:cells){

System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");

System.out.println("Timetamp:"+cell.getTimestamp()+" ");

System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");

System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");

}

}

/**

* 6:查看已有的表-pass

* @throws IOException

*/

public static void listTables() throws IOException {

init();

TableName[] tables = admin.listTableNames();

for(TableName table:tables) {

System.out.println("hbase数据库的表:"+table.toString());

}

close();

}

/**

* 7:删除一张表

* @param tableName

* @throws IOException

*/

public static void deleteTable(String tableName) throws IOException {

init();

TableName tn = TableName.valueOf(tableName);

if (!admin.tableExists(tn)) {

System.out.print("要删除的表不存在");

}else {

admin.disableTable(tn);

admin.deleteTable(tn);

}

close();

}

public static void main(String[] args) throws IOException {

//createTable("user",new String[]{"address","info"});

//insertRow("user", "tom", "info", "age", "26");

//insertRow("user", "tom", "info", "birthday", "1998-04-05");

//getData("user", "tom", "info", "age");

//deleteRow("user","tom","info","birthday");

//listTables();

getDataByRowkey("user","tom");

}

}

参考文献:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值