HBase数据操纵--Day3

一:数据操纵语言

1:查看所有表

list

2:创建表名为s_behavior的表

create 's_behavior',{NAME=>'pc'},{NAME=>'ph'}

3:查看建表

describe 's_behavior'    

4:修改表

disable 's_behavior'   //下线表
alter 's_behavior' {NAME=>"CF",REPLICATUION=>"1",KEEP_DELETED_CELLS=>'TRUE'}  //修改表
enable 's_behavior'  //上线表

5:插入数据

put 's_behavior','12345','pc:v','1001'

6:获取数据

get 's_behavior' '12345'  //获取一条数据
get 's_behavior' '12345',{timestamp=1521423996693}  //获取一条数据在某个时刻的镜像

7:扫描数据

scan 's_behavior'   //扫描表数据
scan 's_behavior' {TIMESTAMP=>[1521423996739,15214239961757]}  //s扫描区间内的数据
scan 's_behavior',{VERSIONS=2}  //获取两个版本数据

8:删除数据

delete 's_behavior','12345','pc:v' //删除列数据
deleteall 's_behavior','12345'  //删除行数据
truncate 's_behavior','删除表数据'

9:删除表

drop 's_behavior'

二:Java操纵HBase

1:格式化工具类

package edu.huanghuai.cn.chpt05;

public class RowKeyUtil {
	/**
	 * 补齐20位再反转
	 */
	public String formatUserId(long userId){
		String  str=String.format("%0"+20+"d",userId);
		StringBuilder sb=new StringBuilder(str);
		return sb.reverse().toString();
	}
	/**
	 * Long.Max_Value-lastupdate得到值再补齐20位
	 */
	public String formatLastUpdate(long lastupdate){
		if(lastupdate<0){
			lastupdate=0;
		}
		long diff=Long.MAX_VALUE-lastupdate;
		return String.format("%0"+20+"d", diff);
	}
	/*
	 * 格式化时间戳
	 */
	public String formatTimeStamp(long timestamp){
		String  str=String.format("%0"+20+"d",timestamp);
		return str;
	}
}

2:连接工厂类

