public class HbaseUtil {
private static SimpleDateFormat parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Configuration conf = null;
static{
setConf();
}
private static void setConf(){
conf = HBaseConfiguration.create();
String userDir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
Path hconf_path = new Path(userDir + "conf.xml");
conf.addResource(hconf_path);
}
public static Connection getConn() throws IOException {
return ConnectionFactory.createConnection(conf);
}
/**
* 该方法用于关闭表和connection的连接
* @param table
* @param conn
*/
private static void closeSource(Table table, Connection conn,ResultScanner scanner){
try {
if(table != null) table.close();
if (conn != null) conn.close();
if (scanner != null) scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 轨迹查询:根据表名 mac 起始时间 结束时间查询
* @param tableName
* @param mac
* @param startTime
* @param endTime
* @return
* @throws IOException
*/
public static ResultScanner scan(String tableName, String mac, long startTime, long endTime) throws IOException {
Connection conn = null;
Table table = null;
ResultScanner scanner = null;
try {
conn = HbaseUtil.getConn();
table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
byte[] startRow = (mac + startTime).getBytes();
byte[] endRow = (mac + endTime).getBytes();
scan.setStartRow(startRow);
scan.setStopRow(endRow);
scanner = table.getScanner(scan);
return scanner;
}catch (Exception e){
e.printStackTrace();
}finally {
closeSource(table,conn,scanner);
}
return null;
}
}