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()))));
}
}
}