val conf = hBaseConfig.get
val connection = ConnectionFactory.createConnection(conf)
val admin = connection.getAdmin
val tableNameObj = TableName.valueOf("hbaseTest")
val hTableDescriptor = new HTableDescriptor(tableNameObj)
val hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes("testFamily"))
hColumnDescriptor.setMaxVersions(Int.MaxValue)
hTableDescriptor.addFamily(hColumnDescriptor)
admin.createTable(hTableDescriptor)
put数据。
我们在刚建的hbaseTest表,testFamily列族下面进行操作。我们put的数据是以“row”为RowKey,”q1”,”q2”两个列,分别向其中put不同的数,时间戳依次递增。也就是我们在一个cell中put了多个不同时间戳的数据,
这里主要调用的api为Put下:
Put addColumn(byte [] family, byte [] qualifier, long ts, byte [] value)其中ts即时间戳,我们这里分别设定为0-100和0-200
val put=new Put(Bytes.toBytes("row"))
for(i<-0 to 100){
put.addColumn(familyBytes,Bytes.toBytes("q1"),i.toLong,Bytes.toBytes(i+"data_2"))
}
for(i<-0 to 200){
put.addColumn(familyBytes,Bytes.toBytes("q2"),i.toLong,Bytes.toBytes(i+"data2"))
}
table.put(put)
val table =new HTable(conf,tableName)
val get=new Get(Bytes.toBytes("row"))
get.setTimeStamp(6l)
get.addColumn(Bytes.toBytes("testFamily"),Bytes.toBytes("q1"))
val result=table.get(get)
val data=result.getValue(Bytes.toBytes("testFamily"),Bytes.toBytes("q1"))
println(Bytes.toString(data))
val table =new HTable(conf,tableName)
val get=new Get(Bytes.toBytes("row"))
get.setTimeStamp(6l)
get.addFamily(Bytes.toBytes("testFamily"))
val result=table.get(get)
get指定版本区间的数据:获取在指定时间戳区间内的数据
主要用到的API:
setTimeRange(long minStamp, long maxStamp)
但是如果使用这个函数,并不能得到这个区间内的全部数据,它只会返回在这个区间内最新的版本的一个数据,(注意这里是左闭右开区间)如下:
val table =new HTable(conf,tableName)
val get=new Get(Bytes.toBytes("row"))
get.setTimeRange(2l,300l) //这样的话 得到的是在这个区间内最新的版本的数据
get.addFamily(Bytes.toBytes(family))
val result=table.get(get)