Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Added docs for vertx pg client for Java #13567

Merged
merged 12 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ycql-4.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ycql.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-ebean.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-jdbc-ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link active">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-sdyb.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
214 changes: 214 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-vertx-pg-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
---
title: Build a Java application that uses YSQL
headerTitle: Build a Java application
linkTitle: Java
description: Build a sample Java application with the Vertx PG Client and use the YSQL API to connect to and interact with YugabyteDB.
menu:
preview:
parent: build-apps
name: Java
identifier: java-10
weight: 550
type: page
isTocNested: true
showAsideToc: true
---

<ul class="nav nav-tabs-alt nav-tabs-yb">
<li >
<a href="../ysql-yb-jdbc/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - YB - JDBC
</a>
</li>
<li >
<a href="../ysql-jdbc/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link active">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - JDBC SSL/TLS
</a>
</li>
<li >
<a href="../ysql-hibernate/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Hibernate
</a>
</li>
<li >
<a href="../ysql-sdyb/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Spring Data YugabyteDB
</a>
</li>
<li >
<a href="../ysql-spring-data/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Spring Data JPA
</a>
</li>
<li>
<a href="../ysql-ebean/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Ebean
</a>
</li>
<li>
<a href="../ycql/" class="nav-link">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL
</a>
</li>
<li>
<a href="../ycql-4.6/" class="nav-link">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL (4.6)
</a>
</li>
</ul>

[Vertx PG client](https://vertx.io/docs/vertx-pg-client/java/) is the client for PostgreSQL with the simple APIs for communicating with the Database. It is reactive and non-blocking client for handling the database connections with single threaded API.
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

As this client is for PostgreSQL and YugabyteDB is wire compatible with PostgreSQL, it is also supported by YugabyteDB with one [limitation](#limitation).
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

## Prerequisites

This tutorial assumes that:

- YugabyteDB is up and running. If you are new to YugabyteDB, you can download, install, and have YugabyteDB up and running within five minutes by following the steps in [Quick start](../../../../quick-start/).
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved
- Java Development Kit (JDK) 1.8, or later, is installed.
- [Apache Maven](https://maven.apache.org/index.html) 3.3 or later, is installed.

## Create and configure the Java project

1. Create a project called "MySample".

```sh
$ mvn archetype:generate \
-DgroupId=com.yugabyte \
-DartifactId=vertx-pg-example \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

$ cd vertx-pg-example
```

1. Add the following dependency for the vertx pg client within the `<dependencies>` element in the `pom.xml` file.
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

```xml
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-pg-client</artifactId>
<version>4.3.2</version>
</dependency>
```

1. Install the added dependency.

```sh
$ mvn install
```

## Create the sample Java application

1. Copy the following Java code to a new file named `src/main/java/com/yugabyte/vertxPgExample.java`:
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

```java
package com.yugabyte;

import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowStream;

public class vertxPgExample {
public static void main(String[] args) {

PgConnectOptions options = new PgConnectOptions()
.setPort(5433)
.setHost("127.0.0.1")
.setDatabase("yugabyte")
.setUser("yugabyte")
.setPassword("yugabyte");

Vertx vertx = Vertx.vertx();
// creating the PgPool with configuration as option and maxsize 10.
PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(10));

//getting a connection from the pool and running the example on that
pool.getConnection().compose(connection -> {
Promise<Void> promise = Promise.promise();
// create a test table
connection.query("create table test(id int primary key, name text)").execute()
.compose(v -> {
// insert some test data
return connection.query("insert into test values (1, 'Hello'), (2, 'World'), (3,'Example'), (4, 'Vertx'), (5, 'Yugabyte')").execute();
})
.compose(v -> {
// prepare the query
return connection.prepare("select * from test order by id");
})
.map(preparedStatement -> {
// create a stream for the prepared statement
return preparedStatement.createStream(50, Tuple.tuple());
})
.onComplete(ar -> {
if (ar.succeeded()) {
RowStream<Row> stream = ar.result();
stream
.exceptionHandler(promise::fail)
.endHandler(promise::complete)
.handler(row -> System.out.println(row.toJson())); // Printing each row as JSON
} else {
promise.fail(ar.cause());
}
});
return promise.future().onComplete(v -> {
// close the connection
connection.close();
});
}).onComplete(ar -> {
if (ar.succeeded()) {
System.out.println("Example ran successfully!");
} else {
ar.cause().printStackTrace();
}
});

}
}
```

1. Run your new program.
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

```sh
$ mvn -q package exec:java -DskipTests -Dexec.mainClass=com.yugabyte.vertxPgExample
```

You should see the following as the output:

```output
{"id":1,"name":"Hello"}
{"id":2,"name":"World"}
{"id":3,"name":"Example"}
{"id":4,"name":"Vertx"}
{"id":5,"name":"Yugabyte"}
Example ran successfully!
```
## Limitation

[PubSub](https://vertx.io/docs/vertx-pg-client/java/#_pubsub) feature of Vertx PG client is currently not supported with YugabyteDB.
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions docs/content/preview/develop/build-apps/java/ysql-yb-jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type: docs
YSQL - JDBC
</a>
</li>
<li >
<a href="../ysql-vertx-pg-client/" class="nav-link ">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL - Vertx PG Client
</a>
</li>
<li >
<a href="../ysql-jdbc-ssl/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
Expand Down
8 changes: 8 additions & 0 deletions docs/content/preview/reference/drivers/ysql-client-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ To download binary JAR files, go to [PostgreSQL JDBC driver – Downloads](https

To get the latest versions for projects using [Apache Maven](https://maven.apache.org), see [Maven Central Repository Search](https://search.maven.org/artifact/org.postgresql/postgresql/42.2.14.jre7/jar).

### Vertx PG Client

[Vertx PG client](https://vertx.io/docs/vertx-pg-client/java/) is the client for PostgreSQL with the simple APIs for communicating with the Database. It is reactive and non-blocking client for handling the database connections with single threaded API.
priyanshi-yb marked this conversation as resolved.
Show resolved Hide resolved

For a tutorial on building a sample Java application with the Vertx PG Client, see [Build a Java application](../../../develop/build-apps/java/ysql-vertx-pg-client/).

To get the latest versions for projects using [Apache Maven](https://maven.apache.org), see [Maven Central Repository of Vertx PG Client](https://mvnrepository.com/artifact/io.vertx/vertx-pg-client).

## Node.JS

### node-postgres
Expand Down