javarethinkdbreql

Retrieve list of table names


I want to get the list of tables in a database.

The documentation of the tableList command says this method returns a list of strings, but that is not exactly the case. It actually returns a TableList object.

And when I run..

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

I get a Result<Object> object as result with one entry that is really an ArrayList with all table names I want.

So, is this actually how this is supposed to work now:

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);
    }
}

Seems rather complicated to me, is there an official/better way to do this?

I am using the RethinkDB Java driver version 2.4.4.


Solution

  • You can take advantage of the run() method which allows you to specify the class of the returned results - in this case an array of strings:

    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);
    }
    

    For me, this prints the following in my test DB:

    movies
    tv_shows
    

    Updated to use List<?> as suggested by @Johannes.