Search in sources :

Example 81 with Query

use of org.skife.jdbi.v2.Query in project beam by apache.

the class V1ReadIT method testE2EV1Read.

/**
 * An end-to-end test for {@link DatastoreV1.Read#withQuery(Query)}
 *
 * <p>Write some test entities to datastore and then run a pipeline that reads and counts the
 * total number of entities. Verify that the count matches the number of entities written.
 */
@Test
public void testE2EV1Read() throws Exception {
    // Read from datastore
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    DatastoreV1.Read read = DatastoreIO.v1().read().withProjectId(project).withQuery(query).withNamespace(options.getNamespace());
    // Count the total number of entities
    Pipeline p = Pipeline.create(options);
    PCollection<Long> count = p.apply(read).apply(Count.globally());
    PAssert.thatSingleton(count).isEqualTo(numEntities);
    p.run();
}
Also used : Query(com.google.datastore.v1.Query) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 82 with Query

use of org.skife.jdbi.v2.Query in project beam by apache.

the class V1TestUtil method deleteAllEntities.

/**
 * Delete all entities with the given ancestor.
 */
static void deleteAllEntities(V1TestOptions options, String project, String ancestor) throws Exception {
    Datastore datastore = getDatastore(options, project);
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    V1TestReader reader = new V1TestReader(datastore, query, options.getNamespace());
    V1TestWriter writer = new V1TestWriter(datastore, new DeleteMutationBuilder());
    long numEntities = 0;
    while (reader.advance()) {
        Entity entity = reader.getCurrent();
        numEntities++;
        writer.write(entity);
    }
    writer.close();
    LOG.info("Successfully deleted {} entities", numEntities);
}
Also used : Entity(com.google.datastore.v1.Entity) Datastore(com.google.datastore.v1.client.Datastore) Query(com.google.datastore.v1.Query)

Example 83 with Query

use of org.skife.jdbi.v2.Query in project beam by apache.

the class V1TestUtil method countEntities.

/**
 * Returns the total number of entities for the given datastore.
 */
static long countEntities(V1TestOptions options, String project, String ancestor) throws Exception {
    // Read from datastore.
    Datastore datastore = V1TestUtil.getDatastore(options, project);
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    V1TestReader reader = new V1TestReader(datastore, query, options.getNamespace());
    long numEntitiesRead = 0;
    while (reader.advance()) {
        reader.getCurrent();
        numEntitiesRead++;
    }
    return numEntitiesRead;
}
Also used : Datastore(com.google.datastore.v1.client.Datastore) Query(com.google.datastore.v1.Query)

Example 84 with Query

use of org.skife.jdbi.v2.Query in project killbill by killbill.

the class DatabaseExportDao method exportDataForAccountAndTable.

private void exportDataForAccountAndTable(final DatabaseExportOutputStream out, final List<ColumnInfo> columnsForTable, final InternalTenantContext context) {
    TableType tableType = TableType.OTHER;
    final String tableName = columnsForTable.get(0).getTableName();
    // Ignore casing (for H2)
    if (TableName.ACCOUNT.getTableName().equalsIgnoreCase(tableName)) {
        tableType = TableType.KB_ACCOUNT;
    } else if (TableName.ACCOUNT_HISTORY.getTableName().equalsIgnoreCase(tableName)) {
        tableType = TableType.KB_ACCOUNT_HISTORY;
    }
    boolean firstColumn = true;
    final StringBuilder queryBuilder = new StringBuilder("select ");
    for (final ColumnInfo column : columnsForTable) {
        if (!firstColumn) {
            queryBuilder.append(", ");
        } else {
            firstColumn = false;
        }
        queryBuilder.append(column.getColumnName());
        if (tableType == TableType.OTHER) {
            // Ignore casing (for H2)
            if (column.getColumnName().equalsIgnoreCase(TableType.KB_PER_ACCOUNT.getAccountRecordIdColumnName())) {
                tableType = TableType.KB_PER_ACCOUNT;
            } else if (column.getColumnName().equalsIgnoreCase(TableType.NOTIFICATION.getAccountRecordIdColumnName())) {
                tableType = TableType.NOTIFICATION;
            }
        }
    }
    // Don't export non-account specific tables
    if (tableType == TableType.OTHER) {
        return;
    }
    // Build the query - make sure to filter by account and tenant!
    queryBuilder.append(" from ").append(tableName).append(" where ").append(tableType.getAccountRecordIdColumnName()).append(" = :accountRecordId and ").append(tableType.getTenantRecordIdColumnName()).append("  = :tenantRecordId");
    // Notify the stream that we're about to write data for a different table
    out.newTable(tableName, columnsForTable);
    dbi.withHandle(new HandleCallback<Void>() {

        @Override
        public Void withHandle(final Handle handle) throws Exception {
            final ResultIterator<Map<String, Object>> iterator = handle.createQuery(queryBuilder.toString()).bind("accountRecordId", context.getAccountRecordId()).bind("tenantRecordId", context.getTenantRecordId()).iterator();
            try {
                while (iterator.hasNext()) {
                    final Map<String, Object> row = iterator.next();
                    for (final String k : row.keySet()) {
                        final Object value = row.get(k);
                        // See also LowerToCamelBeanMapper
                        if (value instanceof Blob) {
                            final Blob blob = (Blob) value;
                            row.put(k, blob.getBytes(0, (int) blob.length()));
                        } else if (value instanceof Clob) {
                            // TODO Update LowerToCamelBeanMapper?
                            final Clob clob = (Clob) value;
                            row.put(k, clob.getSubString(1, (int) clob.length()));
                        }
                    }
                    try {
                        out.write(row);
                    } catch (final IOException e) {
                        logger.warn("Unable to write row: {}", row, e);
                        throw e;
                    }
                }
            } finally {
                iterator.close();
            }
            return null;
        }
    });
}
Also used : Blob(java.sql.Blob) ResultIterator(org.skife.jdbi.v2.ResultIterator) ColumnInfo(org.killbill.billing.util.api.ColumnInfo) DefaultColumnInfo(org.killbill.billing.util.validation.DefaultColumnInfo) IOException(java.io.IOException) IOException(java.io.IOException) Handle(org.skife.jdbi.v2.Handle) Clob(java.sql.Clob) Map(java.util.Map)

Aggregations

Query (org.jpl7.Query)56 Term (org.jpl7.Term)33 Variable (org.jpl7.Variable)23 Map (java.util.Map)18 Atom (org.jpl7.Atom)16 Compound (org.jpl7.Compound)16 Query (com.google.datastore.v1.Query)12 Handle (org.skife.jdbi.v2.Handle)12 Test (org.junit.Test)10 GqlQuery (com.google.datastore.v1.GqlQuery)7 Integer (org.jpl7.Integer)6 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 Datastore (com.google.datastore.v1.client.Datastore)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 SplitQueryFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.SplitQueryFn)4 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)3 Nullable (javax.annotation.Nullable)3