-
Notifications
You must be signed in to change notification settings - Fork 24
聚合操作接口
IHEII edited this page Jul 27, 2023
·
2 revisions
聚合操作可以帮助您在OBKV上获取一张表的一些统计量,目前聚合支持:
- 最小值
- 最大值
- 计数
- 求和
- 求均值
一个简单的例子如下:
// 1. 创建客户端
ObTableClient obTableClient = new ObTableClient();
obTableClient.setFullUserName("full_user_name");
obTableClient.setParamURL("param_url");
obTableClient.setPassword("password");
obTableClient.setSysUserName("sys_user_name");
obTableClient.setSysPassword("sys_user_passwd");
obTableClient.init();
// 2. 设置聚合表的主键
obTableClient.addRowKeyElement("test_aggregation", new String[]{"c1"});
// 3. 准备数据
SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
Date date1 = sdf.parse(" 2001-07-10 19:20:00 ");
Date date2 = sdf.parse(" 2002-07-10 19:20:00 ");
Date date3 = sdf.parse(" 2003-07-10 19:20:00 ");
obTableClient.insert("test_aggregation", "first_row", new String[] { "c2", "c3", "c4", "c5",
"c6", "c7" }, new Object[] { 1, 1L, 1.0f, 1.0, (byte) 1, date1 });
obTableClient.insert("test_aggregation", "second_row", new String[] { "c2", "c3", "c4", "c5",
"c6", "c7" }, new Object[] { 2, 2L, 2.0f, 2.0, (byte) 2, date2 });
obTableClient.insert("test_aggregation", "third_row", new String[] { "c2", "c3", "c4", "c5",
"c6", "c7" }, new Object[] { 3, 3L, 3.0f, 3.0, (byte) 3, date3 });
// 4. 创建 aggregation
ObTableAggregation obtableAggregation = client.aggregate("test_aggregation");
// 聚合加速或者在分区表上进行聚合需要设置范围
obtableAggregation.addScanRange(new Object[] { 0L }, new Object[] { 150L });
// 5. 设置聚合操作
obtableAggregation.max("c2");
obtableAggregation.min("c2");
obtableAggregation.count();
obtableAggregation.sum("c2");
obtableAggregation.avg("c2");
// 6. 执行
ObTableAggregationResult obtableAggregationResult = obtableAggregation.execute();
// 7. 获取结果
System.out.println(obtableAggregationResult.get("max(c2)");
System.out.println(obtableAggregationResult.get("min(c2)");
System.out.println(obtableAggregationResult.get("count(*)");
System.out.println(obtableAggregationResult.get("sum(c2)");
System.out.println(obtableAggregationResult.get("avg(c2)");
更多支持的接口及参数可以了解 Aggregation,同时我们也提供了简单的例子,了解更多可以浏览 例子
目前OBKV只支持单分区的聚合,因为多分区的聚合会存在数据不一致的问题,聚合得到的统计量并不是某一时刻的结果.
如果您需要在分区表上进行聚合,需要通过addScanRange()
合理的设置您的分区键范围.