GridDB时序容器插入数据后查询报RowSet not initialized错误求助
GridDB Java查询时出现GSException: RowSet not initialized的解决方案
我正在开发基于GridDB的Java时序数据处理应用,目标是向时序容器插入记录,但在查询数据时持续遭遇GSException:RowSet not initialized错误。
问题代码
import com.toshiba.mwcloud.gs.*; public class GridDBTest { public static void main(String[] args) throws GSException { GridStoreFactory factory = GridStoreFactory.getInstance(); GridStore store = factory.getGridStore("host=127.0.0.1;port=10001;clusterName=myCluster;user=admin;password=admin"); // Define time-series container with timestamp and value columns ContainerInfo containerInfo = new ContainerInfo(); containerInfo.setType(ContainerType.TIME_SERIES); containerInfo.setRowKeyAssigned(true); containerInfo.setColumnInfoList(List.of( new ColumnInfo("timestamp", GSType.TIMESTAMP), new ColumnInfo("value", GSType.DOUBLE) )); TimeSeries<?> timeSeries = store.putTimeSeries("SampleData", containerInfo, true); // Insert data for (int i = 0; i < 10; i++) { Date timestamp = new Date(System.currentTimeMillis() - (i * 1000L)); Row row = timeSeries.createRow(); row.setTimestamp(0, timestamp); row.setDouble(1, Math.random() * 100); timeSeries.put(timestamp, row); } // Attempt to retrieve data RowSet<?> rs = timeSeries.query(new Date(System.currentTimeMillis() - 60000), new Date()).fetch(); while (rs.hasNext()) { Row row = rs.next(); System.out.println("Timestamp: " + row.getTimestamp(0) + ", Value: " + row.getDouble(1)); } store.close(); } }
错误信息
Exception in thread "main" com.toshiba.mwcloud.gs.GSException: RowSet not initialized at com.toshiba.mwcloud.gs.RowSetImpl.hasNext(RowSetImpl.java:48) ...
已尝试的排查步骤
- 验证连接信息(主机、端口、集群名称等)正确无误;
- 确认容器创建步骤无错误;
- 检查是否需在query()后立即调用fetch()初始化RowSet。
解决方案
这个错误的核心原因是时序容器查询结果为空时,直接调用hasNext()会触发未初始化异常,同时代码存在两处关键问题,修正如下:
增加RowSet有效性判断
GridDB的fetch()方法在查询无结果时会返回未初始化的RowSet,必须先通过isInitialized()判断状态,再调用遍历方法。修正时序数据插入API
使用timeSeries.put(timestamp, row)时,Row内已设置timestamp列,与时序容器的行键重复,直接调用timeSeries.put(row)即可完成插入。
修改后的完整代码
import com.toshiba.mwcloud.gs.*; import java.util.Date; import java.util.List; public class GridDBTest { public static void main(String[] args) throws GSException { GridStoreFactory factory = GridStoreFactory.getInstance(); GridStore store = factory.getGridStore("host=127.0.0.1;port=10001;clusterName=myCluster;user=admin;password=admin"); // Define time-series container with timestamp and value columns ContainerInfo containerInfo = new ContainerInfo(); containerInfo.setType(ContainerType.TIME_SERIES); containerInfo.setRowKeyAssigned(true); containerInfo.setColumnInfoList(List.of( new ColumnInfo("timestamp", GSType.TIMESTAMP), new ColumnInfo("value", GSType.DOUBLE) )); TimeSeries<?> timeSeries = store.putTimeSeries("SampleData", containerInfo, true); // Insert data - 修正put方法调用 for (int i = 0; i < 10; i++) { Date timestamp = new Date(System.currentTimeMillis() - (i * 1000L)); Row row = timeSeries.createRow(); row.setTimestamp(0, timestamp); row.setDouble(1, Math.random() * 100); timeSeries.put(row); // 直接传入row,无需重复传timestamp } // Retrieve data - 增加RowSet有效性判断 RowSet<?> rs = timeSeries.query(new Date(System.currentTimeMillis() - 60000), new Date()).fetch(); if (rs != null && rs.isInitialized()) { while (rs.hasNext()) { Row row = rs.next(); System.out.println("Timestamp: " + row.getTimestamp(0) + ", Value: " + row.getDouble(1)); } } else { System.out.println("No data found in the query range."); } store.close(); } }
额外排查建议
- 验证查询时间范围:打印查询起止时间与插入的timestamp,确认时间范围是否匹配;
- 检查数据插入状态:使用GridDB CLI工具
gs_sh连接集群,执行select * from SampleData;验证数据是否存在。
内容的提问来源于stack exchange,提问作者Ahmed Ben Khelifa
相关产品推荐
相关产品推荐

