5.4 HBase语句

hbase shell 进入HBase
HBase光标是往后删除的,要往前删除需要按着Ctrl
list 查看当前HBase中具有哪些表list
create 'scores','grade', 'course' 创建表,表名scores,列族grade,course
put 'scores','1','course:name','sansan' 向scores表中添加row key为1,列族course当中,列neme=sansan的数据
describe 'scores' 查看scores表的结构
get 'scores','1' 查看row key为1的一条记录
get 'scores','1','course:age' 条件查询
scan 'scores' 查看表的所有记录
disable 'scores' 废弃表
is_disabled 'scores' 查看表是否废弃
drop 'scores' 删除表,删除表之前必须先废弃
delete 'tab01','1','cf1:age' 删除一条记录
deleteall ‘< table name >’, ‘< row >’, 删除一行数据
create,describe,disable,drop,list,scan,put,get,delete,deleteall,cou nt,status等,通过help可以看到详细的用法

整合Java需要HBase的jar和hadoop的jar
package com.sxt.hbase;

import java.io.IOException;
import java.text.ParseException;
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.TableName;
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.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;

public class HBaseDemo {
 
 //通话详单 表名
 public String TN = "tp";

 HBaseAdmin hBaseAdmin;
 HTable htable;
 
 @Before
 public void setup() throws Exception {
  Configuration conf = new Configuration();
  conf.set("hbase.zookeeper.quorum", "node1");//要是使用zookeeper集群:node1,node2,node3
  hBaseAdmin = new HBaseAdmin(conf);
  htable = new HTable(conf, TN);
 }
 
 @After
 public void end() throws Exception {
  if(hBaseAdmin != null) {
   hBaseAdmin.close();
  }
  if(htable != null) {
   htable.close();
  }
 }
 
 @Test
 /**
  * 创建表
  * @throws Exception
  */
 public void creatTbl() throws Exception {
  if(hBaseAdmin.tableExists(TN)) {
   hBaseAdmin.disableTable(TN);
   hBaseAdmin.deleteTable(TN);
  }
 
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
 
  HColumnDescriptor family = new HColumnDescriptor("cf");
  family.setInMemory(true);
  family.setMaxVersions(1);
  desc.addFamily(family);
 
  hBaseAdmin.createTable(desc);
 }
 
 @Test
 /**
  * 插入数据
  */
 public void insertDB() throws Exception {
  //rowkey设计: 自己的手机号码 _ 时间戳
  Put put = new Put("18612341234_15525353434".getBytes());
  put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
  htable.put(put);
 }
 
 @Test
 /**
  * 查询 某些cell
  * @throws Exception
  */
 public void getDB() throws Exception {
  // 参数:rowkey
  Get get = new Get("001".getBytes());
  get.addColumn("cf".getBytes(), "name".getBytes());
 
  Result rs = htable.get(get);
  Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
  System.out.println(new String(CellUtil.cloneValue(cell)));
 }
 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 
 /**
  * 十个手机号码 每个用户一年 产生100条
  * @throws Exception
  * @throws
  */
 @Test
 public void insertDBs() throws Exception {
 
  List<Put> puts = new ArrayList<Put>();
 
  for (int i = 0; i < 10; i++) {
   // 自己手机号码
   String pNum = getPhone("186");
   
   for (int j = 0; j < 100; j++) {
    String dateStr = getDate("2017");
    try {
     Long dataLong = sdf.parse(dateStr).getTime();
     
     // rowkey设计:大数-当前时间戳
     String rowkey = pNum + "_" + (Long.MAX_VALUE-dataLong);
   
     // 对方手机号码
     String pNum2 = getPhone("170");
     
     Put put = new Put(rowkey.getBytes());
     put.add("cf".getBytes(), "phonenum".getBytes(), pNum2.getBytes());
     put.add("cf".getBytes(), "type".getBytes(), (r.nextInt(2)+"").getBytes());
     put.add("cf".getBytes(), "date".getBytes(), dateStr.getBytes());
   
     puts.add(put);
    } catch (ParseException e) {
     e.printStackTrace();
    }
   }
  }
 
  htable.put(puts);
 }
 
 
 /**
  * 查询某个手机号 一个月内的通话记录
  * 手机号:186964321629
  * 时间段:2017-01月份
  * @throws Exception
  */
 @Test
 public void scanDB1() throws Exception {
  Scan scan = new Scan();
 
  String startRowkey = "186964321629_" + (Long.MAX_VALUE - sdf.parse("20170201000000").getTime());
  String stopRowkey = "186964321629_" + (Long.MAX_VALUE - sdf.parse("20170101000000").getTime());
 
  scan.setStartRow(startRowkey.getBytes());
  scan.setStopRow(stopRowkey.getBytes());
 
  ResultScanner rss = htable.getScanner(scan);
  for (Result rs : rss) {
   System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "phonenum".getBytes()))));
   System.out.print(" " + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));
   System.out.println(" " + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "date".getBytes()))));
  }
 }
 
 /**
  * 查询某个手机号 所有的被叫类型的(type=1) 通话记录
  * 手机号码:186964321629
  * @throws Exception
  */
 @Test
 public void scanDB2() throws Exception {
  FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
 
  PrefixFilter prefixFilter = new PrefixFilter("186964321629_".getBytes());
  list.addFilter(prefixFilter);
 
  SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("cf".getBytes(),
    "type".getBytes(), CompareOp.EQUAL, "1".getBytes());
  list.addFilter(singleColumnValueFilter);
 
  Scan scan = new Scan();
  scan.setFilter(list);
 
  ResultScanner rss = htable.getScanner(scan);
  for (Result rs : rss) {
   System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "phonenum".getBytes()))));
   System.out.print(" " + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "type".getBytes()))));
   System.out.println(" " + new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "date".getBytes()))));
  }
 }
 
 public Random r = new Random();
 
 /**
  * 随机返回手机号码
  * @param prefix 手机号码前缀 eq:186
  * @return
  */
 public String getPhone(String prefix) {
  return prefix + String.format("%08d", r.nextInt(999999999));
 }
 
 /**
  * 随机返回日期 yyyyMMddHHmmss
  * @param year 年
  * @return
  */
 public String getDate(String year) {
  return year + String.format("%02d%02d%02d%02d%02d",
    new Object[]{r.nextInt(12)+1,r.nextInt(29),
   r.nextInt(24),r.nextInt(24),r.nextInt(24)});
 }
 
}

猜你喜欢

转载自blog.csdn.net/u011418530/article/details/80589730
5.4