本文列举一些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基本操作 增删改查 java代码 要使用须导入对应的jar包
java 利用 sping-data-hadoop HbaseTemplate 操作hbase find get execute 等方法 可以直接运行
Hbase 基本操作类 static { //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 /* * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", ...
htabse 命令的基本操作步骤,1.熟悉使用HBase操作常用的Shell命令。 2.学会表和族的属性操作。 3.学会Filter操作。 4.学会时间戳和数据版本的操作。 学会数据批量导入。
A.3实验三:熟悉常用的HBase操作 本实验对应第5章的内容。 A.3.1 实验目的 (1)理解HBase在Hadoop体系结构中的角色。(2)熟练使用HBase操作常用的 Shell命令。(3)熟悉HBase操作常用的 Java API。 A.3.2 实验平台 (1...
hadoop 集群环境下 hbase 的shell 基本操作命令
HBase基本数据操作详解,分享给大家!
来自一位老学姐的Hbase安装详细教程(清华大学镜像下)及基本操作,希望能带给你们些许帮助。主要包含Hbase的下载过程及其遇到的小问题,后续会继续完善该文档!
上机实操,熟悉指令操作Hbase和java代码操作Hbase 二、实验环境 Windows 10 VMware Workstation Pro虚拟机 Hadoop环境 Jdk1.8 三、实验内容 1:指令操作Hbase (1):start-all.sh,启动所有进程 (2):start-hbase.sh...
│ Day15[Hbase 基本使用及存储设计].pdf │ ├─02_视频 │ Day1501_Hbase的介绍及其发展.mp4 │ Day1502_Hbase中的特殊概念.mp4 │ Day1503_Hbase与MYSQL的存储比较.mp4 │ Day1504_Hbase部署环境准备.mp4 │ Day...
Hbase shell 、Hbase api、Hbase 配置
HBase 基本原理,出版于 2014,HBase is a NoSQL database that primarily works on top of Hadoop. HBase is based on the storage architecture followed by the BigTable. HBase inherits the storage design ...
hbase shell操作指南,提供hbase shell操作指导,其中包含增删改查
HBase基本操作(实验)2018年7月31日-(肖光鼎)
这里详细介绍了hbase的命令行操作,版本信息的一些管理
HBASE具体操作指令
该文档是介绍HBase表的基本操作,贡献出来,给大家下载!
NULL 博文链接:https://username2.iteye.com/blog/2106533
hbase 的基本操作.mp4
hbase表操作工具类.连接hbase数据库,根据rowkey删除单条记录,批量删除记录。获取某个字段的值,判断记录是否存在,向hbase表中插单条数据,批量插入到hbase表。 查询表,获取某个字段的值列表,hbase工具