HBASE数据库操作

本文详细介绍了一个使用Java进行HBase操作的示例,包括创建表、插入数据、获取数据、批量插入以及扫描数据等核心功能。通过具体代码展示了如何与HBase进行交互,适用于初学者理解和实践HBase的基本操作。

 

package com.neudu.hbase;

import java.io.IOException;

import java.io.InterruptedIOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellUtil;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import org.apache.hadoop.hbase.filter.FilterList;

import org.apache.hadoop.hbase.filter.PrefixFilter;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class HbaseDemo {

     HBaseAdmin admin;

     HTable htable;

     String TN="phone";

     

     @Before

     public void setUp() throws Exception{

         Configuration conf = new Configuration();

         conf.set("hbase.zookeeper.quorum", "node02,node03,node04");

         admin=new HBaseAdmin(conf);

         htable = new HTable(conf, TN);

     }

     

     @After

     public void end() throws Exception{

         if(admin!=null){

              admin.close();

         }

         if(htable!=null){

              htable.close();

         }

     }

     

     @Test

     public void createTable() throws Exception{

         if(admin.tableExists(TN)){

              admin.disableTable(TN);

              admin.deleteTable(TN);

         }

         HTableDescriptor desc= new HTableDescriptor(TableName.valueOf(TN));

         HColumnDescriptor cf = new HColumnDescriptor("cf");

         cf.setInMemory(true);

         desc.addFamily(cf);

         admin.createTable(desc);

     }

     

     @Test

     public void insertDbl() throws Exception{

         String rowkey = "123456";

         Put put = new Put(rowkey.getBytes());

         put.add("cf".getBytes(),"name".getBytes(),"xiaoming".getBytes());

         put.add("cf".getBytes(),"age".getBytes(),"1".getBytes());

         put.add("cf".getBytes(),"sex".getBytes(),"xx".getBytes());

         htable.put(put);

     }

     

     @Test

     public void getDbl() throws Exception{

         String rowkey="123456";

         Get get = new Get(rowkey.getBytes());

         get.addColumn("cf".getBytes(), "name".getBytes());

         get.addColumn("cf".getBytes(), "age".getBytes());

         get.addColumn("cf".getBytes(), "sex".getBytes());

         Result rs = htable.get(get);

         Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());

         Cell cell2 = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());

         Cell cell3 = rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());

         byte[] bs = CellUtil.cloneValue(cell);

         byte[] bs2 = CellUtil.cloneValue(cell2);

         byte[] bs3 = CellUtil.cloneQualifier(cell3);

         System.out.println(new String(bs)+" "+new String(bs2) + " "+new String(bs3));

     }

     

     Random r = new Random();

     

     public String getPhoneNum(String prefix){

         return prefix + String.format("%08d",r.nextInt(99999999));

     }

     

     public String getDate(String year){

         return year+String.format("%02d%02d%02d%02d%02d", new Object[]{r.nextInt(12)+1,r.nextInt(28)+1,r.nextInt(24),r.nextInt(60),r.nextInt(60)});  

     }

     public String getDate2(String prefix){

         return prefix+String.format("%02d%02d%02d%02d%02d", new Object[]{r.nextInt(24),r.nextInt(60),r.nextInt(60)});

     }

     

     @Test

     public void insertdb2() throws Exception{

         List<Put> puts = new ArrayList<>();

         for(int i=0;i<10;i++){

              String phoneNum = getPhoneNum("186");

              for(int j=0;j<100;j++){

                  String dNum=getPhoneNum("177");

                  String length=r.nextInt(99)+"";

                  String type = r.nextInt(2)+"";

                  String datestr = getDate("2017");

                  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

                  String rowkey=phoneNum+"_"+(Long.MAX_VALUE-sdf.parse(datestr).getTime());

                  Put put = new Put(rowkey.getBytes());

                  put.add("cf".getBytes(),"dnum".getBytes(),dNum.getBytes());

                  put.add("cf".getBytes(),"length".getBytes(),length.getBytes());

                  put.add("cf".getBytes(),"type".getBytes(),type.getBytes());

                  put.add("cf".getBytes(),"datestr".getBytes(),datestr.getBytes());

                  puts.add(put);

              }

         }

         

         htable.put(puts);

     }

     

     @Test

     public void scanDb1() throws Exception{

         Scan scan =new Scan();

         String pNum = "18658655342";

         

         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

         String startRow = pNum+"_"+(Long.MAX_VALUE-sdf.parse("20170301000000").getTime());

         String stopRow = pNum+"_"+(Long.MAX_VALUE-sdf.parse("20170201000000").getTime());

         scan.setStartRow(startRow.getBytes());

         scan.setStopRow(stopRow.getBytes());

         ResultScanner rss = htable.getScanner(scan);

         for (Result rs : rss) {

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "datestr".getBytes()))));

         }

     }

     

     @Test

     public void scanDb2() throws Exception{

         String pNum="18658655342";

         FilterList list=new FilterList(FilterList.Operator.MUST_PASS_ALL);

         PrefixFilter filter1 = new PrefixFilter(pNum.getBytes());

         list.addFilter(filter1);

         

         SingleColumnValueFilter filter2 = new SingleColumnValueFilter("cf".getBytes(), "type".getBytes(), CompareOp.EQUAL,"1".getBytes());

         list.addFilter(filter2);

         

         Scan scan = new Scan();

         scan.setFilter(list);

         ResultScanner rss = htable.getScanner(scan);

         for (Result rs : rss) {

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "datestr".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));

              System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".getBytes()))));

         }

     }

}

 

HBase是一个开源的非关系型分布式数据库(NoSQL),它基于Google的BigTable模型,运行在Hadoop文件系统(HDFS)之上,适用于存储大量的稀疏数据集。HBase提供对数据的高可靠性和高性能读写访问,特别是适合那些需要快速读写大量动态变化数据的应用场景。 HBase的核心概念包括表(Table)、行(Row)、列族(Column Family)和时间戳(Timestamp)。在HBase中,数据是按行存储的,每行都有一个唯一的行键(Row Key)来标识。列族是表中列的集合,它定义了列数据的存储和访问方式。时间戳是HBase数据版本的标识,每个单元格(Cell)都有一个时间戳,表示数据的版本。 HBase提供了丰富的API,包括Java API、REST API、Thrift API和Avro API等,其中Java API是最常用的方式。使用Java API进行HBase数据库操作通常包括以下几个步骤: 1. 配置HBase环境,包括设置HBase的配置文件(hbase-site.xml)和Hadoop的配置文件(core-site.xml和hdfs-site.xml)。 2. 使用HBase配置对象,通常是`Configuration`类的实例,来创建`Connection`对象,它是与HBase集群通信的入口。 3. 通过`Connection`对象获取`Admin`对象,用于执行管理操作,如创建表、删除表、列出表等。 4. 通过`Connection`对象获取`Table`对象,代表一个HBase表。使用`Table`对象可以执行CRUD(创建、读取、更新、删除)操作。 5. 执行具体的数据操作,如`put`方法插入数据,`get`方法检索数据,`scan`方法扫描数据等。 6. 完成操作后,应当关闭连接,释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值