hBase官方文档以及HBase基础操作封装类

HBase 官方文档 0.97 http://abloz.com/hbase/book.html

HBase基本操作

package cn.crxy.spider.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseUtils {

    /**
     * HBASE 表名称
     */
    public static final String TABLE_NAME = "spider";
    /**
     * 列簇1 商品信息
     */
    public static final String COLUMNFAMILY_1 = "goodsinfo";
    /**
     * 列簇1中的列
     */
    public static final String COLUMNFAMILY_1_DATA_URL = "data_url";
    public static final String COLUMNFAMILY_1_PIC_URL = "pic_url";
    public static final String COLUMNFAMILY_1_TITLE = "title";
    public static final String COLUMNFAMILY_1_PRICE = "price";
    /**
     * 列簇2 商品规格
     */
    public static final String COLUMNFAMILY_2 = "spec";
    public static final String COLUMNFAMILY_2_PARAM = "param";

    HBaseAdmin admin = null;
    Configuration conf = null;

    /**
     * 构造函数加载配置
     */
    public HbaseUtils() {
        conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.1.177:2181");
        conf.set("hbase.rootdir", "hdfs://192.168.1.177:9000/hbase");
        try {
            admin = new HBaseAdmin(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        HbaseUtils hbase = new HbaseUtils();
        // 创建一张表
        // hbase.createTable("stu","cf");
        // //查询所有表名
        // hbase.getALLTable();
        // //往表中添加一条记录
        // hbase.addOneRecord("stu","key1","cf","name","zhangsan");
        // hbase.addOneRecord("stu","key1","cf","age","24");
        // //查询一条记录
        // hbase.getKey("stu","key1");
        // //获取表的所有数据
        // hbase.getALLData("stu");
        // //删除一条记录
        // hbase.deleteOneRecord("stu","key1");
        // //删除表
        // hbase.deleteTable("stu");
        // scan过滤器的使用
        // hbase.getScanData("stu","cf","age");
        // rowFilter的使用
        // 84138413_20130313145955
    }

    /**
     * rowFilter的使用
     *
     * @param tableName
     * @param reg
     * @throws Exception
     */
    public void getRowFilter(String tableName, String reg) throws Exception {
        HTable hTable = new HTable(conf, tableName);
        Scan scan = new Scan();
        // Filter
        RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL,
                new RegexStringComparator(reg));
        scan.setFilter(rowFilter);
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(new String(result.getRow()));
        }
    }

    public void getScanData(String tableName, String family, String qualifier)
            throws Exception {
        HTable hTable = new HTable(conf, tableName);
        Scan scan = new Scan();
        scan.addColumn(family.getBytes(), qualifier.getBytes());
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            if (result.raw().length == 0) {
                System.out.println(tableName + " 表数据为空!");
            } else {
                for (KeyValue kv : result.raw()) {
                    System.out.println(new String(kv.getKey()) + "\t"
                            + new String(kv.getValue()));
                }
            }
        }
    }

    private void deleteTable(String tableName) {
        try {
            if (admin.tableExists(tableName)) {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
                System.out.println(tableName + "表删除成功!");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(tableName + "表删除失败!");
        }

    }

    /**
     * 删除一条记录
     *
     * @param tableName
     * @param rowKey
     */
    public void deleteOneRecord(String tableName, String rowKey) {
        HTablePool hTablePool = new HTablePool(conf, 1000);
        HTableInterface table = hTablePool.getTable(tableName);
        Delete delete = new Delete(rowKey.getBytes());
        try {
            table.delete(delete);
            System.out.println(rowKey + "记录删除成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(rowKey + "记录删除失败!");
        }
    }

    /**
     * 获取表的所有数据
     *
     * @param tableName
     */
    public void getALLData(String tableName) {
        try {
            HTable hTable = new HTable(conf, tableName);
            Scan scan = new Scan();
            ResultScanner scanner = hTable.getScanner(scan);
            for (Result result : scanner) {
                if (result.raw().length == 0) {
                    System.out.println(tableName + " 表数据为空!");
                } else {
                    for (KeyValue kv : result.raw()) {
                        System.out.println(new String(kv.getKey()) + "\t"
                                + new String(kv.getValue()));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 读取一条记录
    /*
     * @SuppressWarnings({ "deprecation", "resource" }) public Article
     * get(String tableName, String row) { HTablePool hTablePool = new
     * HTablePool(conf, 1000); HTableInterface table =
     * hTablePool.getTable(tableName); Get get = new Get(row.getBytes());
     * Article article = null; try {
     *
     * Result result = table.get(get); KeyValue[] raw = result.raw(); if
     * (raw.length == 4) { article = new Article(); article.setId(row);
     * article.setTitle(new String(raw[3].getValue())); article.setAuthor(new
     * String(raw[0].getValue())); article.setContent(new
     * String(raw[1].getValue())); article.setDescribe(new
     * String(raw[2].getValue())); } } catch (IOException e) {
     * e.printStackTrace(); } return article; }
     */

    // 添加一条记录
    public void put(String tableName, String row, String columnFamily,
            String column, String data) throws IOException {
        HTablePool hTablePool = new HTablePool(conf, 1000);
        HTableInterface table = hTablePool.getTable(tableName);
        Put p1 = new Put(Bytes.toBytes(row));
        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
                Bytes.toBytes(data));
        table.put(p1);
        System.out.println("put‘" + row + "‘," + columnFamily + ":" + column
                + "‘,‘" + data + "‘");
    }

    /**
     * 查询所有表名
     *
     * @return
     * @throws Exception
     */
    public List<String> getALLTable() throws Exception {
        ArrayList<String> tables = new ArrayList<String>();
        if (admin != null) {
            HTableDescriptor[] listTables = admin.listTables();
            if (listTables.length > 0) {
                for (HTableDescriptor tableDesc : listTables) {
                    tables.add(tableDesc.getNameAsString());
                    System.out.println(tableDesc.getNameAsString());
                }
            }
        }
        return tables;
    }

    /**
     * 创建一张表
     *
     * @param tableName
     * @param column
     * @throws Exception
     */
    public void createTable(String tableName, String column) throws Exception {
        if (admin.tableExists(tableName)) {
            System.out.println(tableName + "表已经存在!");
        } else {
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);
            tableDesc.addFamily(new HColumnDescriptor(column.getBytes()));
            admin.createTable(tableDesc);
            System.out.println(tableName + "表创建成功!");
        }
    }
}
时间: 2024-10-23 12:24:15

hBase官方文档以及HBase基础操作封装类的相关文章

hbase官方文档(转)

Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Software Foundation.保留所有权利. Apache Hadoop, Hadoop, MapReduce, HDFS, Zookeeper, HBase 及 HBase项目 logo 是Apache Software Foundation的商标. Revision History Revision 0.95-SNAPSHOT 2012-12-03T13:38 中文版

HBase 官方文档0.90.4

HBase 官方文档0.90.4 Copyright ? 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision 0.90.4 配置,数据模型使用入门 Abstract 这是 Apache HBase的官方文档, Hbase是一个分布式,版本化(versioned),构建在 Apache Hadoop和 Apache ZooKeeper上的列数据库. 我(译者)熟悉Hbase的源代码,从事Hbase

常用SQL_官方文档使用

SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于select关键字完成,查询语句是SQL语句中最复杂,功能最丰富的语句. ②DML(Data Munipulation Language,数据操作语言)语句,DML语句修改后数据将保持较好的一致性:操作表的语句,如增插insert.更新update.删除delete等: ③DDL(Data Defini

Android 触摸手势基础 官方文档概览2

Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: MotionEvent 兼容版的: MotionEventCompat  (Note that MotionEventCompat is not a replacement for the MotionEvent class. Rather, it provides static utility metho

ios开发UI基础—安装苹果官方文档和Xcode模拟器

ios开发UI基础-安装苹果官方文档和Xcode模拟器 提示:准备资料 (1)com.apple.adc.documentation.AppleiOS7.1.iOSLibrary.docset.zip (2)iPhoneSimulator6.1.sdk.zip 一.安装苹果官方文档 1.安装路径 文档有两个安装路径: (1)~/资源库/Developer/Xcode/UserData/ (2)/Applications/Xcode.app/Contents/Developer/Documenta

EasyTouch的使用官方文档操作步骤

对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很好,而最新版的已经解决了这一问题.当然unity也自带了摇杆Joystick,用起来也简单,但存在不少局限,不会满足普通mmo游戏的需求,比如指定显示区域或者是更改一些素材等等,而这些EasyTouch插件都已经帮你实现,不得不佩服插件的原作者,能做出这么炫酷好用的插件,当然这估计是老外开发的,关于

Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇)

目录 Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 1 Internal Locking Methods 2 Metadata Locking 3 External Locking Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 1 Internal Locking Methods 这里介绍Mysql的几种锁,该锁由Mysql自行进行管理,用户不需要处理该锁. Row-Level Locking 对于InnoDB,行锁可以通过SELECT ... FOR UPDAT

苹果官方文档阅读的入门

1.下载和打开官方文档的步骤 (1).在code->preferences->components(目录下的document栏目)可以下载相关的官方文档 (2).在help->documentation and API references中可以打开官方文档 2.下面介绍的是官方网站上的文档结构,相关链接:https://developer.apple.com/library/ios/navigation/#section=Resource%20Types&topic=Guide

Android Google官方文档解析之——Device Compatibility

Android is designed to run on many different types of devices, from phones to tablets and televisions. As a developer, the range of devices provides a huge potential audience for your app. In order for your app to be successful on all these devices,