从RethinkDB Count对象中提取数据

问题描述 投票:1回答:1

我有一个JavaScript RethinkDB查询,如下所示:

r.db('test')
  .table('trolleys')
  .eqJoin(r.row('currect_location')('_id'),r.table('bufferzone'))
  .zip()
  .filter(r.row('currect_destination')('_id').ne('DEPOT'))
  .group('id')
  .count()

该查询产生以下输出,这正是我正在寻找的

[
  {
    "group":  "a" ,
    "reduction": 2
  },
  {
    "group":  "b" ,
    "reduction": 1
  }
]

但是,在Java中执行相同的查询时:

Count countObj = r.db("test")
    .table("trolleys")
    .eqJoin(row -> row.g("currect_location").g("_id"), r.table("bufferzone"))
    .zip()
    .filter(row -> row.g("currect_destination").g("_id").ne("DEPOT"))
    .group("id")
    .count();

我得到一个Count对象作为响应,但是如何从Java中的Count对象中提取类似于我在JavaScript案例中获得的数据?

java rethinkdb
1个回答
0
投票

我发现了这个问题。解决方案是在查询结束时调用run()方法。从查询返回的类型实际上在发布的问题中也是错误的。查询返回Count列表,而不是GroupedResults对象,即List<GroupedResult>。为了完整起见,这里给出了完整的解决方案:

import java.util.List;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.model.GroupedResult;
import com.rethinkdb.net.Connection;

public class EmptyTrolleysInBufferzone {

    public static final RethinkDB r = RethinkDB.r;

    public static void main(String[] args) {

        Connection conn = r.connection().hostname("localhost").port(28015).connect();

        List<GroupedResult> response = r.db("test")
            .table("trolleys")
            .eqJoin(row -> row.g("currect_location").g("_id"), r.table("bufferzone"))
            .zip().filter(row -> row.g("currect_destination").g("_id").ne("DEPOT"))
            .group("id")
            .count()
            .run(conn);

        for (GroupedResult g : response) {
            System.out.println(g.group + " = " + g.values.toString());
        }

        conn.close();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.