package edu.huanghuai.cn.chpt06;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class HBaseConnectionFactory {
	private static Connection connection=null;
	static{
		createConnection();
	}
	private static synchronized void createConnection(){
		Configuration configuration=HBaseConfiguration.create();
		configuration.set("hbase.client.pause", "100");
		configuration.set("hbase.client.write.buffer","10485760");
		configuration.set("hbase.client.retries.number","5");
		configuration.set("hbase.zookeeper.property.clientPort","2181");
		configuration.set("hbase.client.scanner.timeout.period", "100000");
		configuration.set("hbase.rpc.timeout", "40000");
		try{
			connection=ConnectionFactory.createConnection(configuration);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	public static Connection getConnection(){
		return connection;
	}
}

3:建表

package edu.huanghuai.cn.chpt06;

import java.io.IOException;


import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;

public class TableDemo {
	/**
	 * 创建表
	 * @param tableName 表名
	 * @param familyNames 列族名
	 * @return
	 * @throws IOException
	 */
	public boolean createTable(String tableName,String... familyNames) throws IOException{
		Admin admin=HBaseConnectionFactory.getConnection().getAdmin();
		if(admin.tableExists(TableName.valueOf(tableName))){
			return false;
		}
		//通过HTableDescriptor类描述一个表,HColumnDescriptor描述一个列族
		HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
		for(String familyName:familyNames){
			HColumnDescriptor oneFamily=new HColumnDescriptor(familyName);
			//设置行健编码格式
			oneFamily.setDataBlockEncoding(DataBlockEncoding.PREFIX_TREE);
			//设置数据保留多版本
			oneFamily.setMaxVersions(3);
			tableDescriptor.addFamily(oneFamily);
		}
		admin.createTable(tableDescriptor);
		return true;
	}
	
	/**
	 * 删除表
	 * @param tableName 表名
	 * @return
	 * @throws IOException 
	 */
	public boolean deleteTable(String tableName) throws IOException{
		Admin admin=HBaseConnectionFactory.getConnection().getAdmin();
		if(!admin.tableExists(TableName.valueOf(tableName))){
			return false;
		}
		//删除前要将表禁用
		if(!admin.isTableDisabled(TableName.valueOf(tableName))){
			admin.disableTable(TableName.valueOf(tableName));	
		}
		admin.deleteTable(TableName.valueOf(tableName));
		return true;
	}
	public static void main(String[] args) throws IOException{
		TableDemo tableDemo=new TableDemo();
		//新建表
		System.out.println(tableDemo.createTable("s_behavior","pc","ph"));
		//删除表
		//System.out.println(tableDemo.deleteTable("s_behavior"));
	}
}

4:压缩分区

package edu.huanghuai.cn.chpt06;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.MetaTableAccessor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;

public class AdminDemo {
	private static ExecutorService executors=Executors.newFixedThreadPool(5);		
	
	public void compact(String table)throws Exception{
		Admin admin=HBaseConnectionFactory.getConnection().getAdmin();
		Map<String,List<byte[]>> serverMap=new HashMap<String,List<byte[]>>();
		List<HRegionInfo> regionInfos=admin.getTableRegions(TableName.valueOf(table));
		//将表所有的分区按所在分区服务器分组,这样可以并发在每个分区服务器压缩一个分区
		for(HRegionInfo hRegionInfo:regionInfos){
			HRegionLocation regionLocation=MetaTableAccessor.getRegionLocation(HBaseConnectionFactory.getConnection(), hRegionInfo);
			if(serverMap.containsKey(regionLocation.getHostname())){
				serverMap.get(regionLocation.getHostname()).add(hRegionInfo.getRegionName());
			}else{
				List<byte[]> list=new ArrayList<byte[]>();
				list.add(hRegionInfo.getRegionName());
				serverMap.put(regionLocation.getHostname(), list);
				
			}
		}
		List<Future<String>> futures=new ArrayList<Future<String>>();
		//为每个服务器压缩一个分区
		for(Map.Entry<String, List<byte[]>> entry:serverMap.entrySet()){
			futures.add(executors.submit(new HBaseCompactThread(entry)));
		}
		for(Future<String> future:futures){
			System.out.println("compact results"+future.get());
		}
	}
	class  HBaseCompactThread implements Callable<String>{

		private Map.Entry<String, List<byte[]>> entry;
		public HBaseCompactThread(Map.Entry<String, List<byte[]>> entry){
			this.entry=entry;
		}
		@Override
		public String call() throws Exception {
			Admin admin=HBaseConnectionFactory.getConnection().getAdmin();
			for(byte[] bytes:entry.getValue()){
				AdminProtos.GetRegionInfoResponse.CompactionState state=admin.getCompactionStateForRegion(bytes);
				//如果分区当前状态不为主压缩,则触发主压缩
				if(state!=AdminProtos.GetRegionInfoResponse.CompactionState.MAJOR){
					admin.majorCompactRegion(bytes);
				}
				while(true){
					Thread.sleep(3*60*1000);
					state=admin.getCompactionStateForRegion(bytes);
					if(state==AdminProtos.GetRegionInfoResponse.CompactionState.NONE){
						break;
					}
				}
			}
			// TODO Auto-generated method stub
			return entry.getKey()+"success";
		}
		
	}
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		AdminDemo adminDemo=new AdminDemo();
		adminDemo.compact("s_behavior");
	}

}

5:插入数据

package edu.huanghuai.cn.chpt06;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt05.RowKeyUtil;

public class PutDemo {

	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIEW="v";
	private static final String COLUMN_ORDER="o";
	private static final String[] ITEM_ID_ARRAY=new String[]{"1001","1002","1003","1004"};
	private static final long userId=12345;
	private static RowKeyUtil rowKeyUtil=new RowKeyUtil();
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		List<Put> actions=new ArrayList<>();
		Random random=new Random();
		for(int i=0;i<ITEM_ID_ARRAY.length;i++){
			String rowkey=generateRowkey(userId, System.currentTimeMillis(), i);
			Put put=new Put(Bytes.toBytes(rowkey));
			//添加列
			put.addColumn(Bytes.toBytes(CF_PC), Bytes.toBytes(COLUMN_VIEW), Bytes.toBytes(ITEM_ID_ARRAY[i]));
			if(random.nextBoolean()){
				put.addColumn(Bytes.toBytes(CF_PC), Bytes.toBytes(COLUMN_ORDER), Bytes.toBytes(ITEM_ID_ARRAY[i]));
			}
			actions.add(put);
		}
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		//设置不启动客户端缓存,直接提交
		((HTable)table).setAutoFlush(true,false);
		//方法一:向表写入数据
		Object[] results=new Object[actions.size()];
		//方法二:执行Table的批量操作,actions可以是Put,Delete,Get,Increment等操作,并且可以获取执行结果
		table.batch(actions, results);
		//如果启用了客户端缓存,也可以执行flushCommits显示提交
		//((HTable)table).flushCommits();
	}
	private static String generateRowkey(long userId,long timestamp,long seqId){
		return rowKeyUtil.formatUserId(userId)+rowKeyUtil.formatTimeStamp(timestamp)+seqId;
	}
}

