您的位置:首页 > 编程语言 > Java开发

HBase的JavaAPI使用

2014-05-31 13:03 375 查看
Java Client API Overview

HBase是用Java写的,支持用编程语言来动态操作管理数据库,能用命令行做的都能够用API来做。

主要的使用步骤例如以下:

1.创建一个 Configuration 对象

–从 HDFS 对象中调用 Configuration

–加入 HBase 属性

Configuration conf = HbaseConfiguration.create();

2.创建 HTable

–提供 Configuration 对象

–提供 表名

HTable hTable = new HTable(conf, tableName);

3.运行操作

–如 put, get, scan, delete, etc...

hTable.getTableName();

4.关闭 HTable 实例

–清空缓存

–释放资源

hTable.close();

以下是一个建表的样例:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

public class  ConstructHTable
{
public static void main(String[] args) throws IOException
{
Configuration conf = HBaseConfiguration.create();
HTable htable = new HTable(conf,"table-created_from_api");
System.out.println("Table :"+Bytes.toString(htable.getTableName()));
htable.close();
}
}


以下是插入数据的样例:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import static org.apache.hadoop.hbase.util.Bytes.*;

public class PutExample {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "HBaseSamples");
Put put1 = new Put(toBytes("row1"));
put1.add(toBytes("test"), toBytes("col1"), toBytes("val1"));
put1.add(toBytes("test"), toBytes("col2"), toBytes("val2"));
hTable.put(put1);
hTable.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: