@@ -6,7 +6,7 @@ author: saimicrosoft
6
6
ms.service : postgresql
7
7
ms.subservice : hyperscale-citus
8
8
ms.topic : how-to
9
- ms.date : 06/20 /2022
9
+ ms.date : 07/26 /2022
10
10
---
11
11
12
12
# Java app to connect and query Hyperscale (Citus)
@@ -71,6 +71,12 @@ Using your favorite IDE, create a new Java project with groupId **test** and art
71
71
<artifactId >postgresql</artifactId >
72
72
<version >42.2.12</version >
73
73
</dependency >
74
+ <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
75
+ <dependency >
76
+ <groupId >com.zaxxer</groupId >
77
+ <artifactId >HikariCP</artifactId >
78
+ <version >5.0.0</version >
79
+ </dependency >
74
80
<dependency >
75
81
<groupId >org.junit.jupiter</groupId >
76
82
<artifactId >junit-jupiter-params</artifactId >
@@ -101,6 +107,7 @@ This file configures [Apache Maven](https://maven.apache.org/) to use:
101
107
Create a ` src/main/resources/application.properties ` file, and add:
102
108
103
109
``` properties
110
+ driver.class.name =org.postgresql.Driver
104
111
url =jdbc:postgresql://<host>:5432/citus?ssl =true&sslmode =require
105
112
user =citus
106
113
password =<password>
@@ -142,6 +149,61 @@ select create_distributed_table('public.pharmacy','pharmacy_id');
142
149
143
150
Next, add the Java code that will use JDBC to store and retrieve data from your Hyperscale (Citus) server group.
144
151
152
+ #### Connection Pooling Setup
153
+
154
+ Using the code below, create a ` DButil.java ` file, which contains the ` DButil `
155
+ class. The ` DBUtil ` class sets up a connection pool to PostgreSQL using
156
+ [ HikariCP] ( https://github.com/brettwooldridge/HikariCP ) . In the example
157
+ application, we'll be using this class to connect to PostgreSQL and start
158
+ querying.
159
+
160
+ [ !INCLUDE[ why-connection-pooling] ( includes/why-connection-pooling.md )]
161
+
162
+ ``` java
163
+ // DButil.java
164
+ package test.crud ;
165
+
166
+ import java.io.FileInputStream ;
167
+ import java.io.IOException ;
168
+ import java.sql.SQLException ;
169
+ import java.util.Properties ;
170
+
171
+ import javax.sql.DataSource ;
172
+
173
+ import com.zaxxer.hikari.HikariDataSource ;
174
+
175
+ public class DButil {
176
+ private static final String DB_USERNAME = " db.username" ;
177
+ private static final String DB_PASSWORD = " db.password" ;
178
+ private static final String DB_URL = " db.url" ;
179
+ private static final String DB_DRIVER_CLASS = " driver.class.name" ;
180
+ private static Properties properties = null ;
181
+ private static HikariDataSource datasource;
182
+
183
+ static {
184
+ try {
185
+ properties = new Properties ();
186
+ properties. load(new FileInputStream (" src/main/java/application.properties" ));
187
+
188
+ datasource = new HikariDataSource ();
189
+ datasource. setDriverClassName(properties. getProperty(DB_DRIVER_CLASS ));
190
+ datasource. setJdbcUrl(properties. getProperty(DB_URL ));
191
+ datasource. setUsername(properties. getProperty(DB_USERNAME ));
192
+ datasource. setPassword(properties. getProperty(DB_PASSWORD ));
193
+ datasource. setMinimumIdle(100 );
194
+ datasource. setMaximumPoolSize(1000000000 );
195
+ datasource. setAutoCommit(false );
196
+ datasource. setLoginTimeout(3 );
197
+ } catch (IOException | SQLException e) {
198
+ e. printStackTrace();
199
+ }
200
+ }
201
+ public static DataSource getDataSource () {
202
+ return datasource;
203
+ }
204
+ }
205
+ ```
206
+
145
207
Create a ` src/main/java/DemoApplication.java ` file that contains:
146
208
147
209
``` java
@@ -168,11 +230,9 @@ public class DemoApplication {
168
230
}
169
231
public static void main (String [] args )throws Exception
170
232
{
171
- log. info(" Loading application properties" );
172
- Properties properties = new Properties ();
173
- properties. load(DemoApplication . class. getClassLoader(). getResourceAsStream(" application.properties" ));
174
233
log. info(" Connecting to the database" );
175
- Connection connection = DriverManager . getConnection(properties. getProperty(" url" ), properties);
234
+ Connection connection = DButil . getDataSource(). getConnection();
235
+ System . out. println(" The Connection Object is of Class: " + connection. getClass());
176
236
log. info(" Database connection test: " + connection. getCatalog());
177
237
log. info(" Creating table" );
178
238
log. info(" Creating index" );
@@ -272,12 +332,12 @@ public class Pharmacy {
272
332
}
273
333
@Override
274
334
public String toString () {
275
- return " TPharmacy{"
276
- " pharmacy_id=" + pharmacy_id
277
- " , pharmacy_name='" + pharmacy_name + ' \' '
278
- " , city='" + city + ' \' '
279
- " , state='" + state + ' \' '
280
- " , zip_code='" + zip_code + ' \' '
335
+ return " TPharmacy{" +
336
+ " pharmacy_id=" + pharmacy_id +
337
+ " , pharmacy_name='" + pharmacy_name + ' \' ' +
338
+ " , city='" + city + ' \' ' +
339
+ " , state='" + state + ' \' ' +
340
+ " , zip_code='" + zip_code + ' \' ' +
281
341
' }' ;
282
342
}
283
343
}
@@ -472,14 +532,17 @@ The following code is an example for copying data from a CSV file to a database
472
532
It requires the file [ pharmacies.csv] ( https://download.microsoft.com/download/d/8/d/d8d5673e-7cbf-4e13-b3e9-047b05fc1d46/pharmacies.csv ) .
473
533
474
534
``` java
475
- public static long copyFromFile( Connection connection, String filePath, String tableName)
476
- throws SQLException , IOException
477
- {
535
+ public static long
536
+ copyFromFile( Connection connection, String filePath, String tableName)
537
+ throws SQLException , IOException {
478
538
long count = 0 ;
479
539
FileInputStream fileInputStream = null ;
480
540
481
541
try {
482
- CopyManager copyManager = new CopyManager ((BaseConnection ) connection);
542
+ Connection unwrap = connection. unwrap(Connection . class);
543
+ BaseConnection connSec = (BaseConnection ) unwrap;
544
+
545
+ CopyManager copyManager = new CopyManager ((BaseConnection ) connSec);
483
546
fileInputStream = new FileInputStream (filePath);
484
547
count = copyManager. copyIn(" COPY " + tableName + " FROM STDIN delimiter ',' csv" , fileInputStream);
485
548
} finally {
@@ -531,16 +594,20 @@ The following code is an example for copying in-memory data to table.
531
594
``` java
532
595
private static void inMemory(Connection connection) throws SQLException ,IOException {
533
596
log. info(" Copying in-memory data into table" );
534
- String [] input = {" 5000,Target,Sunnyvale,California,94001" };
597
+ String [] input = {" 0,Target,Sunnyvale,California,94001" };
598
+
599
+ Connection unwrap = connection. unwrap(Connection . class);
600
+ BaseConnection connSec = (BaseConnection ) unwrap;
535
601
536
- CopyManager copyManager = new CopyManager ((BaseConnection ) connection );
537
- String copyCommand = " COPY pharmacy FROM STDIN with csv " ;
602
+ CopyManager copyManager = new CopyManager ((BaseConnection ) connSec );
603
+ String copyCommand = " COPY pharmacy FROM STDIN with csv" ;
538
604
539
605
for (String var : input)
540
606
{
541
607
Reader reader = new StringReader (var );
542
608
copyManager. copyIn(copyCommand, reader);
543
609
}
610
+
544
611
copyManager. copyIn(copyCommand);
545
612
}
546
613
```
0 commit comments