
jsonStore 应声明为 DocumentStore 类型,这是 OJAI API 中提供 query()、find()(返回 QueryResult,即 DocumentStream 子类)及 getAllDocumentsAsStream() 等核心数据访问方法的标准接口。
`jsonstore` 应声明为 `documentstore` 类型,这是 ojai api 中提供 `query()`、`find()`(返回 `queryresult`,即 `documentstore` 中 `queryresult` 是 `documentstream` 的子类)及 `getalldocumentsasstream()` 等核心数据访问方法的标准接口。
在基于 MapR-DB(现为 MapR Data Platform)的 OJAI(Open JSON Application Interface)开发中,DocumentStore 是执行文档级 CRUD 和查询操作的核心接口。它抽象了底层存储(如 MapR-DB 表或 JSON 文件系统),并统一暴露标准方法:
-
query(QueryCondition condition)→ 返回List<document></document> -
find(QueryCondition condition)→ 返回QueryResult(实现了DocumentStream,支持流式迭代) -
getAllDocumentsAsStream(String... fields)→ 返回DocumentStream,用于全量扫描指定字段
⚠️ 注意:原始代码中调用的 jsonStore.queryAsStream(condition) 并非 OJAI 标准 API —— 正确方式是使用 jsonStore.find(condition),其返回值 QueryResult 可直接赋值给 DocumentStream 类型变量,例如:
@Repository
public class AccountRepository extends JsonRepository {
// ✅ 正确声明:DocumentStore 是唯一支持 query/find/getAllDocumentsAsStream 的接口
public DocumentStore jsonStore;
public DocumentStream accountsAsStream(List<string> ids) {
QueryCondition condition = QueryCondition.create()
.in("id", ids); // 注意:字段名应为字符串(如 "id"),而非数字 123
// ✅ 使用 find() 替代已废弃/不存在的 queryAsStream()
return jsonStore.find(condition);
}
public DocumentStream accountIdsAsStream() {
// ✅ getAllDocumentsAsStream 支持投影字段(可选)
return jsonStore.getAllDocumentsAsStream("id");
}
}</string>
此外,DocumentStore 实例通常通过依赖注入获取(如 Spring Data MapR 或手动构建):
// 示例:通过 Connection 获取 DocumentStore(需配置 MapR 集群连接)
Connection connection = OjaiConnectionBuilder.connect("mapr:///");
jsonStore = connection.getStore("/tables/accounts"); // 指向 MapR-DB 表路径
? 关键总结:
- 不要将
jsonStore声明为Object或模糊类型,必须明确为org.ojai.store.DocumentStore; - 避免使用不存在的方法名(如
queryAsStream),以find()作为流式查询的唯一标准入口; - 查询条件构造请使用
QueryCondition.create()链式 API,并确保字段名与实际 JSON schema 一致; - 所有
DocumentStream实现(包括QueryResult)均支持forEach()、iterator()和close(),务必在使用后显式关闭以释放资源。











