use of com.datastax.driver.core.TableMetadata in project presto by prestodb.
the class CassandraTokenSplitManager method checkSizeEstimatesTableExist.
private void checkSizeEstimatesTableExist() {
KeyspaceMetadata ks = session.executeWithSession(session -> session.getCluster().getMetadata().getKeyspace(SYSTEM));
checkState(ks != null, "system keyspace metadata must not be null");
TableMetadata table = ks.getTable(SIZE_ESTIMATES);
if (table == null) {
throw new PrestoException(NOT_SUPPORTED, "Cassandra versions prior to 2.1.5 are not supported");
}
}
use of com.datastax.driver.core.TableMetadata in project presto by prestodb.
the class NativeCassandraSession method getTable.
@Override
public CassandraTable getTable(SchemaTableName tableName) throws TableNotFoundException {
TableMetadata tableMeta = getTableMetadata(tableName);
List<String> columnNames = new ArrayList<>();
for (ColumnMetadata columnMetadata : tableMeta.getColumns()) {
columnNames.add(columnMetadata.getName());
}
// check if there is a comment to establish column ordering
String comment = tableMeta.getOptions().getComment();
Set<String> hiddenColumns = ImmutableSet.of();
if (comment != null && comment.startsWith(PRESTO_COMMENT_METADATA)) {
String columnOrderingString = comment.substring(PRESTO_COMMENT_METADATA.length());
// column ordering
List<ExtraColumnMetadata> extras = extraColumnMetadataCodec.fromJson(columnOrderingString);
List<String> explicitColumnOrder = new ArrayList<>(ImmutableList.copyOf(transform(extras, ExtraColumnMetadata::getName)));
hiddenColumns = ImmutableSet.copyOf(transform(filter(extras, ExtraColumnMetadata::isHidden), ExtraColumnMetadata::getName));
// add columns not in the comment to the ordering
Iterables.addAll(explicitColumnOrder, filter(columnNames, not(in(explicitColumnOrder))));
// sort the actual columns names using the explicit column order (this allows for missing columns)
columnNames = Ordering.explicit(explicitColumnOrder).sortedCopy(columnNames);
}
ImmutableList.Builder<CassandraColumnHandle> columnHandles = ImmutableList.builder();
// add primary keys first
Set<String> primaryKeySet = new HashSet<>();
for (ColumnMetadata columnMeta : tableMeta.getPartitionKey()) {
primaryKeySet.add(columnMeta.getName());
boolean hidden = hiddenColumns.contains(columnMeta.getName());
CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, true, false, columnNames.indexOf(columnMeta.getName()), hidden);
columnHandles.add(columnHandle);
}
// add clustering columns
for (ColumnMetadata columnMeta : tableMeta.getClusteringColumns()) {
primaryKeySet.add(columnMeta.getName());
boolean hidden = hiddenColumns.contains(columnMeta.getName());
CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, false, true, columnNames.indexOf(columnMeta.getName()), hidden);
columnHandles.add(columnHandle);
}
// add other columns
for (ColumnMetadata columnMeta : tableMeta.getColumns()) {
if (!primaryKeySet.contains(columnMeta.getName())) {
boolean hidden = hiddenColumns.contains(columnMeta.getName());
CassandraColumnHandle columnHandle = buildColumnHandle(tableMeta, columnMeta, false, false, columnNames.indexOf(columnMeta.getName()), hidden);
columnHandles.add(columnHandle);
}
}
List<CassandraColumnHandle> sortedColumnHandles = columnHandles.build().stream().sorted(comparing(CassandraColumnHandle::getOrdinalPosition)).collect(toList());
CassandraTableHandle tableHandle = new CassandraTableHandle(connectorId, tableMeta.getKeyspace().getName(), tableMeta.getName());
return new CassandraTable(tableHandle, sortedColumnHandles);
}
use of com.datastax.driver.core.TableMetadata in project metacat by Netflix.
the class CassandraConnectorTableService method listNames.
/**
* {@inheritDoc}
*/
@Override
public List<QualifiedName> listNames(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
final String catalog = name.getCatalogName();
final String keyspace = name.getDatabaseName();
log.debug("Attempting to list table names in Cassandra keyspace {} for request {}", keyspace, context);
try {
final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
if (keyspaceMetadata == null) {
throw new DatabaseNotFoundException(name);
}
// TODO: Should we include views?
final List<QualifiedName> tableNames = Lists.newArrayList();
for (final TableMetadata tableMetadata : keyspaceMetadata.getTables()) {
final String tableName = tableMetadata.getName();
if (prefix != null && !tableName.startsWith(prefix.getTableName())) {
continue;
}
tableNames.add(QualifiedName.ofTable(catalog, keyspace, tableName));
}
// Sort
if (sort != null) {
final Comparator<QualifiedName> tableNameComparator = Comparator.comparing(QualifiedName::getTableName);
ConnectorUtils.sort(tableNames, sort, tableNameComparator);
}
// Paging
final List<QualifiedName> paged = ConnectorUtils.paginate(tableNames, pageable);
log.debug("Listed {} table names in Cassandra keyspace {} for request {}", paged.size(), keyspace, context);
return paged;
} catch (final DriverException de) {
log.error(de.getMessage(), de);
throw this.getExceptionMapper().toConnectorException(de, name);
}
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSyncTest method testCreateWithCompositeKey.
@Test
public void testCreateWithCompositeKey() {
EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class).markUnSynced(keyspace);
SchemaSync.sync(keyspace, session, EntityWithCompositeKey.class);
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class);
TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
assertNotNull(tableMetadata);
assertEquals("test_entity_composites", tableMetadata.getName());
assertEquals(8, tableMetadata.getColumns().size());
ColumnMetadata columnMetadata = tableMetadata.getColumn("timestamp");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("asof");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("created");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("email");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("name");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("rank");
assertNotNull(columnMetadata);
columnMetadata = tableMetadata.getColumn("key");
assertNull(columnMetadata);
}
use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.
the class SchemaSyncTest method testCreateWithTimeUUID.
@Test
public void testCreateWithTimeUUID() {
EntityTypeParser.remove(EntityWithTimeUUID.class);
System.out.println(SchemaSync.getScript(keyspace, session, EntityWithTimeUUID.class));
SchemaSync.sync(keyspace, session, EntityWithTimeUUID.class);
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithTimeUUID.class);
TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
assertNotNull(tableMetadata);
assertEquals("test_entity_timeuuid", tableMetadata.getName());
assertEquals(3, tableMetadata.getColumns().size());
ColumnMetadata columnMetadata = tableMetadata.getColumn("name");
assertNotNull(columnMetadata);
assertEquals(DataType.text(), columnMetadata.getType());
columnMetadata = tableMetadata.getColumn("convId");
assertNotNull(columnMetadata);
assertEquals(DataType.timeuuid(), columnMetadata.getType());
columnMetadata = tableMetadata.getColumn("msgId");
assertNotNull(columnMetadata);
assertEquals(DataType.timeuuid(), columnMetadata.getType());
}
Aggregations