类: QueryWithFilter
package com.atguigu.dataconsumer.query;
import com.atguigu.dataconsumer.util.HBaseFilterUtil;
import com.atguigu.dataconsumer.util.HBaseUtil;
import com.atguigu.dataconsumer.util.PropertyUtil;
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.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class QueryWithFilter {
public static void main(String[] args) throws IOException {
queryBeginEnd("16480981069", "2018-03", "2018-12");
}
public static void query(String phone, int year, int month, int day) throws IOException {
Table table = HBaseUtil.getTable(PropertyUtil.getProperty("hbase.table.name"));
if (month != -1 && day != -1) {
queryOneDay(table, phone, year, month, day);
} else if (month != -1 && day == -1) {
queryOneMonth(table, phone, year, month);
} else {
queryOneYear(table, phone, year);
}
}
public static void queryOneDay(Table table, String phone, int year, int month, int day) throws IOException {
Scan scan = new Scan();
Filter filter1 = HBaseFilterUtil.eqFilter("info", "call1", Bytes.toBytes(phone));
Filter filter2 = HBaseFilterUtil.eqFilter("info", "call2", Bytes.toBytes(phone));
Filter filter12 = HBaseFilterUtil.orFilter(filter1, filter2);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
String dateStr = dateFormatter.format(calendar.getTime());
Filter filter3 = HBaseFilterUtil.subStringFilter("info", "startTime", dateStr);
Filter filter = HBaseFilterUtil.andFilter(filter12, filter3);
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
System.out.println(Bytes.toString(result.getRow()));
}
}
public static void queryOneMonth(Table table, String phone, int year, int month) throws IOException {
Scan scan = new Scan();
Filter filter1 = HBaseFilterUtil.eqFilter("info", "call1", Bytes.toBytes(phone));
Filter filter2 = HBaseFilterUtil.eqFilter("info", "call2", Bytes.toBytes(phone));
Filter filter12 = HBaseFilterUtil.orFilter(filter1, filter2);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1,1);
String dateStr = dateFormatter.format(calendar.getTime());
Filter filter3 = HBaseFilterUtil.subStringFilter("info", "startTime", dateStr);
Filter filter = HBaseFilterUtil.andFilter(filter12, filter3);
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
System.out.println(Bytes.toString(result.getRow()));
}
}
public static void queryOneYear(Table table, String phone, int year) throws IOException {
Scan scan = new Scan();
Filter filter1 = HBaseFilterUtil.eqFilter("info", "call1", Bytes.toBytes(phone));
Filter filter2 = HBaseFilterUtil.eqFilter("info", "call2", Bytes.toBytes(phone));
Filter filter12 = HBaseFilterUtil.orFilter(filter1, filter2);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
String dateStr = dateFormatter.format(calendar.getTime());
Filter filter3 = HBaseFilterUtil.subStringFilter("info", "startTime", dateStr);
Filter filter = HBaseFilterUtil.andFilter(filter12, filter3);
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
System.out.println(Bytes.toString(result.getRow()));
}
}
public static void queryBeginEnd(String phone, String beginTime, String endTime) throws IOException {
Table table = HBaseUtil.getTable(PropertyUtil.getProperty("hbase.table.name"));
Scan scan = new Scan();
Filter filter1 = HBaseFilterUtil.eqFilter("info", "call1", Bytes.toBytes(phone));
Filter filter2 = HBaseFilterUtil.eqFilter("info", "call2", Bytes.toBytes(phone));
Filter filter12 = HBaseFilterUtil.orFilter(filter1, filter2);
Filter filter3 = HBaseFilterUtil.gtFilter("info", "startTime", Bytes.toBytes(beginTime));
Filter filter4 = HBaseFilterUtil.ltFilter("info", "startTime", Bytes.toBytes(endTime));
Filter filter = HBaseFilterUtil.andFilter(filter12, filter3, filter4);
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
System.out.println(Bytes.toString(result.getRow()));
}
}
}