-1

I'm trying to write a simple function which will connect to postgres and execute a select statement.

PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(
        PostgresqlConnectionConfiguration.builder()
            .host("localhost")
            .port(5432)
            .database("MyDB")
            .username("username")
            .password("password").build());



DatabaseClient client = DatabaseClient.create(connectionFactory);

Flux<Map<String, Object>> result = client.execute("select * from table").fetch().all();

result.map(s -> {
  System.out.println(s);
  return null;
});

The above piece of code isn't printing anything. There is no error as well. I can connect to DB using the same credentials. What is missing in the code to stream data from DB?

Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. Also check the [help/on-topic] to see what questions you can ask. Please explain what your problem is and what exactly you mean with "I got stuck". – Progman Jun 18 '20 at 14:32
  • @Progman Edited the question and updated the code. Thanks for pointing out the mistake while prosting the question. – Santosh Hegde Jun 18 '20 at 15:03
  • 1
    It looks like you are missing a `subscribe()`. You have configured the `Flux` object, but you are not reading from it. Does it work when you use `subscribe()` on the return value of `map()`? – Progman Jun 18 '20 at 15:19

1 Answers1

1

Create the configuration class which is similar to the below code to connect to the PostgreSQL database

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {

    @Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/DATABASE_NAME");
    }

}
Thirumal
  • 8,280
  • 11
  • 53
  • 103