hbase的常用操作工具类


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

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注