3

我想获取数据库中的表列表。

tableList命令的文档说此方法返回一个字符串列表,但事实并非如此。它实际上返回一个TableList对象。

当我跑步时..

r.db("test").tableList().run(conn);

我得到一个Result<Object>对象作为结果,其中一个条目实际上是ArrayList我想要的所有表名。

那么,这实际上是现在应该如何工作的:

Connection connection = r.connection().hostname(DEFAULT_HOSTNAME).port(DEFAULT_PORT).connect();

Object tableListObject = r.db(DEFAULT_DB_NAME).tableList().run(connection).single();

if (tableListObject instanceof Collection) {
    List<?> tableList = new ArrayList<>((Collection<?>) tableListObject);

    for(Object tableName : tableList) {
        System.out.println(tableName);
    }
}

对我来说似乎相当复杂,有没有官方/更好的方法来做到这一点?

我正在使用 RethinkDB Java 驱动程序版本 2.4.4。

4

1 回答 1

1

您可以利用run()允许您指定返回结果的类的方法 - 在本例中为字符串数组:

Connection conn = r.connection().hostname("localhost").port(32769).connect();
List<?> tables = r.db("test").tableList().run(conn, ArrayList.class).first();
if (tables != null) {
    tables.forEach(System.out::println);
}

对我来说,这会在我的测试数据库中打印以下内容:

movies
tv_shows

更新为List<?>按照@Johannes 的建议使用。

于 2020-09-06T22:21:55.747 回答