HBASE部分:通话查询(涉及到造数据,过滤器,)

package com.sxt.hbasedemo;

import java.io.IOException;
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.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.sxt.hbasedemo.Phone.PhoneDetail;

public class HBaseDemo {

	HBaseAdmin admin = null;
	HTable table = null;
	private String tn = "phone";

	@Before
	public void init() throws Exception {
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
		admin = new HBaseAdmin(conf);
		table = new HTable(conf, tn.getBytes());
	}

	@Test
	public void createTable() throws Exception {
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tn));
		HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
		family.setInMemory(true);
		if (admin.tableExists(tn)) {
			admin.disableTable(tn);
			admin.deleteTable(tn);
		}
		admin.createTable(desc);
	}

	@Test
	public void insertDB() throws Exception {
		String rowkey = "1111";
		Put put = new Put(rowkey.getBytes());
		put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
		put.add("cf".getBytes(), "age".getBytes(), "12".getBytes());
		put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());
		table.put(put);
	}

	@Test
	public void get() throws Exception {
		String rowkey = "1111";
		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 = table.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());
		// System.out.println(new String(cell.getValue()));
		System.out.println(new String(CellUtil.cloneValue(cell)));
		System.out.println(new String(CellUtil.cloneValue(cell2)));
		System.out.println(new String(CellUtil.cloneValue(cell3)));
	}

	/**
	 * 给10个用户,每个用户添加100条记录
	 * 
	 * @throws Exception
	 */
	@Test
	public void insertDB2() throws Exception {
		List<Put> list = new ArrayList<Put>();
		for (int i = 0; i < 10; i++) {
			String phoneNum = getPhoneNum("158");
			for (int j = 0; j < 100; j++) {
				String dnum = getPhoneNum("177");
				String length = r.nextInt(99) + "";
				String type = r.nextInt(2) + "";
				long dateString = sdf.parse(getDate("2018")).getTime();
				String rowkey = phoneNum + "_" + (Long.MAX_VALUE - dateString);
				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(), "date".getBytes(), (dateString + "").getBytes());
				list.add(put);
			}
		}
		table.put(list);
	}

	@Test
	public void insertDB3() throws Exception {
		List<Put> list = new ArrayList<Put>();
		for (int i = 0; i < 100; i++) {
			String phoneNum = getPhoneNum("158");
			for (int j = 0; j < 1000; j++) {
				String dnum = getPhoneNum("177");
				String length = r.nextInt(99) + "";
				String type = r.nextInt(2) + "";
				long dateString = sdf.parse(getDate("2018")).getTime();
				PhoneDetail.Builder pd = PhoneDetail.newBuilder();
				String rowkey = phoneNum + "_" + (Long.MAX_VALUE - dateString);
				pd.setDnum(dnum);
				pd.setLength(length);
				pd.setType(type);
				pd.setDate(dateString + "");
				Put put = new Put(rowkey.getBytes());
				put.add("cf".getBytes(), "phone".getBytes(), pd.build().toByteArray());
				list.add(put);
			}
		}
		table.put(list);
	}

	/**
	 * 有10个用户,每个用户每天产生100条记录,属于一个rowkey
	 * 
	 * @throws Exception
	 */
	@Test
	public void insertDB4() throws Exception {
		List<Put> list = new ArrayList<Put>();
		for (int i = 0; i < 10; i++) {
			String phoneNum = getPhoneNum("158");
			String rowkey = phoneNum + "_" + (Long.MAX_VALUE - sdf.parse("20180703000000").getTime());
			Phone.dayPhoneDetail.Builder dayPhone = Phone.dayPhoneDetail.newBuilder();
			for (int j = 0; j < 100; j++) {
				String dnum = getPhoneNum("177");
				String length = r.nextInt(99) + "";
				String type = r.nextInt(2) + "";
				long dateString = sdf.parse(getDate2("20180703")).getTime();
				Phone.PhoneDetail.Builder pd = Phone.PhoneDetail.newBuilder();
				pd.setDnum(dnum);
				pd.setLength(length);
				pd.setType(type);
				pd.setDate(dateString + "");
				dayPhone.addDayPhone(pd);
			}
			Put put = new Put(rowkey.getBytes());
			put.add("cf".getBytes(), "day".getBytes(), dayPhone.build().toByteArray());
			list.add(put);
		}
		table.put(list);
	}

	private String getDate2(String string) {
		return string+String.format("%02d%02d%02d",  r.nextInt(24),
				r.nextInt(60), r.nextInt(60));
	}

	@Test
	public void get2() throws Exception {
		String rowkey = "15893265824_9223370521356379807";
		Get get = new Get(rowkey.getBytes());
		Result rs = table.get(get);
		Cell cell = rs.getColumnLatestCell("cf".getBytes(), "phone".getBytes());
		Phone2.PhoneDetail pd = Phone2.PhoneDetail.parseFrom(CellUtil.cloneValue(cell));
		System.out.println(pd);
	}
	 

	@Test
	public void get3() throws Exception {
		String rowkey = "15898591280_9223370506307575807";
		Get get = new Get(rowkey.getBytes());
		Result rs = table.get(get);
		long start = System.currentTimeMillis();
		Cell cell = rs.getColumnLatestCell("cf".getBytes(), "day".getBytes());
		Phone.dayPhoneDetail dayPhone = Phone.dayPhoneDetail.parseFrom(CellUtil.cloneValue(cell));
		for (Phone.PhoneDetail pd : dayPhone.getDayPhoneList()) {
			System.out.println(pd);
		}
		long end = System.currentTimeMillis();
		System.out.println(end-start);
	}
	Random r = new Random();

	private String getDate(String string) {
		return string + String.format("%02d%02d%02d%02d%02d", r.nextInt(12) + 1, r.nextInt(30) + 1, r.nextInt(24),
				r.nextInt(60), r.nextInt(60));
	}

	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

	private String getPhoneNum(String string) {
		return string + String.format("%08d", r.nextInt(99999999));
	}

	/**
	 * 查询二月份的所有通话记录
	 * 
	 * @throws Exception
	 */
	@Test
	public void scan() throws Exception {
		Scan scan = new Scan();
		String phoneNumber = "15889455995";
		String startRow = phoneNumber + "_" + (Long.MAX_VALUE - sdf.parse("20180301000000").getTime());
		String stopRow = phoneNumber + "_" + (Long.MAX_VALUE - sdf.parse("20180201000000").getTime());
		scan.setStartRow(startRow.getBytes());
		scan.setStopRow(stopRow.getBytes());
		ResultScanner rss = table.getScanner(scan);
		for (Result rs : rss) {
			System.out
					.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));
			System.out.print("---"
					+ new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".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()))));
		}
	}

	/**
	 * 查询某一个用户主叫的所有通话记录 主叫=1
	 * 
	 * @throws Exception
	 */
	@Test
	public void scan2() throws Exception {
		FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
		SingleColumnValueFilter filter1 = new SingleColumnValueFilter("cf".getBytes(), "type".getBytes(),
				CompareOp.EQUAL, Bytes.toBytes("1"));
		list.addFilter(filter1);
		PrefixFilter filter2 = new PrefixFilter("15889455995".getBytes());
		list.addFilter(filter2);
		Scan scan = new Scan();
		scan.setFilter(list);
		ResultScanner rss = table.getScanner(scan);
		for (Result rs : rss) {
			System.out
					.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes()))));
			System.out.print("---"
					+ new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf".getBytes(), "length".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()))));
		}
	}

	@After
	public void close() throws Exception {
		if (admin != null) {
			admin.close();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值