- 浏览: 525453 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
飞天奔月:
public List<String> gener ...
实践中的重构30_不做油漆匠 -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道public class A {
...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道在世界的中心呼喚愛 写道在classB ...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道在classB的finalize上打断 ...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
iteye比较少上,如果可以的话,可以发e-mail交流:ch ...
深入理解ReferenceQueue GC finalize Reference
本文列举一些hbase的基本操作代码。
package allen.studyhbase; import java.io.IOException; import java.util.LinkedList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HBaseAdmin; 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.util.Bytes; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; public class BaseTest { protected static Log log = LogFactory.getLog(BaseTest.class); protected static HBaseAdmin hbaseAdmin; protected static HTablePool pool; protected static String TableName = "allen_test"; protected static byte[] TableNameBytes = Bytes.toBytes(TableName); protected static byte[] ColumnFamilyName = Bytes.toBytes("cf"); protected static String QNameStr1 = "q1"; protected static String QNameStr2 = "q2"; protected static String QNameStr3 = "q3"; protected static byte[] QName1 = Bytes.toBytes(QNameStr1); protected static byte[] QName2 = Bytes.toBytes(QNameStr2); protected static byte[] QName3 = Bytes.toBytes(QNameStr3); protected HTableInterface table; @BeforeClass public static void beforeClass() throws Exception { hbaseAdmin = CommonConfig.getHBaseAdmin(); deleteTable(); createTable(); } @AfterClass public static void afterClass() throws Exception { deleteTable(); } @Before public void before() throws Throwable { pool = CommonConfig.getHTablePool(); table = pool.getTable(TableName); fillData(); } @After public void after() throws Exception { try { // full scan. Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); List<byte[]> rows = new LinkedList<byte[]>(); for (Result result = resultScanner.next(); result != null; result = resultScanner .next()) { rows.add(result.getRow()); } for (byte[] row : rows) { table.delete(new Delete(row)); log.info("delete " + Bytes.toString(row)); } // return table to pool. table.close(); } catch (IOException e) { throw new RuntimeException(e); } } private static void createTable() throws Exception { // create new table. HTableDescriptor tableDescriptor = new HTableDescriptor(TableName); tableDescriptor.addFamily(new HColumnDescriptor(ColumnFamilyName)); hbaseAdmin.createTable(tableDescriptor); } private static void deleteTable() throws Exception { // delete table if table exist. if (hbaseAdmin.tableExists(TableName)) { // disable table before delete it. hbaseAdmin.disableTable(TableName); hbaseAdmin.deleteTable(TableName); } } String rowKeyStr1 = "allen_test_row1"; String rowKeyStr2 = "allen_test_row2"; String rowKeyStr3 = "allen_test_row3"; String rowKeyStr4 = "allen_test_row4"; byte[] rowKey1 = Bytes.toBytes(rowKeyStr1); byte[] rowKey2 = Bytes.toBytes(rowKeyStr2); byte[] rowKey3 = Bytes.toBytes(rowKeyStr3); byte[] rowKey4 = Bytes.toBytes(rowKeyStr4); private void fillData() throws Throwable { Put put = new Put(rowKey1); put.add(ColumnFamilyName, QName1, Bytes.toBytes(100L)); put.add(ColumnFamilyName, QName2, Bytes.toBytes(100L)); table.put(put); put = new Put(rowKey2); put.add(ColumnFamilyName, QName1, Bytes.toBytes(20L)); put.add(ColumnFamilyName, QName2, Bytes.toBytes(200L)); table.put(put); // set null case. put = new Put(rowKey3); put.add(ColumnFamilyName, QName1, null); put.add(ColumnFamilyName, QName2, null); table.put(put); // empty case. put = new Put(rowKey4); put.add(ColumnFamilyName, QName3, Bytes.toBytes("test")); table.put(put); } }
package allen.studyhbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTablePool; /** * CommonConfig. * */ public class CommonConfig { private static HTablePool pool; private static HBaseAdmin hbaseAdmin; private static Configuration conf; static { try { conf = HBaseConfiguration.create(); pool = new HTablePool(conf, 50); hbaseAdmin = new HBaseAdmin(conf); } catch (Exception e) { throw new RuntimeException(e); } }; public static Configuration getConfiguration() { return conf; } public static HBaseAdmin getHBaseAdmin() { return hbaseAdmin; } public static HTablePool getHTablePool() { return pool; } }
package allen.studyhbase; import java.util.HashSet; import java.util.Set; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.util.Bytes; import org.junit.Assert; import org.junit.Test; public class TestHbaseOp extends BaseTest { @Test public void testDelete() throws Exception { Get get = new Get(rowKey1); Result result = table.get(get); Assert.assertTrue(!result.isEmpty()); Delete delete = new Delete(rowKey1); table.delete(delete); get = new Get(rowKey1); result = table.get(get); Assert.assertTrue(result.isEmpty()); } @Test public void testDeleteNotExistRow() throws Exception { byte[] rowKey = Bytes.toBytes("allen_test_row"); Delete delete = new Delete(rowKey); table.delete(delete); } @Test public void testScan_01() throws Exception { Set<String> resultRowKeys = new HashSet<String>(); Scan scan = new Scan(rowKey1, rowKey2); ResultScanner resultScanner = table.getScanner(scan); for (Result result = resultScanner.next(); result != null; result = resultScanner .next()) { resultRowKeys.add(Bytes.toString(result.getRow())); } Assert.assertTrue(resultRowKeys.size() == 1); Assert.assertTrue(resultRowKeys.contains(rowKeyStr1)); } @Test public void testScan_02() throws Exception { Set<String> resultRowKeys = new HashSet<String>(); Scan scan = new Scan(rowKey1); ResultScanner resultScanner = table.getScanner(scan); for (Result result = resultScanner.next(); result != null; result = resultScanner .next()) { resultRowKeys.add(Bytes.toString(result.getRow())); } Assert.assertTrue(resultRowKeys.size() == 4); Assert.assertTrue(resultRowKeys.contains(rowKeyStr1)); Assert.assertTrue(resultRowKeys.contains(rowKeyStr2)); Assert.assertTrue(resultRowKeys.contains(rowKeyStr3)); Assert.assertTrue(resultRowKeys.contains(rowKeyStr4)); } @Test public void testScan_03() throws Exception { Set<String> resultRowKeys = new HashSet<String>(); Scan scan = new Scan(rowKey1); scan.addColumn(ColumnFamilyName, QName1); ResultScanner resultScanner = table.getScanner(scan); for (Result result = resultScanner.next(); result != null; result = resultScanner .next()) { resultRowKeys.add(Bytes.toString(result.getRow())); } Assert.assertTrue(resultRowKeys.size() == 3); Assert.assertTrue(resultRowKeys.contains(rowKeyStr1)); Assert.assertTrue(resultRowKeys.contains(rowKeyStr2)); Assert.assertTrue(resultRowKeys.contains(rowKeyStr3)); } @Test public void testCheckAndPut() throws Exception { byte[] rowKey = Bytes.toBytes("allen_test_row"); Put put = new Put(rowKey); put.add(ColumnFamilyName, QName1, Bytes.toBytes("a")); put.add(ColumnFamilyName, QName2, Bytes.toBytes("b")); boolean result = false; result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, Bytes.toBytes("b"), put); // check fail, put fail. Assert.assertFalse(result); result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, null, put); // check ok, put ok. Assert.assertTrue(result); result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, null, put); // check fail, put fail. Assert.assertFalse(result); result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, Bytes.toBytes("b"), put); // check ok, put ok. Assert.assertTrue(result); } @Test public void testPutAndGet() throws Exception { byte[] rowKey = Bytes.toBytes("allen_test_row"); Put put = new Put(rowKey); put.add(ColumnFamilyName, QName1, Bytes.toBytes("a")); put.add(ColumnFamilyName, QName3, null); table.put(put); Get get = new Get(rowKey); Result result = table.get(get); byte[] q1 = result.getValue(ColumnFamilyName, QName1); byte[] q2 = result.getValue(ColumnFamilyName, QName2); byte[] q3 = result.getValue(ColumnFamilyName, QName3); Assert.assertEquals("a", Bytes.toString(q1)); // we get null byte array here. Assert.assertEquals(null, Bytes.toString(q2)); // we get empty byte array here. not a null. Assert.assertEquals("", Bytes.toString(q3)); // get a row doesn't exist. byte[] rowKey2 = Bytes.toBytes("allen_test_row_not_exist"); get = new Get(rowKey2); result = table.get(get); Assert.assertTrue(result.isEmpty()); } @Test public void testPutWithoutColumn() throws Exception { byte[] rowKey = Bytes.toBytes("allen_test_row"); Put put = new Put(rowKey); try { table.put(put); Assert.fail(); } catch (IllegalArgumentException e) { // ignore. } } }
发表评论
-
hbase分页功能的几种实现方案
2015-01-13 23:52 5377hbase分页功能的几种实现方案。 分页功能是线上系统的常用 ... -
simplehbase v0.98.1开始支持hbase0.98
2014-12-29 21:48 1021https://github.com/zhang-xzhi/s ... -
hbase轻量级中间件simplehbase v1.0简介
2014-12-13 18:55 1369https://github.com/zhang-xzhi/s ... -
hbase put UML图
2014-12-11 23:40 1274create Htable put hbase rpc ... -
hbase开发问题-PooledHTable多次close导致问题
2014-12-11 23:27 2001PooledHTable多次close导致问题 Pooled ... -
hbase的CoprocessorProtocol及一个简单的通用扩展实现V2
2014-12-04 18:00 2076hbase中的CoprocessorProtocol机制. ... -
hbase 0.94.0 0.94.9 0.94.24 功能不兼容初步分析
2014-12-04 16:10 1090hbase 0.94.0 0.94.9 0.94.24 功能不 ... -
simplehbase对JOPO新增xml配置和无配置方式
2014-10-24 22:50 973simplehbase介绍文章如下: https://gith ... -
hbase轻量级中间件simplehbase v0.9简介
2014-07-14 13:57 616https://github.com/zhang-xzhi/s ... -
hbase轻量级中间件simplehbase v0.8简介
2014-04-28 21:44 3729https://github.com/zhang-xzhi/s ... -
hbase开发问题-hbase版本号报错
2014-04-22 19:19 2740由于使用了自定义的classloader,导致报错。 p ... -
HBase Client使用注意点
2014-04-21 12:51 2526HBase Client使用注意点: 1 HTable线程 ... -
hbase开发问题-hbase-0.94.0的ServerCallable callTimeout处理有问题
2014-04-14 22:07 2164读hbase-0.94.0的ServerCallable时,发 ... -
Phoenix和simplehbase功能简单比较
2014-04-02 17:20 1604Phoenix和simplehbase功能简单比较 大数据应 ... -
hbase web console simplehbaseviewer
2014-03-12 19:11 1221https://github.com/zhang-xzhi/s ... -
hbase轻量级中间件simplehbase v0.2简介
2013-12-19 23:51 1660https://github.com/zhang-xzhi/s ... -
hbase轻量级中间件simplehbase v0.1简介
2013-10-09 19:29 1531simplehbase尝试简化基于hbase的java应用开发 ... -
hbase的CoprocessorProtocol及一个简单的通用扩展实现V1
2013-08-18 14:15 5272hbase的CoprocessorProtocol及一个简单的 ... -
hadoop_hadoop的一次读取
2013-04-29 13:09 1857一次hadoop的read getFileSystem 代码 ... -
hadoop_hadoop的map reduce
2011-11-09 21:21 1221这个根据功能模块分为 ...
相关推荐
HBase基本操作 增删改查 java代码 要使用须导入对应的jar包
Hbase 基本操作类 static { //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 /* * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", ...
htabse 命令的基本操作步骤,1.熟悉使用HBase操作常用的Shell命令。 2.学会表和族的属性操作。 3.学会Filter操作。 4.学会时间戳和数据版本的操作。 学会数据批量导入。
HBase基本操作(实验)2018年7月31日-(肖光鼎)
HBase基本数据操作详解,分享给大家!
hadoop 集群环境下 hbase 的shell 基本操作命令
hbase资料整理PPT,资料详细。为内部培训课程PPT,请勿转载!
上机实操,熟悉指令操作Hbase和java代码操作Hbase 二、实验环境 Windows 10 VMware Workstation Pro虚拟机 Hadoop环境 Jdk1.8 三、实验内容 1:指令操作Hbase (1):start-all.sh,启动所有进程 (2):start-hbase.sh...
来自一位老学姐的Hbase安装详细教程(清华大学镜像下)及基本操作,希望能带给你们些许帮助。主要包含Hbase的下载过程及其遇到的小问题,后续会继续完善该文档!
│ Day15[Hbase 基本使用及存储设计].pdf │ ├─02_视频 │ Day1501_Hbase的介绍及其发展.mp4 │ Day1502_Hbase中的特殊概念.mp4 │ Day1503_Hbase与MYSQL的存储比较.mp4 │ Day1504_Hbase部署环境准备.mp4 │ Day...
该文档是介绍HBase表的基本操作,贡献出来,给大家下载!
NULL 博文链接:https://username2.iteye.com/blog/2106533
hbase 的基本操作.mp4
1. HBase基本工作原理 2. HBase基本操作与编程方法示例 3. Hive基本工作原理 4. Hive基本操作示例
HBase的基本元素: 4 3) 数据模型有哪些操作? 4 4) 返回结果的排序方式是什么? 5 5)最后,HBase不支持联合查询 5 mapreduce与HBase表配合使用 5 4. HBase的模式Schema设计的一些概念和原则 5 1)模式的创建与...
大数据技术基础实验报告-HBase安装配置和应用实践
hbase的安装和基本操作.mp4
3.1 基本操作 进入HBase客户端命令行 [atguigu@hadoop102 hbase]$ bin/hbase shell 查看帮助命令 hbase(main):001:0> help 查看当前数据库中有哪些表 hbase(main):002:0> list 3.2 表的操作 创建表 hbase(main):...
Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询