gpt4 book ai didi

java - 如何从 `txn.getAll("Entity")` 方法获取子集结果?

转载 作者:行者123 更新时间:2023-12-02 10:54:55 25 4
gpt4 key购买 nike

这是获取实体列表的示例代码,示例用户<​​em>s:

final EntityIterable allUsers = txn.getAll("User); // returns million records for example
for (Entity user: allUsers) {
System.out.println(user.getProperty("fullName"));
}

我的问题,如果用户存储有数百万条记录,我们如何才能只获取结果的子集?就像拥有跳过限制一样,这可以通过Xodus API实现吗?还是我们真的需要对其进行编程以迭代所有条目并手动进行分区?在 Xodus 中获取实体子集的最有效和最快的方法是什么?

这是我们正在尝试解决的完整示例实现:

@Override
public List<User> listUsers(String instance, final String storeName, long skip, long limit) {
final List<User> users = new LinkedList<>();
final PersistentEntityStore entityStore = PersistentEntityStores.newInstance(xodusRoot + instance);
try {
entityStore.executeInTransaction(new StoreTransactionalExecutable() {
@Override
public void execute(@NotNull final StoreTransaction txn) {
final EntityIterable allUsers = txn.getAll(storeName);
// TODO: How to skip/limit with 100k to millions of records, efficiently
for (Entity userEntity : allUsers) {
User user = new User();
user.setEntityId((String) userEntity.getId().toString());
user.setUsername((String) userEntity.getProperty("username"));
users.add(user);
}
}
});
} finally {
entityStore.close();
}
return users;
}

最佳答案

只需评估 txn.getAll(storeName).skip(skip).take(limit) 就足够了。你会得到一个EntityIterable ( Iterable<Entity ) 实例,然后您可以将其转换为用户集合。

关于java - 如何从 `txn.getAll("Entity")` 方法获取子集结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51847729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com