Search in sources :

Example 21 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata 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 22 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata 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 23 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata 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)

Example 24 with KeyspaceMetadata

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

the class CassandraConnectorDatabaseService method listViewNames.

/**
     * {@inheritDoc}
     */
@Override
public List<QualifiedName> listViewNames(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName databaseName) {
    final String catalogName = databaseName.getCatalogName();
    final String keyspace = databaseName.getDatabaseName();
    log.debug("Attempting to get materialized view names for keyspace {} due to request {}", keyspace, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(databaseName);
        }
        final ImmutableList.Builder<QualifiedName> viewsBuilder = ImmutableList.builder();
        for (final MaterializedViewMetadata view : keyspaceMetadata.getMaterializedViews()) {
            viewsBuilder.add(QualifiedName.ofView(catalogName, keyspace, view.getBaseTable().getName(), view.getName()));
        }
        final List<QualifiedName> views = viewsBuilder.build();
        log.debug("Successfully found {} views for keyspace {} due to request {}", views.size(), keyspace, context);
        return views;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, databaseName);
    }
}
Also used : MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) ImmutableList(com.google.common.collect.ImmutableList) 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 25 with KeyspaceMetadata

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

the class CassandraConnectorTableService method list.

/**
     * {@inheritDoc}
     */
@Override
public List<TableInfo> list(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    final String keyspace = name.getDatabaseName();
    log.debug("Attempting to list tables 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<TableInfo> tables = Lists.newArrayList();
        for (final TableMetadata tableMetadata : keyspaceMetadata.getTables()) {
            if (prefix != null && !tableMetadata.getName().startsWith(prefix.getTableName())) {
                continue;
            }
            tables.add(this.getTableInfo(name, tableMetadata));
        }
        // Sort
        if (sort != null) {
            final Comparator<TableInfo> tableComparator = Comparator.comparing((t) -> t.getName().getTableName());
            ConnectorUtils.sort(tables, sort, tableComparator);
        }
        // Paging
        final List<TableInfo> pagedTables = ConnectorUtils.paginate(tables, pageable);
        log.debug("Listed {} tables in Cassandra keyspace {} for request {}", pagedTables.size(), keyspace, context);
        return pagedTables;
    } 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) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Aggregations

KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)26 TableMetadata (com.datastax.driver.core.TableMetadata)9 DriverException (com.datastax.driver.core.exceptions.DriverException)7 Cluster (com.datastax.driver.core.Cluster)5 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)5 Test (org.junit.Test)5 ImmutableList (com.google.common.collect.ImmutableList)3 QualifiedName (com.netflix.metacat.common.QualifiedName)3 RegularStatement (com.datastax.driver.core.RegularStatement)2 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)2 ArrayList (java.util.ArrayList)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 MappingManager (com.datastax.driver.mapping.MappingManager)1 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)1 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)1 PrestoException (com.facebook.presto.spi.PrestoException)1 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)1 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)1 AnnotationUDT (zipkin.storage.cassandra3.Schema.AnnotationUDT)1