一个使用HBase的例子,如下。
import java.io.IOException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scanner;
import org.apache.hadoop.hbase.io.BatchUpdate;
import org.apache.hadoop.hbase.io.Cell;
import org.apache.hadoop.hbase.io.RowResult;
public class MyClient {
public static void main(String args[]) throws IOException {
// You need a configuration object to tell the client where to connect.
// But don't worry, the defaults are pulled from the local config file.
HBaseConfiguration config = new HBaseConfiguration();
// This instantiates an HTable object that connects you to the "myTable"
// table.
HTable table = new HTable(config, "myTable");
// To do any sort of update on a row, you use an instance of the BatchUpdate
// class. A BatchUpdate takes a row and optionally a timestamp which your
// updates will affect.
BatchUpdate batchUpdate = new BatchUpdate("myRow");
// The BatchUpdate#put method takes a Text that describes what cell you want
// to put a value into, and a byte array that is the value you want to
// store. Note that if you want to store strings, you have to getBytes()
// from the string for HBase to understand how to store it. (The same goes
// for primitives like ints and longs and user-defined classes - you must
// find a way to reduce it to bytes.)
batchUpdate.put("myColumnFamily:columnQualifier1",
"columnQualifier1 value!".getBytes());
// Deletes are batch operations in HBase as well.
batchUpdate.delete("myColumnFamily:cellIWantDeleted");
// Once you've done all the puts you want, you need to commit the results.
// The HTable#commit method takes the BatchUpdate instance you've been
// building and pushes the batch of changes you made into HBase.
table.commit(batchUpdate);
// Now, to retrieve the data we just wrote. The values that come back are
// Cell instances. A Cell is a combination of the value as a byte array and
// the timestamp the value was stored with. If you happen to know that the
// value contained is a string and want an actual string, then you must
// convert it yourself.
Cell cell = table.get("myRow", "myColumnFamily:columnQualifier1");
String valueStr = new String(cell.getValue());
// Sometimes, you won't know the row you're looking for. In this case, you
// use a Scanner. This will give you cursor-like interface to the contents
// of the table.
Scanner scanner =
// we want to get back only "myColumnFamily:columnQualifier1" when we iterate
table.getScanner(new String[]{"myColumnFamily:columnQualifier1"});
// Scanners in HBase 0.2 return RowResult instances. A RowResult is like the
// row key and the columns all wrapped up in a single interface.
// RowResult#getRow gives you the row key. RowResult also implements
// Map, so you can get to your column results easily.
// Now, for the actual iteration. One way is to use a while loop like so:
RowResult rowResult = scanner.next();
while(rowResult != null) {
// print out the row we found and the columns we were looking for
System.out.println("Found row: " + new String(rowResult.getRow()) + " with value: " +
rowResult.get("myColumnFamily:columnQualifier1".getBytes()));
rowResult = scanner.next();
}
// The other approach is to use a foreach loop. Scanners are iterable!
for (RowResult result : scanner) {
// print out the row we found and the columns we were looking for
System.out.println("Found row: " + new String(result.getRow()) + " with value: " +
result.get("myColumnFamily:columnQualifier1".getBytes()));
}
// Make sure you close your scanners when you are done!
scanner.close();
}
}
在这个例子中,使用了HBase中的很多概念,包括:
HBaseConfiguration: 用于告诉client如何连接,连接到哪个HBase的服务器上。
HTable:代表一个HBase表格。
BatchUpdate:用于表格中一行的更新。包括添加某个列,修改某列的值,删除某列等。
commit:table的一个方法。代表某个BatchUpdate操作可以生效了。类似于数据库中的commit操作。
Cell:table中对应某个(行key, 列值,时间戳)下的单元格值。
获取Cell的方法。For example:
table.get("myRow", "myColumnFamily:columnQualifier1");
scanner:用于遍历表格。
rowResult:遍历过程当中保存某行信息。
--
从上面可以看到,HBase中的数据都是Bytes。HBase并不care里面实际存的数据到底是什么数据,只要
该数据可以转化成byte[]即可。
分享到:
相关推荐
本人原创, 1.Hbase连接需要改Hbase包中的两个配置文件,加上Hbase所在机器ip及端口 2.HBaseDMLT初始化环境 3.MapDataIni为建表 4.MapDataInsert为写数据 5.MapDataDelete为删除数据 6.MapDataRead为读数据
不同的是,HBase 有列族的概念,它将一列或者多列组织在一起,HBase 的每一个列都必须属于某个列族。 HBase 是一个可以进行随机访问的存取和检索数据的存储平台,存储结构化和半结构化的数据,因此一般的网站可以将...
不同的是,HBase 有列族的概念,它将一列或者多列组织在一起,HBase 的每一个列都必须属于某个列族。 HBase 是一个可以进行随机访问的存取和检索数据的存储平台,存储结构化和半结构化的数据,因此一般的网站可以将...
java 利用 sping-data-hadoop HbaseTemplate 操作hbase find get execute 等方法 可以直接运行
1. HBase有哪些基本的特征? 1 HBase特征: 1 2. HBase相对于关系数据库能... HBase与Cassender的比较 8 7:几个关键概念: 9 行键(RowKey) 9 列族(ColumnFamily) 9 时间戳(TimeStamp) 9 单元格(Cell) 9 区域(Reg
根据个人的经验,总结出来的HBASE基本概念以及使用场景,原本用于内部分享
python3 使用 thrift 操作hbase 安装hbase-thirft后有一个Hbase报错 使用这个修改完成的Hbase类替换掉原来的Hbase类问题全部解决 主要是因为python版本兼容性带来的问题
│ Day1502_Hbase中的特殊概念.mp4 │ Day1503_Hbase与MYSQL的存储比较.mp4 │ Day1504_Hbase部署环境准备.mp4 │ Day1505_Hbase伪分布式配置文件的修改.mp4 │ Day1506_Hbase伪分布式的启动及hbase命令的使用.mp4 ...
3. 如果设计一个笔记的表,表中要求有笔记的属性和笔记的内容,怎么做 4. HBase部署时如何指定多个zookeeper 5. HBase shell是基于哪种JVM运行的语言实现的 6. HBase shell如何写过滤条件 7. 如何删除HBase中的表 8....
多线程 hbase
HBase(hbase-2.4.9-bin.tar.gz)是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System...
(1)理解HBase在Hadoop体系结构中的角色。(2)熟练使用HBase操作常用的 Shell命令。(3)熟悉HBase操作常用的 Java API。 A.3.2 实验平台 (1)操作系统:Linux。 (2)Hadoop 版本:2.7.1或以上版本。(3)HBase版本:...
搭建pinpoint需要的hbase初始化脚本hbase-create.hbase
HBase 多租户使用
讲师:陈杨——快手大数据高级研发工程师 ...内容概要:(1)讲解hbase coprocessor的原理以及使用场景,(2) coprocessor整个流程实战,包括开发,加载,运行以及管理(3)结合1,2分析coprocessor在rsgroup中的具体使用
本资源为hbase的安装和使用,内含hbase安装工具包,hbase的安装说明,hbase的使用说明 。
hbase各种例子新增修改删除批量导入: public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); ...
删除Hbase中某个表的一列值 命令 java -jar deleteOneColumn.jar(这个文件的路径) '表名' '列簇名' '列名'
对HBase的API做了一层抽象,统一了HBase1.x和HBase2.x的实现,并提供了读写HBase的ORM的支持,同时,sdk还对HBase thrift 的客户端API进行了池化封装,(类似JedisPool),消除了直接使用原生API的各种问题,使之...
HBase入门与使用,可以了解如何使用HBase