Search in sources :

Example 1 with StructuredTableSchema

use of io.cdap.cdap.spi.data.table.StructuredTableSchema in project cdap by caskdata.

the class SpannerStructuredTableAdmin method loadSchema.

private StructuredTableSchema loadSchema(StructuredTableId tableId) throws TableNotFoundException {
    // Query the information_schema to reconstruct the StructuredTableSchema
    // See https://cloud.google.com/spanner/docs/information-schema for details
    Statement schemaStatement = Statement.newBuilder("SELECT C.column_name, C.spanner_type, I.index_type, I.ordinal_position FROM information_schema.columns C " + "LEFT JOIN information_schema.index_columns I " + "ON C.column_name = I.column_name AND C.table_name = I.table_name " + "WHERE C.table_name = @table_name ORDER BY C.ordinal_position").bind("table_name").to(tableId.getName()).build();
    List<FieldType> fields = new ArrayList<>();
    List<String> primaryKeys = new ArrayList<>();
    List<String> indexes = new ArrayList<>();
    try (ReadOnlyTransaction tx = databaseClient.readOnlyTransaction()) {
        try (ResultSet resultSet = tx.executeQuery(schemaStatement)) {
            while (resultSet.next()) {
                Struct row = resultSet.getCurrentRowAsStruct();
                String columnName = row.getString("column_name");
                String indexType = row.isNull("index_type") ? null : row.getString("index_type");
                // If a field is not a primary nor an index, the ordinal_position will be NULL in the index_columns table.
                boolean isIndex = !row.isNull("ordinal_position");
                fields.add(new FieldType(columnName, fromSpannerType(row.getString("spanner_type"))));
                if ("PRIMARY_KEY".equalsIgnoreCase(indexType)) {
                    primaryKeys.add(columnName);
                } else if ("INDEX".equalsIgnoreCase(indexType) && isIndex) {
                    indexes.add(columnName);
                }
            }
        }
    }
    if (fields.isEmpty()) {
        throw new TableNotFoundException(tableId);
    }
    return new StructuredTableSchema(tableId, fields, primaryKeys, indexes);
}
Also used : StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) Statement(com.google.cloud.spanner.Statement) ArrayList(java.util.ArrayList) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) Struct(com.google.cloud.spanner.Struct)

Example 2 with StructuredTableSchema

use of io.cdap.cdap.spi.data.table.StructuredTableSchema in project cdap by caskdata.

the class FieldValidatorTest method init.

@BeforeClass
public static void init() {
    StructuredTableSpecification spec = new StructuredTableSpecification.Builder().withId(SIMPLE_TABLE).withFields(Fields.intType(KEY), Fields.longType(KEY2), Fields.stringType(KEY3), Fields.stringType(STRING_COL)).withPrimaryKeys(KEY, KEY2, KEY3).build();
    schema = new StructuredTableSchema(spec);
}
Also used : StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) BeforeClass(org.junit.BeforeClass)

Example 3 with StructuredTableSchema

use of io.cdap.cdap.spi.data.table.StructuredTableSchema in project cdap by caskdata.

the class NoSqlStructuredTableContext method getTable.

@Override
public StructuredTable getTable(StructuredTableId tableId) throws StructuredTableInstantiationException, TableNotFoundException {
    try {
        StructuredTableSchema schema = tableAdmin.getSchema(tableId);
        Map<String, String> arguments = new HashMap<>();
        if (schema.getIndexes().isEmpty()) {
            // No indexes on the table
            arguments.put(IndexedTable.INDEX_COLUMNS_CONF_KEY, "");
            arguments.put(IndexedTable.DYNAMIC_INDEXING_PREFIX, "");
        } else {
            arguments.put(IndexedTable.INDEX_COLUMNS_CONF_KEY, Joiner.on(",").join(schema.getIndexes()));
            arguments.put(IndexedTable.DYNAMIC_INDEXING_PREFIX, tableId.getName());
        }
        StructuredTable table = new NoSqlStructuredTable(datasetContext.getDataset(NoSqlStructuredTableAdmin.ENTITY_TABLE_NAME, arguments), schema);
        return new MetricStructuredTable(tableId, table, metricsCollector, emitTimeMetrics);
    } catch (DatasetInstantiationException e) {
        throw new StructuredTableInstantiationException(tableId, String.format("Error instantiating table %s", tableId), e);
    }
}
Also used : StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) StructuredTableInstantiationException(io.cdap.cdap.spi.data.StructuredTableInstantiationException) HashMap(java.util.HashMap) MetricStructuredTable(io.cdap.cdap.spi.data.common.MetricStructuredTable) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) MetricStructuredTable(io.cdap.cdap.spi.data.common.MetricStructuredTable)

Aggregations

StructuredTableSchema (io.cdap.cdap.spi.data.table.StructuredTableSchema)3 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 Struct (com.google.cloud.spanner.Struct)1 DatasetInstantiationException (io.cdap.cdap.api.data.DatasetInstantiationException)1 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)1 StructuredTableInstantiationException (io.cdap.cdap.spi.data.StructuredTableInstantiationException)1 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)1 MetricStructuredTable (io.cdap.cdap.spi.data.common.MetricStructuredTable)1 StructuredTableSpecification (io.cdap.cdap.spi.data.table.StructuredTableSpecification)1 FieldType (io.cdap.cdap.spi.data.table.field.FieldType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BeforeClass (org.junit.BeforeClass)1