Search in sources :

Example 1 with TableMetadata

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");
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) PrestoException(com.facebook.presto.spi.PrestoException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 2 with TableMetadata

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);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with TableMetadata

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);
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) QualifiedName(com.netflix.metacat.common.QualifiedName) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 4 with TableMetadata

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);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) EntityWithCompositeKey(com.datastax.driver.mapping.entity.EntityWithCompositeKey) Test(org.junit.Test)

Example 5 with TableMetadata

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());
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) EntityWithTimeUUID(com.datastax.driver.mapping.entity.EntityWithTimeUUID) Test(org.junit.Test)

Aggregations

TableMetadata (com.datastax.driver.core.TableMetadata)18 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)9 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)8 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)8 Test (org.junit.Test)7 Cluster (com.datastax.driver.core.Cluster)3 DriverException (com.datastax.driver.core.exceptions.DriverException)3 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)3 ArrayList (java.util.ArrayList)3 RegularStatement (com.datastax.driver.core.RegularStatement)2 ImmutableList (com.google.common.collect.ImmutableList)2 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)2 com.datastax.driver.core (com.datastax.driver.core)1 EntityWithCompositeKey (com.datastax.driver.mapping.entity.EntityWithCompositeKey)1 EntityWithIndexes (com.datastax.driver.mapping.entity.EntityWithIndexes)1 EntityWithTimeUUID (com.datastax.driver.mapping.entity.EntityWithTimeUUID)1 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)1 PrestoException (com.facebook.presto.spi.PrestoException)1 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1