实用代码| hbase表的创建以及简单的增删改查

Posted 数据开放资源

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实用代码| hbase表的创建以及简单的增删改查相关的知识,希望对你有一定的参考价值。

学习是一件终身的事情,让我们一起成长!!


import java.io.IOException;

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.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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.util.Bytes;

public class HBaseClient {

private static Configuration configuration =HBaseConfiguration.create();

static{

System.out.println(configuration.get("hbase.master"));

}

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

//HBaseClient.createTable("users", "info", 3);

//HBaseClient.putTable("mytable", "s-"+ String.valueOf(new Date().getTime()), "cf", "col","xxx");

//HBaseClient.getTable("mytable", "s-1528369637196");

HBaseClient.getValueForResult("mytable", "s-1528369637196", "cf", "col");

}

//创建表

public static void createTable(String tableName,String columnFamily,Integer versionNum) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(configuration);

if (admin.tableExists(tableName)) {

System.out.println(tableName+"已存在!");

}else {

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);

if (versionNum != null) {

hColumnDescriptor.setMaxVersions(versionNum);

}

hTableDescriptor.addFamily(hColumnDescriptor);

admin.close();

System.out.println(tableName+"创建成功!");

}

}

//向表里增加内容

public static void putTable(String tableName,String row,String columnFamily,String column,String data) throws IOException{

HTable table = new HTable(configuration, tableName);

Put put = new Put(Bytes.toBytes(row));

put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),data.getBytes());

table.put(put);

table.close();

System.out.println("put--->row:" + row + ",columnFamily:" + columnFamily + ",column:" + column + ",data:" + data);

}

//获取表里一行数据

public static Result getTable(String tableName,String row) throws IOException{

HTable hTable = new HTable(configuration, tableName);

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

Result result = hTable.get(get);

System.out.println("Get: " + result);

Cell[] cells = result.rawCells();

for (Cell cell : cells) {

String cloneFamily = Bytes.toString(CellUtil.cloneFamily(cell));

String cloneQualifier = Bytes.toString(CellUtil.cloneQualifier(cell));

String cloneValue = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println("cloneFamily: "+cloneFamily);

System.out.println("cloneQualifier: "+cloneQualifier);

System.out.println("cloneValue: "+cloneValue);

}

hTable.close();

return result;

}

//获取表里一列数据

public static String getValueForResult(String tableName, String row , String columnFamily, String column) throws Exception {

HTable table = new HTable(configuration, tableName);

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

Result result = table.get(get);

System.out.println("Get: " + result);

byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));

String m = new String(value); System.out.println("Found row: " + m);

table.close();

return m;

}

}


数据开放资源

ID:coreplatform

■ 中国数据资源开放平台

 让数据流通更可信、高效、安全


以上是关于实用代码| hbase表的创建以及简单的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

MySQL基本操作--库表增删改查

MySQL-增删改查简易操作

MySQL-增删改查简易操作

Oracle数据库的增删改,索引视图以及序列的创建和销毁

c#中怎么实现表的增删改查?

TP5的多对多模型,以及中间表,已经中间表的增删改查