3

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.

Johannes
  • 828
  • 12
  • 29
  • @DevilsHnd Are you aware that your link is sending me to this very question? .. – Johannes Sep 06 '20 at 10:18
  • 1
    My apologies @Johannes, I wasn't aware of that and now I can't find the link intended. :( I believe I originally got it from this [ReThinkDB web page](https://rethinkdb.com/api/java/table_list/) but can seem to dig it up any more. There were few answers within the SO thread which may of helped. Again, my apologies. – DevilsHnd - 退職した Sep 06 '20 at 16:31
  • @DevilsHnd No worries.. – Johannes Sep 06 '20 at 20:48

1 Answers1

1

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.

Johannes
  • 828
  • 12
  • 29
andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • That's much better.. maybe even use `List> tables = ...` to avoid the unchecked cast warning. – Johannes Sep 07 '20 at 06:45
  • Yes - thank you for pointing that out. I had somehow managed to disable that warning check in my IDE. I would never have noticed... – andrewJames Sep 07 '20 at 13:22