zoukankan      html  css  js  c++  java
  • HBase的JavaAPI使用

    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();
    	}
    }


  • 相关阅读:
    HDU 3537 Daizhenyang's Coin(博弈,翻硬币)
    【转】博弈-翻硬币游戏
    QRCode.js:使用 JavaScript 生成二维码
    3种高效的Tags标签系统数据库设计方案分享
    CI框架+Umeditor上传图片配置信息
    【军哥谈CI框架】之CI中集成百度UEditor
    【ci框架基础】之部署百度编辑器
    CI框架中集成CKEditor编辑器的教程
    如何将文本编辑器嵌入框架--以Umeditor&CodeIgniter框架为例
    ****CI和UEditor集成
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/3762288.html
Copyright © 2011-2022 走看看