本文中使用的是最原始的java api, 没有使用spring-data-hbase,只是使用spring管理Hbase的配置
查询操作分为如下几个步骤:
1. 获取Hbase配置,这里的配置主要指hbase的地址。如果是Zookeeper管理的,可以使用Zookeeper的地址和端口
2. 根据配置获取Hbase连接:
connection = ConnectionFactory.createConnection(this.hbaseConfig.gethBaseConfiguration())
3. 根据Hbase连接,获取HTable。
HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));
4. 查询。多行查询(Scan方式)和单行查询(Get方式)
示例代码为scan方式
scan.setStartRow(Bytes.toBytes(startRow)); scan.setStopRow(Bytes.toBytes(endRow)); scan.setCaching(4000); scan.setBatch(3); //设置列族 // scan.addFamily(Bytes.toBytes(columnFamily)); // 设置列 scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("createtime")); scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("lat")); scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("lon"));
5. 获取查询数据,以scan为例
resultScanner = hTable.getScanner(scan);
6. 获取Cell中的数据
for (Result result : resultScanner) { RwwData rwwData = new RwwData(); for (Cell cell : result.rawCells()) { String k = Bytes.toString(CellUtil.cloneQualifier(cell)); String v = Bytes.toString(CellUtil.cloneValue(cell)); } }
其中:
CellUtil.cloneQualifier(cell)
获取的是每一列的名称。
CellUtil.cloneValue(cell)
获取的是每一列的名称对应的值。
HbaseConfig
public class HbaseConfig { private static String zookeeperAddr = ""; private static String zookeeperPoot = "2181"; private static int zkRetry = 2; private static Configuration hBaseConfiguration = null; static { if (hBaseConfiguration == null) { Configuration configuration = new Configuration(); configuration.set("hbase.zookeeper.quorum", zookeeperAddr); configuration.set("hbase.zookeeper.property.clientPort", zookeeperPoot); configuration.setInt("hbase.client.retries.number", zkRetry); hBaseConfiguration = HBaseConfiguration.create(configuration); } } public static Configuration gethBaseConfiguration() { return hBaseConfiguration; } public void setZookeeperAddr(String zookeeperAddr) { this.zookeeperAddr = zookeeperAddr; } public void setZookeeperPoot(String zookeeperPoot) { this.zookeeperPoot = zookeeperPoot; } public void setZkRetry(int zkRetry) { this.zkRetry = zkRetry; } }
HbaseConnection
public class HbaseConnection { private static Connection connection = null; private static HbaseConfig hbaseConfig; static { if (connection == null) { try { connection = ConnectionFactory.createConnection(hbaseConfig.gethBaseConfiguration()); } catch (IOException e) { e.printStackTrace(); } } } public static Connection getConnection() { return connection; } public void setHbaseConfig(HbaseConfig hbaseConfig) { this.hbaseConfig = hbaseConfig; } }
HbaseService
public class HbaseService { private Logger logger = LoggerFactory.getLogger(HbaseService.class); private final long BASE = 2147483647; private HbaseConnection hbaseCcnnection; /** * @param tableName : 表名 * @param columnFamily:列族 * @param phone:手机号 * @param beginTime:查询开始时间 * @param endTime:查询结束时间 * @return * @throws IOException */ public List<GeoPoint> getGeoPoints(final String tableName, final String columnFamily , String phone, long beginTime, long endTime) throws IOException, InvocationTargetException, IllegalAccessException, ParseException { List<GeoPoint> geoPointList = new ArrayList<>(); Connection connection = HbaseConnection.getConnection(); HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName)); //构建scan Scan scan = new Scan(); String startRow = this.getRowKey(phone, endTime); String endRow = this.getRowKey(phone, beginTime); System.out.println(startRow + " : " + endRow); scan.setStartRow(Bytes.toBytes(startRow)); scan.setStopRow(Bytes.toBytes(endRow)); scan.setCaching(4000); scan.setBatch(3); //设置列族 // scan.addFamily(Bytes.toBytes(columnFamily)); // 设置列 scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("createtime")); scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("lat")); scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("lon")); ResultScanner resultScanner = null; List<RwwData> rowDataList = new ArrayList<>(); //获取坐标点 try { resultScanner = hTable.getScanner(scan); for (Result result : resultScanner) { System.out.println("result: " + result); RwwData rwwData = new RwwData(); System.out.println("rawCelles" + result.rawCells()); for (Cell cell : result.rawCells()) { String k = Bytes.toString(CellUtil.cloneQualifier(cell)); String v = Bytes.toString(CellUtil.cloneValue(cell)); rwwData.set(k, v); } System.out.println(rwwData); rowDataList.add(rwwData); } } catch (Exception e) { logger.error("从Hbase中获取坐标点信息出错", e); e.printStackTrace(); } finally { resultScanner.close(); } System.out.println("【rowDataList : 】" + rowDataList.size()); System.out.println(rowDataList); if (rowDataList.size() > 0) { //按照时间升序排序 Collections.sort(rowDataList); System.out.println("【rowDataList sort : 】" + rowDataList); // GeoPoint geoPoint = new GeoPoint(); for (RwwData rwwData : rowDataList) { GeoPoint geoPoint = new GeoPoint(); // BeanUtils.copyProperties(geoPoint, rwwData); geoPoint.x = rwwData.lat; //维度 geoPoint.y = rwwData.lon; //经度 geoPointList.add(geoPoint); } } System.out.println(geoPointList); return geoPointList; } /** * phone+(2147483647-指定时间秒数) * * @param phone * @param time * @return * @throws ParseException */ private String getRowKey(String phone, long time) throws ParseException { // long phoneNum = NumberUtils.toLong(phone); // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // long timeMillSeconds = format.parse(time).getTime(); long result = this.BASE - time / 1000; return phone + result; } /** * */ private class RwwData implements Comparable<RwwData> { private String createtime; private double lat; //维度,lat private double lon; //经度,lon //可以使用反射啊,用最简单的 public void set(String k, String value) throws NoSuchFieldException, IllegalAccessException { Preconditions.checkArgument(StringUtils.isNotBlank(k)); Preconditions.checkArgument(value != null); Field f = RwwData.class.getDeclaredField(k); f.setAccessible(true); f.set(this, value); // if (StringUtils.equals("createtime", k)) { // this.createtime = String.valueOf(value); // } else if (StringUtils.equals("lat", k)) { // this.x = Double.parseDouble(value); //维度 // } else if (StringUtils.equals("lon", k)) { // this.y = Double.parseDouble(value); //经度 // } else { // throw new IllegalArgumentException(" no properties " + k + " found"); // } } @Override public int compareTo(RwwData o) { return this.createtime.compareTo(o.createtime); } public double getLon() { return lon; } public void setLon(double lon) { this.lon = lon; } public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } @Override public String toString() { return "RwwData{" + "createtime=‘" + createtime + ‘\‘‘ + ", lat=" + lat + ", lon=" + lon + ‘}‘; } } public class GeoPoint { private double x; private double y; private double xyy; private double yx; private String fyx; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getXyy() { return xyy; } public void setXyy(double xyy) { this.xyy = xyy; } public double getYx() { return yx; } public void setYx(double yx) { this.yx = yx; } public String getFyx() { return fyx; } public void setFyx(String fyx) { this.fyx = fyx; } @Override public String toString() { return "GeoPoint{" + "x=" + x + ", y=" + y + ‘}‘; } } }
时间: 2024-10-21 11:52:38