https://github.com/onozaty/postgresql-copy-helper
https://github.com/onozaty/postgresql-copy-helper
java postgresql
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/onozaty/postgresql-copy-helper
- Owner: onozaty
- License: apache-2.0
- Created: 2020-02-01T05:08:40.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-17T15:49:11.000Z (over 6 years ago)
- Last Synced: 2024-12-16T06:32:19.769Z (over 1 year ago)
- Topics: java, postgresql
- Language: Java
- Size: 103 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.ja.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# PostgreSQL COPY Helper
[](https://maven-badges.herokuapp.com/maven-central/com.github.onozaty/postgresql-copy-helper)
PostgreSQL COPY Helper は、PostgreSQLのCOPYコマンドを利用するためのヘルパクラスを提供するJava用ライブラリです。
オブジェクトの一覧を簡単かつ高速にインポートできます。
PostgreSQLのCOPYコマンドは、INSERTと比べてとても高速に動作します。
* [PostgreSQL: Documentation: COPY](https://www.postgresql.org/docs/current/sql-copy.html)
## セットアップ
PostgreSQL COPY Helper は jCenter および Maven Central で公開されています。
依存関係に追加するだけで利用可能です。
### maven
```xml
com.github.onozaty
postgresql-copy-helper
1.1.0
pom
```
### Gradle
```groovy
implementation 'com.github.onozaty:postgresql-copy-helper:1.1.0'
```
## 利用方法
登録したいデータを表すクラスを定義します。
`@Table`で対象のテーブル名、`@Column`でカラム名を指定します。
```java
package postgresql.copy.helper.example;
import com.github.onozaty.postgresql.copy.bean.Column;
import com.github.onozaty.postgresql.copy.bean.Table;
@Table("items")
public class Item {
@Column("id")
private final int id;
@Column("name")
private final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
```
`CopyHelper.copyFrom`で対象のデータリストを指定するだけで、COPYコマンドが実行されます。
```java
BaseConnection connection = (BaseConnection) DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
List items = generateItems();
CopyHelper.copyFrom(connection, items, Item.class);
```
## パフォーマンスについて
下記のプロジェクトにて、COPYコマンドのパフォーマンスを測定しています。
* https://github.com/onozaty/java-sandbox/tree/master/postgresql-copy-helper-example
100,000件で、それぞれ下記のような時間となっています。
| パターン | 時間(msec) |
|---------|----------:|
| 1件ずつINSERT | 12,000 |
| Batch INSERT | 1,000 |
| COPYコマンド(本ライブラリを利用) | 100 |