Search in sources :

Example 41 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 42 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)

Example 43 with KeyspaceMetadata

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

the class CassandraConnectorDatabaseService method get.

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

Example 44 with KeyspaceMetadata

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

the class SchemaSync method alterTableStatements.

/**
 * Compare TableMetadata against Entity metadata and generate alter statements if necessary.
 * <p>
 * Cannot alter clustered and primary key columns.
 *
 * @param class the class to generate statements for or indexed
 * @return a new {@code List<RegularStatement>}.
 */
private static <T> List<RegularStatement> alterTableStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
    boolean doNotAddCols = false;
    boolean doDropCols = true;
    if (syncOptions != null) {
        List<SyncOptionTypes> opts = syncOptions.getOptions(entityMetadata.getEntityClass());
        doNotAddCols = opts.contains(SyncOptionTypes.DoNotAddColumns);
        doDropCols = !opts.contains(SyncOptionTypes.DoNotDropColumns);
    }
    List<RegularStatement> statements = new ArrayList<RegularStatement>();
    // get EntityTypeMetadata
    String table = entityMetadata.getTableName();
    // get TableMetadata - requires connection to cassandra
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    // build statements for a new column or a columns with changed datatype.
    for (EntityFieldMetaData field : entityMetadata.getFields()) {
        String column = field.getColumnName();
        String fieldType = field.getDataType().name();
        ColumnMetadata columnMetadata = tableMetadata.getColumn(column);
        String colIndex = null;
        /*if (columnMetadata!= null && columnMetadata.getIndex() != null) {
    			colIndex = columnMetadata.getIndex().getName();
    		}*/
        String fieldIndex = null;
        if (entityMetadata.getIndex(column) != null) {
            fieldIndex = entityMetadata.getIndex(column);
        }
        if (columnMetadata == null) {
            if (doNotAddCols)
                continue;
            // if column not exists in Cassandra then build add column Statement
            String colType = fieldType;
            if (field.isGenericType()) {
                colType = field.getGenericDef();
            }
            AlterTable statement = new AlterTable.Builder().addColumn(keyspace, table, column, colType);
            statements.add(statement);
            if (fieldIndex != null) {
                statements.add(new DropIndex(column, fieldIndex));
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            }
        } else if (colIndex != null || fieldIndex != null) {
            if (colIndex == null) {
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            } else if (fieldIndex == null) {
                statements.add(new DropIndex(column, colIndex));
            } else if (!"".equals(fieldIndex) && !fieldIndex.equals(colIndex)) {
                statements.add(new DropIndex(column, colIndex));
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            }
        } else if (!fieldType.equals(columnMetadata.getType().getName().name())) {
            // can't change datatype for clustered columns
            if (tableMetadata.getClusteringColumns().contains(columnMetadata)) {
                continue;
            }
            // can't change datatype for PK  columns
            if (tableMetadata.getPrimaryKey().contains(columnMetadata)) {
                continue;
            }
            // drop index if any
            /*if (columnMetadata.getIndex() != null) {
    				statements.add(new DropIndex(column, columnMetadata.getIndex().getName()));
    			}*/
            // alter column datatype
            statements.add(new AlterTable.Builder().alterColumn(keyspace, table, column, fieldType));
            // create index if any
            if (entityMetadata.getIndex(column) != null) {
                statements.add(new CreateIndex(keyspace, table, column, entityMetadata.getIndex(column)));
            }
        }
    }
    // column is in Cassandra but not in entity anymore
    if (doDropCols) {
        for (ColumnMetadata colmeta : tableMetadata.getColumns()) {
            colmeta.getName();
            boolean exists = false;
            for (EntityFieldMetaData field : entityMetadata.getFields()) {
                if (colmeta.getName().equalsIgnoreCase(field.getColumnName())) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                AlterTable statement = new AlterTable.Builder().dropColumn(keyspace, table, colmeta.getName());
                statements.add(statement);
            }
        }
    }
    return statements;
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) RegularStatement(com.datastax.driver.core.RegularStatement) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 45 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)

Aggregations

KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)61 TableMetadata (com.datastax.driver.core.TableMetadata)28 DriverException (com.datastax.driver.core.exceptions.DriverException)14 ImmutableList (com.google.common.collect.ImmutableList)14 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)10 Test (org.junit.Test)10 AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)8 Cluster (com.datastax.driver.core.Cluster)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 PrestoException (com.facebook.presto.spi.PrestoException)7 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)6 QualifiedName (com.netflix.metacat.common.QualifiedName)6 ArrayList (java.util.ArrayList)6 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)5 Metadata (com.datastax.driver.core.Metadata)5 RegularStatement (com.datastax.driver.core.RegularStatement)4 SchemaNotFoundException (com.facebook.presto.spi.SchemaNotFoundException)4 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4