Java Code Examples for org.jdbi.v3.core.Handle#prepareBatch()

The following examples show how to use org.jdbi.v3.core.Handle#prepareBatch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBResourceService.java    From trellis with Apache License 2.0 6 votes vote down vote up
private static void batchUpdateTriples(final Handle handle, final int resourceId, final String table,
        final Graph graph, final int batchSize) {
    final String query
        = "INSERT INTO " + table + " (resource_id, subject, predicate, object, lang, datatype) "
        + "VALUES (?, ?, ?, ?, ?, ?)";
    try (final PreparedBatch batch = handle.prepareBatch(query)) {
        graph.stream().sequential().forEach(triple -> {
            batch.bind(0, resourceId)
                 .bind(1, ((IRI) triple.getSubject()).getIRIString())
                 .bind(2, triple.getPredicate().getIRIString())
                 .bind(3, getObjectValue(triple.getObject()))
                 .bind(4, getObjectLang(triple.getObject()))
                 .bind(5, getObjectDatatype(triple.getObject())).add();
            if (batch.size() >= batchSize) {
                batch.execute();
            }
        });
        if (batch.size() > 0) {
            batch.execute();
        }
    }
}
 
Example 2
Source File: H2QueryRunner.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data)
{
    List<ColumnMetadata> columns = tableMetadata.getColumns().stream()
            .filter(columnMetadata -> !columnMetadata.isHidden())
            .collect(toImmutableList());

    String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
    String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);

    RecordCursor cursor = data.cursor();
    while (true) {
        // insert 1000 rows at a time
        PreparedBatch batch = handle.prepareBatch(sql);
        for (int row = 0; row < 1000; row++) {
            if (!cursor.advanceNextPosition()) {
                if (batch.size() > 0) {
                    batch.execute();
                }
                return;
            }
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    batch.bind(column, cursor.getBoolean(column));
                }
                else if (BIGINT.equals(type)) {
                    batch.bind(column, cursor.getLong(column));
                }
                else if (INTEGER.equals(type)) {
                    batch.bind(column, toIntExact(cursor.getLong(column)));
                }
                else if (DOUBLE.equals(type)) {
                    batch.bind(column, cursor.getDouble(column));
                }
                else if (type instanceof VarcharType) {
                    batch.bind(column, cursor.getSlice(column).toStringUtf8());
                }
                else if (DATE.equals(type)) {
                    long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
                    // H2 expects dates in to be millis at midnight in the JVM timezone
                    long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millisUtc);
                    batch.bind(column, new Date(localMillis));
                }
                else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
            batch.add();
        }
        batch.execute();
    }
}