一个使用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
HBase api 大数据,HBASE.094.5_API.chm
hbase api chm
HBASE AP
自行制作的HBase 1.2.0 Javadoc API CHM版本。内容抽取自官方站点网页
2019-8____2019-10工作期间的java版hiveApi,hbaseApi和spark版本的hiveApi,hbaseApi
HBase Java API类介绍
HBase-APi操作demo
hbase 学习必背 有了它就可以方便的学习hadoop,hbase了
HbaseAPI.java
赠送jar包:hbase-metrics-api-1.4.3.jar; 赠送原API文档:hbase-metrics-api-1.4.3-javadoc.jar; 赠送源代码:hbase-metrics-api-1.4.3-sources.jar; 赠送Maven依赖信息文件:hbase-metrics-api-1.4.3.pom; ...
hbase的api手册,包含hbase的变成接口和参数说明
2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. 配置文件 2.4. 配置示例 2.5. 重要配置 3. 升级 3.1. 从 0.94.x 升级到 0.96.x 3.2. 从 0.92.x 升级到 0.94.x 3.3. 从 0.90.x...
利用python3连接hbase,亲测无误,这里分享给大家,欢迎大家下载。
HBase API chm hadoop 分布式 java HBase0.90.4_API.chm
java 端开发 hbase 所需jar包
hbase 1.0 对api做了较大改动,该文件对此做了详细说明
包含翻译后的API文档:flink-hbase_2.11-1.10.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.flink:flink-hbase_2.11:1.10.0; 标签:flink、11、apache、hbase_2、jar包、java、API文档、中文版; ...
包含翻译后的API文档:hbase-client-1.1.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.hbase:hbase-client:1.1.2; 标签:apache、client、hbase、jar包、java、API文档、中文版; 使用方法:解压...
Hbase笔记 —— 利用JavaAPI的方式操作Hbase数据库(往hbase的表中批量插入数据)