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);
}
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);
}
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);
}
}
Aggregations