博客
关于我
java操作hbase的增删改查
阅读量:116 次
发布时间:2019-02-26

本文共 8082 字,大约阅读时间需要 26 分钟。

HBase 基础操作指南

一、配置HBase环境

在使用HBase之前,需要将相关的JAR包添加到项目类路径,并配置HBase的基本参数。以下是常用的配置方式:

  • 配置文件

    使用HBase自带的配置工具或手动编辑配置文件。推荐使用以下方式创建配置文件:

    Configuration config = HBaseConfiguration.create();config.set("hbase.rootdir", "hdfs://server:9000/hbase");config.set("hbase.zookeeper.quorum", "server:2181");
  • 创建连接对象

    使用ConnectionFactory工厂方法创建HBase连接:

    try {    connection = ConnectionFactory.createConnection(config);} catch (IOException e) {    e.printStackTrace();}
  • 二、基本操作说明

    1. 创建表

    通过Admin类操作表的创建、删除等功能。以下是创建表的示例:

    public static boolean createTable(String tableName, String columnFamily) {    boolean success = false;    Admin admin = null;    TableName tn = TableName.valueOf(tableName);    try {        admin = connection.getAdmin();        if (!admin.tableExists(tn)) {            HTableDescriptor desc = new HTableDescriptor(tn);            desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));            admin.createTable(desc);            System.out.println("表 [" + tableName + "] 创建成功");            success = true;        } else {            System.out.println("表 [" + tableName + "] 已存在");            success = false;        }    } catch (Exception e) {        success = false;    }    return success;}

    2. 插入数据

    使用Put类操作数据插入。以下是插入数据的示例:

    public static boolean putData(String tableName, String rowKey, String columnFamily, String qualifier, Object value) {    boolean success = false;    Admin admin = null;    TableName table = TableName.valueOf(tableName);    try {        admin = connection.getAdmin();        if (admin.tableExists(table)) {            Table t = connection.getTable(table);            Put put = new Put(rowKey.getBytes());            put.addColumn(columnFamily.getBytes(), qualifier.getBytes(), value.toString().getBytes());            t.put(put);            success = true;        } else {            System.out.println("表 [" + tableName + "] 不存在");            success = false;        }    } catch (Exception e) {        success = false;    }    return success;}

    3. 查询数据

    使用Table和ResultScanner类查询数据。以下是查询数据的示例:

    @SuppressWarnings("deprecation")public static void getValueByTable(String tableName) {    TableName tn = TableName.valueOf(tableName.getBytes());    try {        Table table = connection.getTable(tn);        ResultScanner rs = table.getScanner(new Scan());        for (Result r : rs) {            System.out.println("行:" + new String(r.getRow()));            for (KeyValue kv : r.raw()) {                System.out.println("列族:" + new String(kv.getFamilyArray())                        + " 列名:" + new String(kv.getQualifierArray())                        + " 值:" + new String(kv.getValueArray()));            }        }    } catch (Exception e) {        e.printStackTrace();    }}

    4. 删除表

    通过Admin类删除表。以下是删除表的示例:

    public static boolean dropTable(String tableName) {    boolean success = false;    Admin admin = null;    try {        admin = connection.getAdmin();        TableName table = TableName.valueOf(tableName);        if (admin.tableExists(table)) {            admin.disableTable(table);            admin.deleteTable(table);            System.out.println("表 [" + tableName + "] 删除成功");            success = true;        }    } catch (Exception e) {        success = false;    } finally {        IOUtils.closeQuietly(admin);    }    return success;}

    三、代码示例

    以下是完整的Java代码示例:

    package com.xxx.hbase.test;import java.io.IOException;import org.apache.commons.io.IOUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;public class HBaseTest {    private static Connection connection;    static {        Configuration config = HBaseConfiguration.create();        config.set("hbase.rootdir", "hdfs://server:9000/hbase");        config.set("hbase.zookeeper.quorum", "server:2181");        try {            connection = ConnectionFactory.createConnection(config);        } catch (IOException e) {            e.printStackTrace();        }    }    public static boolean createTable(String tableName, String columnFamily) {        boolean success = false;        Admin admin = null;        TableName tn = TableName.valueOf(tableName);        try {            admin = connection.getAdmin();            if (!admin.tableExists(tn)) {                HTableDescriptor desc = new HTableDescriptor(tn);                desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));                admin.createTable(desc);                System.out.println("表 [" + tableName + "] 创建成功");                success = true;            } else {                System.out.println("表 [" + tableName + "] 已存在");                success = false;            }        } catch (Exception e) {            success = false;        }        return success;    }    public static boolean dropTable(String tableName) {        boolean success = false;        Admin admin = null;        try {            admin = connection.getAdmin();            TableName table = TableName.valueOf(tableName);            if (admin.tableExists(table)) {                admin.disableTable(table);                admin.deleteTable(table);                System.out.println("表 [" + tableName + "] 删除成功");                success = true;            }        } catch (Exception e) {            success = false;        } finally {            IOUtils.closeQuietly(admin);        }        return success;    }    public static boolean putData(String tableName, String rowKey, String columnFamily, String qualifier, Object value) {        boolean success = false;        Admin admin = null;        TableName table = TableName.valueOf(tableName);        try {            admin = connection.getAdmin();            if (admin.tableExists(table)) {                Table t = connection.getTable(table);                Put put = new Put(rowKey.getBytes());                put.addColumn(columnFamily.getBytes(), qualifier.getBytes(), value.toString().getBytes());                t.put(put);                success = true;            } else {                System.out.println("表 [" + tableName + "] 不存在");                success = false;            }        } catch (Exception e) {            success = false;        }        return success;    }    @SuppressWarnings("deprecation")    public static void getValueByTable(String tableName) {        TableName tn = TableName.valueOf(tableName.getBytes());        try {            Table table = connection.getTable(tn);            ResultScanner rs = table.getScanner(new Scan());            for (Result r : rs) {                System.out.println("行:" + new String(r.getRow()));                for (KeyValue kv : r.raw()) {                    System.out.println("列族:" + new String(kv.getFamilyArray())                            + " 列名:" + new String(kv.getQualifierArray())                            + " 值:" + new String(kv.getValueArray()));                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {        // 创建表        createTable("stu", "info");        // 插入数据        putData("stu", "1", "info", "id", 1);        putData("stu", "1", "info", "name", "xxx");        // 查询数据        getValueByTable("stu");        // 删除表        dropTable("stu");    }}

    四、构建脚本

    以下是用于构建项目的Ant构建脚本:

    清理完毕

    转载地址:http://wehy.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
    查看>>
    Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
    查看>>
    Objective-C实现binomial coefficient二项式系数算法(附完整源码)
    查看>>
    Objective-C实现BitMap算法(附完整源码)
    查看>>
    Objective-C实现bitonic sort双调排序算法(附完整源码)
    查看>>
    Objective-C实现bogo sort排序算法(附完整源码)
    查看>>
    Objective-C实现BP误差逆传播算法(附完整源码)
    查看>>
    Objective-C实现breadth First Search广度优先搜索算法(附完整源码))
    查看>>
    Objective-C实现Burke 抖动算法(附完整源码)
    查看>>
    Objective-C实现CaesarsCiphe凯撒密码算法(附完整源码)
    查看>>
    Objective-C实现cartesianProduct笛卡尔乘积算法(附完整源码)
    查看>>
    Objective-C实现check strong password检查密码强度算法(附完整源码)
    查看>>
    Objective-C实现circle sort圆形排序算法(附完整源码)
    查看>>
    Objective-C实现cocktail shaker sort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现cocktailShakerSort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现combine Without Repetitions不重复地结合算法(附完整源码)
    查看>>
    Objective-C实现coulombs law库仑定律算法(附完整源码)
    查看>>
    Objective-C实现data transformations数据转换算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>