6:删除数据

package edu.huanghuai.cn.chpt06;

import java.io.IOException;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class DeleteDemo {

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIEW="v";
	private static final String COLUMN_ORDER="o";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String rowkeyToDelete="5432100000000000000015537799646513";
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		Get oneGet=new Get(Bytes.toBytes(rowkeyToDelete));
		Result result=table.get(oneGet);
		printResult(result, "1");
		
		Delete deleteColumn=new Delete(Bytes.toBytes(rowkeyToDelete));
		//设置需要删除的列
		deleteColumn.addColumn(Bytes.toBytes(CF_PC), Bytes.toBytes(COLUMN_VIEW));
		//设置需要删除一天之前的数据版本
	    deleteColumn.setTimestamp(System.currentTimeMillis()-24*60*60*1000);
	    table.delete(deleteColumn);
		oneGet=new Get(Bytes.toBytes(rowkeyToDelete));
		result=table.get(oneGet);
		printResult(result, "2");
		
		Delete deleteFamily=new Delete(Bytes.toBytes(rowkeyToDelete));
		//设置需要删除的列族
		deleteFamily.addFamily(Bytes.toBytes(CF_PC));
		//设置需要删除一天之前的数据版本
	    deleteFamily.setTimestamp(System.currentTimeMillis()-24*60*60*1000);
	    table.delete(deleteFamily);
		oneGet=new Get(Bytes.toBytes(rowkeyToDelete));
		result=table.get(oneGet);
		printResult(result, "3");
		
		Delete deleteRow=new Delete(Bytes.toBytes(rowkeyToDelete));
		//删除整行
		table.delete(deleteRow);
		oneGet=new Get(Bytes.toBytes(rowkeyToDelete));
		result=table.get(oneGet);
		printResult(result, "4");
	}
	private static void printResult(Result result,String lineNo){
		Cell[] cells=result.rawCells();
		for(Cell cell:cells){
			String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
			String value=Bytes.toString(CellUtil.cloneValue(cell));
			System.out.println("lineNo="+lineNo+",qualifier="+qualifier+",value="+value);
			
		}
	}
}

7:获取数据

package edu.huanghuai.cn.chpt06;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class GetDemo {

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIRE="v";
	private static final String COLUMN_ORDER="o";
	public static void main(String[] args) throws ParseException, IOException {
		// TODO Auto-generated method stub
		List<Get> gets=new ArrayList<>();
		Get oneGet=new Get(Bytes.toBytes("54321000000000000000000000015541056892291"));
		//设置需要Get的数据列族
		oneGet.addFamily(Bytes.toBytes(CF_PC));
		//设置需要get的数据列
		//oneGet.addColumn(Bytes.toBytes(CF_PHONE),Bytes.toBytes(COLUMN_ORDER));
		//设置Get的数据时间范围为2018年1月1日到现在
		String startS="2018-01-01";
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date startDate=dateFormat.parse(startS);
		oneGet.setTimeRange(startDate.getTime(), System.currentTimeMillis());
		//设置Get的版本数据为2
		oneGet.setMaxVersions(2);
		gets.add(oneGet);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		Result[] results=table.get(gets);
		for(Result result:results){
			if(null!=result.getRow()){
				Cell[] cells=result.rawCells();
				System.out.println("rowkey="+Bytes.toString(result.getRow()));
				for(Cell cell:cells){
					String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
					String value=Bytes.toString(CellUtil.cloneValue(cell));
					System.out.println("qualifier="+qualifier+",value="+value);
					
				}
			}
		}
	}

}

8:扫描数据

package edu.huanghuai.cn.chpt06;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.IsolationLevel;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt05.RowKeyUtil;

