HBaseDemo

package com.sxt.hbase;

import java.io.IOException;
import java.io.InterruptedIOException;
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.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.FilterList.Operator;
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 {
	HBaseAdmin  admin = null;
	String TN = "phone";
	HTable table = null;
	@Before
	public void before() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		Configuration  con = new Configuration();
			con.set("hbase.zookeeper.quorum", "node02,node03,node04");
		  admin = new HBaseAdmin(con);
		  table = new HTable(con, TN);
	}
	@After
	public void end() throws IOException{
		if(admin!=null){
			admin.close();
			
		}
		if(table!=null){
			
			table.close();
		}
		
	}
	
	/**
	 * 创建 hbase 表
	 * @throws IOException 
	 */
	@Test
	public  void  createTable() throws IOException{
		if(admin.tableExists(TN)){
			admin.disableTable(TN);
			admin.deleteTable(TN);
		}
		HTableDescriptor  desc = new HTableDescriptor(TableName.valueOf(TN));
		HColumnDescriptor  cf = new HColumnDescriptor("cf".getBytes());
				cf.setMaxVersions(3);
				cf.setInMemory(true);
				desc.addFamily(cf);
		admin.createTable(desc);
	}
	/**
	 * 插入数据 put
	 * @throws InterruptedIOException 
	 * @throws RetriesExhaustedWithDetailsException 
	 */
	@Test
	public  void put() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
		Put put = new Put("222".getBytes());
		
			put.add("cf".getBytes(), "name".getBytes(), "xiaoming".getBytes());
			put.add("cf".getBytes(), "age".getBytes(), "23".getBytes());
		
		table.put(put);
		
	}
	/**
	 * 获取一条hbase数据
	 * @throws IOException 
	 */
	@Test
	public  void get() throws IOException{
		Get get = new Get("111".getBytes());
			get.addFamily("cf".getBytes());
			get.addColumn("cf".getBytes(), "name".getBytes());
		
		Result rs = table.get(get);
		Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
		 
			System.out.print (new String(CellUtil.cloneValue(cell)));
			System.out.println(" - "+new String(CellUtil.cloneRow(cell)));
	}
	
	/**
	 *  scan 表
	 * @throws IOException 
	 */
	@Test
	public  void  scan() throws IOException{
		Scan scan = new Scan();
		 scan.addFamily("cf".getBytes());
		 
		ResultScanner scanner = table.getScanner(scan);
		
		for(Result rs : scanner){
			Cell cellName= rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
			Cell cellAge = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
			System.out.println(new String(CellUtil.cloneValue(cellName))+" - "+new String(CellUtil.cloneValue(cellAge)));
			
		}
	}
	
	/**
	 * 100个用户 100条通话记录
	 * @throws ParseException 
	 * @throws InterruptedIOException 
	 * @throws RetriesExhaustedWithDetailsException 
	 */
	@Test
	public  void  insertDB() throws ParseException, RetriesExhaustedWithDetailsException, InterruptedIOException{
		List<Put> puts =  new ArrayList<Put> ();
		for (int i = 0; i < 100; i++) {
			//获取用户手机号
			 String num = this.getPhoneNum("188");
			for (int j = 0; j < 100; j++) {
					//获取对方手机号
				String dNum = this.getPhoneNum("177");
				//获取通话时间
				String date = this.getDate("2017");
				// 获取通话类型
				String type = r.nextInt(2)+"";
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				//创建rowkey
				String rowkey = num+"_"+(Long.MAX_VALUE-sdf.parse(date).getTime());
				
				Put put = new Put(rowkey.getBytes());
				
				put.add("cf".getBytes(), "dNum".getBytes(), dNum.getBytes());
				put.add("cf".getBytes(), "date".getBytes(), date.getBytes());
				put.add("cf".getBytes(), "type".getBytes(), type.getBytes());
				puts.add(put);
			}
		}
		
		table.put(puts);
		
	}
	
	/**
	 * 获取某用户2017 2月份 的 通话记录   18898325456
	 * @throws IOException 
	 * @throws ParseException 
	 */
	@Test
	public  void scanDB() throws IOException, ParseException{
		Scan scan = new Scan();
		scan.addFamily("cf".getBytes());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String startRow  = "18898325456_"+(Long.MAX_VALUE-sdf.parse("20170301000000").getTime());
		String stopRow  = "18898325456_"+(Long.MAX_VALUE-sdf.parse("20170201000000").getTime());
		scan.setStartRow(startRow.getBytes());
		scan.setStopRow(stopRow.getBytes());
		
		ResultScanner scanner = table.getScanner(scan);
		for(Result rs : scanner){
			Cell  cellDnum = rs.getColumnLatestCell("cf".getBytes(), "dNum".getBytes());
			Cell  cellDate = rs.getColumnLatestCell("cf".getBytes(), "date".getBytes());
			Cell  cellType = rs.getColumnLatestCell("cf".getBytes(), "type".getBytes());
			System.out.print(new String(CellUtil.cloneValue(cellDnum)));
			System.out.print(" - "+new String(CellUtil.cloneValue(cellDate)));
			System.out.println(" - "+new String(CellUtil.cloneValue(cellType)));
		}
	}
	
	/**
	 * 查询某个用户   主叫   1
	 * @throws IOException 
	 */
	@Test
	public  void scanDB2() throws IOException{
		FilterList    list = new FilterList(Operator.MUST_PASS_ALL);
		
		PrefixFilter  prefix = new PrefixFilter("18896831361".getBytes());
		list.addFilter(prefix);
		
		SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
				"cf".getBytes(),
				"type".getBytes(),
				CompareOp.EQUAL,
				 "1".getBytes()
				);
		list.addFilter(filter2);
		Scan   scan = new Scan();
		scan.setFilter(list);
		ResultScanner scanner = table.getScanner(scan);
		for(Result rs : scanner){
			Cell  cellDnum = rs.getColumnLatestCell("cf".getBytes(), "dNum".getBytes());
			Cell  cellDate = rs.getColumnLatestCell("cf".getBytes(), "date".getBytes());
			Cell  cellType = rs.getColumnLatestCell("cf".getBytes(), "type".getBytes());
			System.out.print(new String(CellUtil.cloneRow(cellDnum)));
			System.out.print(" - "+new String(CellUtil.cloneValue(cellDnum)));
			System.out.print(" - "+new String(CellUtil.cloneValue(cellDate)));
			System.out.println(" - "+new String(CellUtil.cloneValue(cellType)));
			
			
		}
	}
Random r = new Random(); 
	
	/**
	 * 随机生成测试手机号码
	 * prefix: 手机号码前缀   eq:186
	 * SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	 */
	public String getPhoneNum(String prefix) {
		return prefix + String.format("%08d", r.nextInt(99999999));
	}
	
	/**
	 * 随机生成时间
	 * @param year年
	 * @return 时间  格式:yyyyMMddHHmmss
	 */
	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)});
	}
	/**
	 * 随机生成时间
	 * @param  20170803
	 * @return 时间  格式:yyyyMMddHHmmss
	 */
	public String getDate2(String prefix) {
		return prefix + String.format("%02d%02d%02d", 
				new Object[]{ r.nextInt(24), r.nextInt(60), r.nextInt(60)});
	}
	
	
}

  

posted @ 2018-06-23 16:54  uuhh  阅读(413)  评论(0)    收藏  举报