如何将原始数据类型存储在hbase中并进行检索 [英] How to store primitive data types in hbase and retrieve

查看:138
本文介绍了如何将原始数据类型存储在hbase中并进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用hbase api存储和检索原始数据类型?我的任务是将随机事件保存在包含随机生成的不可预知数据类型的hbase上。并且每当我想要时都需要检索它们?请有人帮我解决这个问题。因为我对hbase和这个东西真的很陌生。

How can i store and retrieve primitive data types using hbase api.? My task is to save random events on hbase that contains unpredictable data types which are generated randomly. and need to retrieve them after whenever i want? can someone help me out this please. because i'm really new to hbase and this stuff.

推荐答案

这是您将数据放入HBase表的方式:

This is how you put data into a HBase table :

Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "TABLE_NAME");
Put p = new Put(rowKey);
p.add(Bytes.toBytes("cf"), Bytes.toBytes("c1"), Bytes.toBytes("VALUE"));
table.put(p);

您不必担心数据的类型。但是,您需要记住,HBase内部的任何内容都会以字节数组的形式出现。因此,在从HBase获取数据时,您需要将其转换回合适的类型,因为您每次都会得到一个bytearray。这可以使用由字节类提供的各种重载方法来完成。像这样:

You don't have to worry about the type of the data. However, you need to keep in mind that anything which goes inside HBase goes as an array of bytes. So, while fetching the data back from HBase you need to convert it back into the suitable type because you will be getting a bytearray everytime. This can be done using various overloaded methods provided by the Bytes class. Like this :

Bytes.toString(byte[])
Bytes.toFloat(byte[])
Bytes.toLong(byte[])

这篇关于如何将原始数据类型存储在hbase中并进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