public class ScanDemo {

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIRE="v";
	private static final String COLUMN_ORDER="o";
	private static final long userId=12345;
	public static final String MIN_TIME="00000000000000000000";
	public static final String MAX_TIME="99999999999999999999";
	private static RowKeyUtil rowKeyUtil=new RowKeyUtil();
	public static void main(String[] args) throws ParseException, IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();
		scan.addFamily(Bytes.toBytes(CF_PC));
		//设置需要Scan的数据列
		//scan.addColumn(Bytes.toBytes(CF_PHONE), Bytes.toBytes(COLUMN_ORDER));
		//设置small scan以提高性能,如果扫描的数据在一个数据块内,则应该设置为true
		scan.setSmall(true);
		//设置开始扫描行健,结果包含开始行
		scan.setStartRow(Bytes.toBytes(rowKeyUtil.formatUserId(userId)+MIN_TIME));
		//设置扫描结束行健,结果不包含结束行
		scan.setStopRow(Bytes.toBytes(rowKeyUtil.formatUserId(userId)+MAX_TIME));
		//设置事务隔离级别
		scan.setIsolationLevel(IsolationLevel.READ_COMMITTED);
		//设置每次RPC的请求读取数据行
		scan.setCaching(100);
		//设置scan的数据时间范围为2018年1月1日到现在
		String startS="2018-1-1";
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date startData=dateFormat.parse(startS);
		scan.setTimeRange(startData.getTime(), System.currentTimeMillis());
		//设置scan的数据版本为2
		scan.setMaxVersions(2);
		//设置是否缓存读取的数据块,如果数据会被多次读取则应该设置为true,如果数据仅被读取一次则设置为false
		scan.setCacheBlocks(false);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
	
		Result result=null;
		while((result=resultScanner.next()) != null){
			if(result.getRow()==null){
				continue;
			}
			Cell[] cells=result.rawCells();
			for(Cell cell:cells){
				String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
				String value=Bytes.toString(CellUtil.cloneValue(cell));
				System.out.println("qualifier="+qualifier+",value="+value);
			}
		}
		
	}

}

9:自增器

package edu.huanghuai.cn.chpt06;

import java.util.ArrayList;
import java.util.List;


import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class IncrDemo {

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String COLUMN_FOR_INCR="i";
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		List<Thread> incrThreadList=new ArrayList<>();
		for(int i=0;i<20;i++){
			IncrThread incrThread=new IncrThread();
			Thread t=new Thread(incrThread);
			t.setName("Thread_"+i);
			incrThreadList.add(t);
			t.start();
		}
		for(Thread incrThread:incrThreadList){
			incrThread.join();
		}
	}
	
	static class  IncrThread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try{
				Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
				Increment increment=new Increment(Bytes.toBytes("rowkeyforincr"));
				increment.addColumn(Bytes.toBytes(CF_PC), Bytes.toBytes(COLUMN_FOR_INCR), 1);
				table.increment(increment);
				Get oneGet=new Get(Bytes.toBytes("rowkeyforincr"));
				Result getResult=table.get(oneGet);
				Cell[] getCells=getResult.rawCells();
				for(Cell cell:getCells){					
					String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
					Long value=Bytes.toLong(CellUtil.cloneValue(cell));
					System.out.println(Thread.currentThread().getName()+":qualifier="+qualifier+",value="+value);												
				}
			}catch (Exception e) {
				// TODO: handle exception
				System.out.println("incr failed"+e.getMessage());
			}
		}
		
	}
}

三:过滤器

1:BaseDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;

public class BaseDemo {
	protected static void printResult(ResultScanner resultScanner) throws IOException {
		Result result=null;
		while((result=resultScanner.next())!=null){
			if(result.getRow()==null){
				continue;
			}
			Cell[] cells=result.rawCells();
			System.out.println("rowkey="+Bytes.toString(result.getRow()));
			for(Cell cell:cells){
				String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
				String value=Bytes.toString(CellUtil.cloneValue(cell));
				System.out.println("qualifier="+qualifier+",value="+value);
			}
		}
	}
}

2:KeyOnlyFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class KeyOnlyFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();		
		/*
		 * 查询s_behavior表中的所有行健
		 */
		scan.setFilter(new KeyOnlyFilter());
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

3:FirstKeyOnlyFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class FirstKeyOnlyFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();		
		/*
		 * 查询s_behavior表中的所有数据,每行只返回第一列,通常与KeyOnlyFilter一起使用
		 */
		scan.setFilter(new FirstKeyOnlyFilter());
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

4:PreFixFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt05.RowKeyUtil;
import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class PreFixFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		Scan scan=new Scan();	
		RowKeyUtil rowKeyUtil=new RowKeyUtil();
		/*
		* 查询用户12345的所有数据
		*/
		PrefixFilter prefixFilter=new PrefixFilter(Bytes.toBytes(rowKeyUtil.formatUserId(12345)));
		scan.setFilter(prefixFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

5:RowFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt05.RowKeyUtil;
import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class RowFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();	
		RowKeyUtil rowKeyUtil=new RowKeyUtil();
		/*
		* 查询用户12345在2020年1月1日前数据
		*/
		String startS="2020-01-01";
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date startDate=dateFormat.parse(startS);
		
		String rowKey=rowKeyUtil.formatUserId(12345)+rowKeyUtil.formatTimeStamp(startDate.getTime())+"0";
		
		//CompareFilter.CompareOp.LESS表示该行键之前的数据
		RowFilter rowFilter=new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(rowKey)));
		scan.setFilter(rowFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

6: SingleColumnValueFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class SingleColumnValueFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIRE="v";
	private static final String COLUMN_ORDER="o";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();	
		/*
		* 查询表s_behavior列限定符pc:o,值为1004的数据行
		*/
	
		SingleColumnValueFilter singleColumnValueFilter=new SingleColumnValueFilter(Bytes.toBytes(CF_PC),Bytes.toBytes(COLUMN_ORDER), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("1004"));
		scan.setFilter(singleColumnValueFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

7: TimestampsFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.TimestampsFilter;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class TimestampsFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();
		List<Long> timeStampList=new ArrayList<>();
		timeStampList.add(1554105695465L);
		/*
		 * 查询表中时间为1554105695465的数据
		 */
		TimestampsFilter timestampsFilter=new TimestampsFilter(timeStampList);
		scan.setFilter(timestampsFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

8: ValueFilterDemo 

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class ValueFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();	
		/*
		* 查询表s_behavior包含商品1001的数据
		* 注意一行有多列数据,只有值等于1001数据的一列会被返回
		*/			
		ValueFilter valueFilter=new ValueFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("1001")));
		scan.setFilter(valueFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);		
	}

}

9:WhileMatchFilterDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.WhileMatchFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class WhileMatchFilterDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scan scan=new Scan();	
		/*
		* 查询表s_behavior
		* 相当于while执行,当条件满足了返回结果
		* 如下过滤条件相当于遇见1009的数据内容就返回
		*/			
		WhileMatchFilter whileMatchFilter=new WhileMatchFilter(new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("1009"))));
		scan.setFilter(whileMatchFilter);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);		
	}

}

10:FilterListDemo

package edu.huanghuai.cn.chpt06.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.TimestampsFilter;
import org.apache.hadoop.hbase.util.Bytes;

import edu.huanghuai.cn.chpt06.HBaseConnectionFactory;

public class FilterListDemo extends BaseDemo{

	/**
	 * @param args
	 */
	private static final String TABLE="s_behavior";
	private static final String CF_PC="pc";
	private static final String CF_PHONE="ph";
	private static final String COLUMN_VIRE="v";
	private static final String COLUMN_ORDER="o";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		/*
		 * 查询表s_behavior中满足以下条件的数据行
		 * 列限定符pc:o为1004
		 * 列数据时间戳为1554105695465
		 * 只返回数据的行健,不返回列数据值
		 */
		List<Filter> filters=new ArrayList<>();
		Scan scan=new Scan();
		List<Long> timeStampList=new ArrayList<>();
		timeStampList.add(1554105695465L);
		TimestampsFilter timestampsFilter=new TimestampsFilter(timeStampList);
		SingleColumnValueFilter singleColumnValueFilter=new SingleColumnValueFilter(Bytes.toBytes(CF_PC),Bytes.toBytes(COLUMN_ORDER), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("1004"));
		filters.add(singleColumnValueFilter);
		filters.add(timestampsFilter);
		filters.add(new KeyOnlyFilter());
		FilterList filterList=new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);
		scan.setFilter(filterList);
		Table table=HBaseConnectionFactory.getConnection().getTable(TableName.valueOf(TABLE));
		ResultScanner resultScanner=table.getScanner(scan);
		printResult(resultScanner);
	}

}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值