`
aaron-han
  • 浏览: 26886 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HBase Basic API Demo Code

阅读更多
又这么久... 太懒了...  放个很久以前写的HBase基本API的示例程序代码吧,涉及crud操作和几个简单的filter

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

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseAPIDemoCode {

	private static final Configuration config;
	private static final HTablePool tablePool;
	private static HBaseAdmin admin;

	static {
		config = HBaseConfiguration.create();
		tablePool = new HTablePool(config, 10);
		config.set("hbase.zookeeper.quorum", "master");
	}

	public static HBaseAdmin getHBaseAdmin() {
		if (config == null) {
			throw new RuntimeException("config null");
		}
		if (admin == null) {
			try {
				admin = new HBaseAdmin(config);
			} catch (MasterNotRunningException e) {
				e.printStackTrace();
			} catch (ZooKeeperConnectionException e) {
				e.printStackTrace();
			}
		}
		return admin;
	}

	public static HTable getHTable(final String tableName) {
		return (HTable) tablePool.getTable(tableName);
	}

	public static void printResult(final Result result) {
		byte[] family = null;
		System.out.println("RowKey: " + Bytes.toString(result.getRow()));
		for (KeyValue kv : result.raw()) {
			family = kv.getFamily();
			System.out.println("Column: " + Bytes.toString(family) + ":"
					+ Bytes.toString(kv.getQualifier()));
			System.out.println("Value: " + Bytes.toString(kv.getValue()));
		}
	}

	public static void testCreateTable(final String tableName,
			final String[] columnFamily) {
		HBaseAdmin admin = getHBaseAdmin();
		// if exists,delete first.
		try {
			if (admin.tableExists(tableName)) {
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
			}

			HTableDescriptor htd = new HTableDescriptor(tableName);
			for (String column : columnFamily) {
				htd.addFamily(new HColumnDescriptor(column));
			}
			admin.createTable(htd);
			System.out.println(tableName + " created!");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void testDelTable(final String tableName) {
		HBaseAdmin admin = getHBaseAdmin();
		try {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println(tableName + " deleted!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testInsertData(final String tableName) {
		HTable table = getHTable(tableName);
		int rowIdStart = 10001;
		int rowIdEnd = 10010;
		int index = 1;
		// put 10 cookies
		List<Put> putList = new ArrayList<Put>();
		for (; rowIdStart <= rowIdEnd; rowIdStart++) {
			Put put = new Put(Bytes.toBytes(String.valueOf(rowIdStart)));
			put.add(Bytes.toBytes("product"), Bytes.toBytes("proID"),
					Bytes.toBytes(String.valueOf(index)));
			put.add(Bytes.toBytes("product"), Bytes.toBytes("proTime"),
					Bytes.toBytes(String.valueOf(new Date())));
			if (index % 2 == 0) {
				put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
						Bytes.toBytes("coco"));
			} else {
				put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
						Bytes.toBytes("milk"));
			}
			putList.add(put);
			index++;
		}
		try {
			table.put(putList);
			table.close();
			System.out.println("cookie data inserted!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testQueryAll(final String tableName) {
		HTable table = getHTable(tableName);
		try {
			ResultScanner scanner = table.getScanner(new Scan());
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 删除rowKey不存在的记录,HBase会吞掉该错误。
	public static void testDelARow(final String tableName, final String rowKey) {
		HTable table = getHTable(tableName);
		Delete del = new Delete(rowKey.getBytes());
		try {
			table.delete(del);
			table.close();
			System.out.println("RowKey: " + rowKey + " deleted!");
			testQueryAll(tableName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetARow(final String tableName, final String rowKey) {
		HTable table = getHTable(tableName);
		Get get = new Get(rowKey.getBytes());
		try {
			Result result = table.get(get);
			List<KeyValue> resultList = result.list();
			if (resultList == null) {
				System.out.println(rowKey + " null!");
				return;
			}
			printResult(result);
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetRowsByCookieType(final String tableName,
			final String cookieType) {
		HTable table = getHTable(tableName);
		Filter vfilter = new ValueFilter(CompareOp.EQUAL,
				new SubstringComparator(cookieType));
		Scan scan = new Scan();
		scan.setFilter(vfilter);
		try {
			ResultScanner scanner = table.getScanner(scan);
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetRowsBySingleColumn(final String tableName,
			final String family, final String column, final String columnValue) {
		HTable table = getHTable(tableName);
		Filter vfilter = new SingleColumnValueFilter(Bytes.toBytes(family),
				Bytes.toBytes(column), CompareOp.EQUAL,
				Bytes.toBytes(columnValue));
		Scan scan = new Scan();
		scan.setFilter(vfilter);
		try {
			ResultScanner scanner = table.getScanner(scan);
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		final String tableName = "cookie";
		// testCreateTable(tableName, new String[] { "product", "sale" });
		// testInsertData(tableName);
		// testDelTable(tableName);
		// testQueryAll(tableName);
		// testDelARow(tableName, "10010");
		// testGetARow(tableName, "10011");
		// testGetRowsByCookieType(tableName, "milk");
		testGetRowsBySingleColumn(tableName, "product", "proType", "coco");
	}
}


0
0
分享到:
评论

相关推荐

    HBase Java API类介绍

    ### HBase Java API类介绍 #### 一、概述 HBase是一个分布式的、面向列的开源数据库,基于Google的Bigtable论文实现。它适合于非结构化数据存储,并且能够实时处理PB级别的数据。HBase提供了Java API供开发者使用...

    hbase资料api

    HBase数据查询API HBase是一种分布式的、面向列的NoSQL数据库,主要应用于存储大量的半结构化数据。HBase提供了多种查询方式,包括单条查询和批量查询。 单条查询 单条查询是通过rowkey在table中查询某一行的数据...

    HBase 1.2.0 Javadoc API CHM

    自行制作的HBase 1.2.0 Javadoc API CHM版本。内容抽取自官方站点网页

    hbase 1.0 API 修改说明

    hbase 1.0 对api做了较大改动,该文件对此做了详细说明

    hbase java api 访问 增加修改删除(一)

    在本文中,我们将深入探讨如何使用HBase的Java API进行数据的增加、修改和删除操作。HBase是一个基于Google Bigtable设计的开源分布式数据库,它属于Apache Hadoop生态系统的一部分,适用于处理大规模数据存储。通过...

    hbase java api 所需最精简 jar

    "hbase java api 所需最精简 jar"这个标题意味着我们将探讨的是为了在Java环境中最小化依赖,但仍能实现基本HBase操作所需的JAR文件。 首先,我们需要理解HBase Java API的核心组件。HBase的Java客户端API提供了一...

    Hbase Java API

    HBase Java API HBase 是 Hadoop 的数据库,能够对大数据提供随机、实时读写访问。他是开源的,分布式的,多版本的,面向列的,存储模型。HBase 的整体结构主要包括 HBase Master、HRegion 服务器和 HRegion Server...

    nosql实验四-HBaseShell API操作.docx

    HBase Shell API 操作详解 HBase 是一个基于分布式文件系统的 NoSQL 数据库,提供了丰富的 API 来进行数据操作。在本实验中,我们将使用 HBase Shell API 来实现基本的数据操作,包括创建表、查看所有表、插入数据...

    hbase 0.9 api

    "Hbase 0.9 API"指的是HBase在0.94.5版本中的客户端API,这个版本是HBase发展历史上的一个重要里程碑,为开发者提供了丰富的功能来操作HBase数据库。 首先,HBase的API主要分为两种:Java API和Shell命令。Java API...

    HBaseApi.java

    HBase-APi操作demo

    HBase JavaAPI开发

    使用JavaAPI实现HBase的ddl(创建表、删除表、修改表(添加列族等))、dml(添加数据、删除数据)、dql(查询数据(get、scan))等操作 除此之外还包含一些其他操作:命名空间的应用、快照的应用等 对应(《HBase...

    Hbase的api

    hbase的api手册,包含hbase的变成接口和参数说明

    Hadoop+HBase+Java API

    标题 "Hadoop+HBase+Java API" 涉及到三个主要的开源技术:Hadoop、HBase以及Java API,这些都是大数据处理和存储领域的关键组件。以下是对这些技术及其结合使用的详细介绍: **Hadoop** 是一个分布式计算框架,由...

    HBase中文API

    HBase中文API为开发者提供了在中文环境中操作HBase的便利。 1. **入门** - **介绍**: HBase为大数据处理提供了实时读写能力,特别适合于存储海量稀疏数据。 - **快速开始**: 开发者通常需要了解如何创建表、插入...

    Java SpringBoot 连接 Hbase Demo

    Java SpringBoot 连接 Hbase Demo 创建表 插入数据 列族 列 查询:全表、数据过滤 删除数据 删除表 Hbase 集群搭建:https://blog.csdn.net/weixin_42176639/article/details/131796472

    HBase0.90.4_API.chm

    HBase API chm hadoop 分布式 java HBase0.90.4_API.chm

    hbase java api 访问 查询、分页

    在HBase这个分布式列式数据库中,Java API是开发者常用的一种接口来操作HBase,包括创建表、插入数据、查询数据以及实现分页等操作。本文将深入探讨如何使用HBase Java API进行数据访问和分页查询。 首先,我们要...

    hbase的api,后台操作及代码操作

    hbase文档api,用于hbase开发,随意看看就行;hbase文档api,用于hbase开发,随意看看就行;hbase文档api,用于hbase开发,随意看看就行;hbase文档api,用于hbase开发,随意看看就行

    Apache HBase 2.1.5 API.chm

    Apache HBase 2.1.5 API chm格式文档 hbase-apache离线官方文档

    hbase api demo

    在这个“hbase api demo”中,我们将深入探讨如何使用HBase的API进行基本的数据操作,包括创建表、插入数据以及通过关键字检索和特定查询条件获取数据。 首先,让我们了解HBase的基本概念。在HBase中,数据被组织成...

Global site tag (gtag.js) - Google Analytics