1.1、插入测试数据
插入两条Double类型数据
def put(): Unit = {
val hbaseConf = HBaseConfiguration.create()
val hbaseConn = ConnectionFactory.createConnection(hbaseConf)
val table = hbaseConn.getTable(TableName.valueOf("test"))
val put = new Put(Bytes.toBytes("h004_001"))
put.addColumn(Bytes.toBytes("p"), Bytes.toBytes("am"), Bytes.toBytes(2000d))
val put2 = new Put(Bytes.toBytes("h004_002"))
val d: Double = 2000d
put2.addColumn(Bytes.toBytes("p"), Bytes.toBytes("am"), Bytes.toBytes(5000d))
table.put(put)
table.put(put2)
table.close()
}
1.2、查询 (ValueFilter)
1.2.1、大于100的数据
def get(): Unit = {
val hbaseConf = HBaseConfiguration.create()
val hbaseConn = ConnectionFactory.createConnection(hbaseConf)
val table = hbaseConn.getTable(TableName.valueOf("test"))
val scan = new Scan
val hotelId = "h004"
val limit = 100d // 此处必须和存储数据类型保持一致,否则报错
scan.setStartRow(Bytes.toBytes(hotelId + "_"))
scan.setStopRow(Bytes.toBytes(hotelId + "_~"))
scan.addColumn(Bytes.toBytes("p"), Bytes.toBytes("am"))
scan.setFilter(new ValueFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(limit))))
val scanner: ResultScanner = table.getScanner(scan)
val iterator: util.Iterator[Result] = scanner.iterator
while (iterator.hasNext) {
val rs: Result = iterator.next
val rowkey: String = Bytes.toString(rs.getRow)
val cell: Cell = rs.getColumnLatestCell(Bytes.toBytes("p"), Bytes.toBytes("am"))
println(Bytes.toDouble(cell.getValueArray, cell.getValueOffset))
println( rowkey)
}
table.close()
}
返回两条数据
但是返回结果只有5000的那条数据,2000的没有返回