Search in sources :

Example 16 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSync method buildSyncStatements.

/**
     * Generate alter, drop or create statements for the given Entity
     *  
     * @param keyspace
     * @param session
     * @param entityMetadata
     * @param syncOptions
     * @return RegularStatements
     */
public static List<RegularStatement> buildSyncStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
    String table = entityMetadata.getTableName();
    session.execute("USE " + keyspace);
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    List<RegularStatement> statements = new ArrayList<RegularStatement>();
    if (tableMetadata == null) {
        statements = createTableStatements(keyspace, entityMetadata);
    } else {
        statements = alterTableStatements(keyspace, session, entityMetadata, syncOptions);
    }
    return statements;
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) RegularStatement(com.datastax.driver.core.RegularStatement) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 17 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSync method drop.

public static synchronized void drop(String keyspace, Session session, Class<?> clazz) {
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
    entityMetadata.markUnSynced(keyspace);
    String table = entityMetadata.getTableName();
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    if (tableMetadata != null) {
        session.execute("USE " + keyspace);
        // drop indexes
        /*for (ColumnMetadata columnMetadata: tableMetadata.getColumns()) {
    			if (columnMetadata.getIndex() != null) {
    				session.execute(new DropIndex(keyspace, columnMetadata.getIndex().getName()));
    			}
    		}*/
        // drop table
        session.execute(new DropTable(keyspace, entityMetadata));
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Cluster(com.datastax.driver.core.Cluster) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 18 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project presto by prestodb.

the class NativeCassandraSession method getAllTables.

@Override
public List<String> getAllTables(String schema) throws SchemaNotFoundException {
    KeyspaceMetadata meta = getCheckedKeyspaceMetadata(schema);
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (TableMetadata tableMeta : meta.getTables()) {
        builder.add(tableMeta.getName());
    }
    return builder.build();
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ImmutableList(com.google.common.collect.ImmutableList) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 19 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project presto by prestodb.

the class NativeCassandraSession method getTableMetadata.

private TableMetadata getTableMetadata(SchemaTableName schemaTableName) {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    KeyspaceMetadata keyspaceMetadata = getCheckedKeyspaceMetadata(schemaName);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(tableName);
    if (tableMetadata != null) {
        return tableMetadata;
    }
    for (TableMetadata table : keyspaceMetadata.getTables()) {
        if (table.getName().equalsIgnoreCase(tableName)) {
            return table;
        }
    }
    throw new TableNotFoundException(schemaTableName);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 20 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project metacat by Netflix.

the class CassandraConnectorTableService method get.

/**
     * {@inheritDoc}
     */
@Override
public TableInfo get(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name) {
    final String keyspace = name.getDatabaseName();
    final String table = name.getTableName();
    log.debug("Attempting to get metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }
        final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
        if (tableMetadata == null) {
            throw new TableNotFoundException(name);
        }
        final TableInfo tableInfo = this.getTableInfo(name, tableMetadata);
        log.debug("Successfully got metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
        return tableInfo;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Aggregations

TableMetadata (com.datastax.driver.core.TableMetadata)22 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)13 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)8 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)8 Test (org.junit.Test)7 DriverException (com.datastax.driver.core.exceptions.DriverException)6 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)6 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)4 Cluster (com.datastax.driver.core.Cluster)3 ArrayList (java.util.ArrayList)3 RegularStatement (com.datastax.driver.core.RegularStatement)2 ImmutableList (com.google.common.collect.ImmutableList)2 QualifiedName (com.netflix.metacat.common.QualifiedName)2 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)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