Java Code Examples for com.datastax.driver.core.KeyspaceMetadata#getTable()

The following examples show how to use com.datastax.driver.core.KeyspaceMetadata#getTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraConnectorTableService.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TableInfo get(@Nonnull @NonNull final ConnectorRequestContext 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);
    }
}
 
Example 2
Source File: CassandraIntervalCollectionPersistor.java    From brein-time-utilities with Apache License 2.0 6 votes vote down vote up
protected void createColumnFamily() {
    final String ks = getKeySpace();
    final String cf = getColumnFamily();

    final KeyspaceMetadata keySpaceMeta = this.cluster.getMetadata().getKeyspace(ks);
    final TableMetadata tableMetadata = keySpaceMeta.getTable(cf);

    // check if the table exists
    if (tableMetadata != null) {
        return;
    }

    final String stmt = String.format("CREATE TABLE %s (\n" +
            "  " + KEY_COLUMN + " text,\n" +
            "  " + COLL_COLUMN + " blob,\n" +
            "  PRIMARY KEY (" + KEY_COLUMN + ")\n" +
            ");", cf);

    getSession().execute(stmt);
}
 
Example 3
Source File: CassandraDeepJobConfig.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches table metadata from the underlying datastore, using DataStax java driver.
 *
 * @return the table metadata as returned by the driver.
 */
public TableMetadata fetchTableMetadata() {

    Metadata metadata = getSession().getCluster().getMetadata();
    KeyspaceMetadata ksMetadata = metadata.getKeyspace(quote(this.catalog));

    if (ksMetadata != null) {
        return ksMetadata.getTable(quote(this.table));
    } else {
        return null;
    }
}
 
Example 4
Source File: CassandraStorage.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
protected void createSchema(Session session, Cluster cluster) throws Exception {
    if (!createSchemaOnStartup)
        return;

    final KeyspaceMetadata metaData = cluster.getMetadata().getKeyspace(session.getLoggedKeyspace());
    if (metaData.getTable("COP_WORKFLOW_INSTANCE") != null) {
        logger.info("skipping schema creation");
        return;
    }

    logger.info("Creating tables...");
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(CassandraStorage.class.getResourceAsStream("copper-schema.cql")))) {
        StringBuilder cql = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.startsWith("--"))
                continue;
            if (line.endsWith(";")) {
                if (line.length() > 1)
                    cql.append(line.substring(0, line.length() - 1));
                String cqlCmd = cql.toString();
                cql = new StringBuilder();
                logger.info("Executing CQL {}", cqlCmd);
                session.execute(cqlCmd);
            }
            else {
                cql.append(line).append(" ");
            }
        }
    }

}
 
Example 5
Source File: CassandraOperations.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public boolean indexExists(final String indexName) throws IOException {
  final String tableName = getCassandraSafeName(indexName);
  Boolean tableExists = state.tableExistsCache.get(tableName);
  if (tableExists == null) {
    final KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(gwNamespace);
    if (keyspace != null) {
      tableExists = keyspace.getTable(tableName) != null;
    } else {
      tableExists = false;
    }
    state.tableExistsCache.put(tableName, tableExists);
  }
  return tableExists;
}
 
Example 6
Source File: CassandraUtils.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public static TableMetadata getTableMetadata(final Session session, final String keyspace, final String table) {
  Preconditions.checkNotNull(session);
  Preconditions.checkNotNull(keyspace);
  Preconditions.checkNotNull(table);
  final KeyspaceMetadata keyspaceMetadata = session.getCluster().getMetadata().getKeyspace(keyspace);
  if (keyspaceMetadata == null) {
    throw new IllegalStateException(String.format("Keyspace %s does not exist", keyspace));
  }
  final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
  if (tableMetadata == null) {
    throw new IllegalStateException(String.format("Table %s.%s does not exist", keyspace, table));
  }
  return tableMetadata;
}
 
Example 7
Source File: CQLMetadataCache.java    From Doradus with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if column values for the given namespace/store name are binary.
 * 
 * @param namespace Namespace (Keyspace) name.
 * @param storeName Store (ColumnFamily) name.
 * @return          True if the given table's column values are binary.
 */
public boolean columnValueIsBinary(String namespace, String storeName) {
    Boolean cachedValue = getCachedValueIsBinary(namespace, storeName);
    if(cachedValue != null) return cachedValue.booleanValue();
    
    String cqlKeyspace = CQLService.storeToCQLName(namespace);
    String tableName = CQLService.storeToCQLName(storeName);
    KeyspaceMetadata ksMetadata = m_cluster.getMetadata().getKeyspace(cqlKeyspace);
    TableMetadata tableMetadata = ksMetadata.getTable(tableName);
    ColumnMetadata colMetadata = tableMetadata.getColumn("value");
    boolean isBinary = colMetadata.getType().equals(DataType.blob());
    
    putCachedValueIsBinary(namespace, storeName, isBinary);
    return isBinary;
}
 
Example 8
Source File: CQLService.java    From Doradus with Apache License 2.0 5 votes vote down vote up
public List<String> getDoradusKeyspaces() {
    List<String> keyspaces = new ArrayList<>();
    List<KeyspaceMetadata> keyspaceList = m_cluster.getMetadata().getKeyspaces();
    for (KeyspaceMetadata ksMetadata : keyspaceList) {
        if (ksMetadata.getTable(APPS_CQL_NAME) != null) {
            keyspaces.add(ksMetadata.getName());
        }
    }
    return keyspaces;
}
 
Example 9
Source File: CassandraPersistWriter.java    From streams with Apache License 2.0 5 votes vote down vote up
private void createKeyspaceAndTable() {
  Metadata metadata = client.cluster().getMetadata();
  if (Objects.isNull(metadata.getKeyspace(config.getKeyspace()))) {
    LOGGER.info("Keyspace {} does not exist. Creating Keyspace", config.getKeyspace());
    Map<String, Object> replication = new HashMap<>();
    replication.put("class", "SimpleStrategy");
    replication.put("replication_factor", 1);

    String createKeyspaceStmt = SchemaBuilder.createKeyspace(config.getKeyspace()).with()
        .replication(replication).getQueryString();
    client.cluster().connect().execute(createKeyspaceStmt);
  }

  session = client.cluster().connect(config.getKeyspace());

  KeyspaceMetadata ks = metadata.getKeyspace(config.getKeyspace());
  TableMetadata tableMetadata = ks.getTable(config.getTable());

  if (Objects.isNull(tableMetadata)) {
    LOGGER.info("Table {} does not exist in Keyspace {}. Creating Table", config.getTable(), config.getKeyspace());
    String createTableStmt = SchemaBuilder.createTable(config.getTable())
                              .addPartitionKey(config.getPartitionKeyColumn(), DataType.varchar())
                              .addColumn(config.getColumn(), DataType.blob()).getQueryString();

    session.execute(createTableStmt);
  }
}
 
Example 10
Source File: CassandraSession.java    From presto with Apache License 2.0 5 votes vote down vote up
private void checkSizeEstimatesTableExist()
{
    KeyspaceMetadata keyspaceMetadata = executeWithSession(session -> session.getCluster().getMetadata().getKeyspace(SYSTEM));
    checkState(keyspaceMetadata != null, "system keyspace metadata must not be null");
    TableMetadata table = keyspaceMetadata.getTable(SIZE_ESTIMATES);
    if (table == null) {
        throw new PrestoException(NOT_SUPPORTED, "Cassandra versions prior to 2.1.5 are not supported");
    }
}
 
Example 11
Source File: Session.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public @Nullable TableMetadata getTable(String name) {
    KeyspaceMetadata keyspace = getKeyspace();
    if (keyspace == null) {
        return null;
    } else {
        return keyspace.getTable(name);
    }
}
 
Example 12
Source File: CassandraTable.java    From james-project with Apache License 2.0 5 votes vote down vote up
public InitializationStatus initialize(KeyspaceMetadata keyspaceMetadata, Session session) {
    if (keyspaceMetadata.getTable(name) != null) {
        return InitializationStatus.ALREADY_DONE;
    }

    session.execute(createStatement);
    return InitializationStatus.FULL;
}
 
Example 13
Source File: CassandraTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private List<String> checkColumnMappings() throws StageException {
  List<String> invalidColumnMappings = new ArrayList<>();

  columnMappings = new TreeMap<>();
  for (CassandraFieldMappingConfig column : conf.columnNames) {
    columnMappings.put(column.columnName, column.field);
  }

  final String[] tableNameParts = conf.qualifiedTableName.split("\\.");
  final String keyspace = tableNameParts[0];
  final String table = tableNameParts[1];

  try (Cluster validationCluster = getCluster()) {
    final KeyspaceMetadata keyspaceMetadata = validationCluster.getMetadata().getKeyspace(keyspace);
    final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    final List<String> columns = Lists.transform(
        tableMetadata.getColumns(),
        new Function<ColumnMetadata, String>() {
          @Nullable
          @Override
          public String apply(ColumnMetadata columnMetadata) {
            return columnMetadata.getName();
          }
        }
    );

    invalidColumnMappings.addAll(columnMappings.keySet()
        .stream()
        .filter(columnName -> !columns.contains(columnName))
        .collect(Collectors.toList())
    );
  }

  return invalidColumnMappings;
}
 
Example 14
Source File: CassandraStore.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
protected boolean existsTable(String table) {
     KeyspaceMetadata keyspace = this.cluster().getMetadata()
                                     .getKeyspace(this.keyspace);
     if (keyspace != null && keyspace.getTable(table) != null) {
         return true;
     }
     return false;
}
 
Example 15
Source File: ClusterManager.java    From scalardb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link TableMetadata} with the specified keyspace and table name
 *
 * @param keyspace keyspace name
 * @param table table name
 * @return {@code TableMetadata}
 */
@Nonnull
public TableMetadata getMetadata(String keyspace, String table) {
  KeyspaceMetadata metadata;
  try {
    metadata = cluster.getMetadata().getKeyspace(keyspace);
  } catch (RuntimeException e) {
    throw new ConnectionException("can't get metadata from the cluster", e);
  }
  if (metadata == null || metadata.getTable(table) == null) {
    throw new StorageRuntimeException("no table information found");
  }
  return metadata.getTable(table);
}
 
Example 16
Source File: CQLService.java    From Doradus with Apache License 2.0 4 votes vote down vote up
private boolean storeExists(String tableName) {
    KeyspaceMetadata ksMetadata = m_cluster.getMetadata().getKeyspace(m_keyspace);
    return (ksMetadata != null) && (ksMetadata.getTable(tableName) != null);
}
 
Example 17
Source File: AbstractUpsertOutputOperator.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes cassandra cluster connections as specified by the Connection State manager.
 * Aspects that are initialized here include Identifying primary key column names, non-primary key columns,
 * collection type columns, counter columns
 * It also queries the Keyspace and Table metadata for extracting the above information.
 * It finally prepares all possible prepared statements that can be executed in the lifetime of the operator
 * for various permutations like APPEND/REMOVE COLLECTIONS , LIST APPEND/PREPEND , set nulls, set TTLs etc
 * @param context The apex framework context
 */
@Override
public void activate(Context.OperatorContext context)
{
  ConnectionStateManager.ConnectionBuilder connectionBuilder = withConnectionBuilder();
  if (connectionBuilder == null) {
    connectionBuilder = buildConnectionBuilderFromPropertiesFile();
  }
  checkNotNull(connectionBuilder, " Connection Builder cannot be null.");
  connectionStateManager = connectionBuilder.initialize();
  cluster = connectionStateManager.getCluster();
  session = connectionStateManager.getSession();
  checkNotNull(session, "Cassandra session cannot be null");
  tuplePayloadClass = getPayloadPojoClass();
  columnDefinitions = new HashMap<>();
  counterColumns = new HashSet<>();
  collectionColumns = new HashSet<>();
  pkColumnNames = new HashSet<>();
  listColumns = new HashSet<>();
  mapColumns = new HashSet<>();
  setColumns = new HashSet<>();
  codecsForCassandraColumnNames = new HashMap<>();
  userDefinedTypeColumns = new HashSet<>();
  regularColumns = new HashSet<>();
  colNamesMap = new HashMap<>();
  getters = new HashMap<>();
  userDefinedTypesClass = new HashMap<>();
  uniqueHostsWrittenToInCurrentWindow = new HashSet<>();
  registerCodecs();
  KeyspaceMetadata keyspaceMetadata = cluster.getMetadata()
      .getKeyspace(connectionStateManager.getKeyspaceName());
  TableMetadata tableMetadata = keyspaceMetadata
      .getTable(connectionStateManager.getTableName());
  registerPrimaryKeyColumnDefinitions(tableMetadata);
  registerNonPKColumnDefinitions(tableMetadata);
  preparedStatementTypes = new HashMap<>();
  generatePreparedStatements();
  registerGettersForPayload();
  isInSafeMode = false;
  isInReconcilingMode = false;
  reconcilingWindowId = Stateless.WINDOW_ID;
  if ( (context.getValue(Context.OperatorContext.ACTIVATION_WINDOW_ID) != Stateless.WINDOW_ID) &&
      context.getValue(Context.OperatorContext.ACTIVATION_WINDOW_ID) <
      windowDataManager.getLargestCompletedWindow()) {
    isInSafeMode = true;
    reconcilingWindowId = windowDataManager.getLargestCompletedWindow() + 1;
    isInReconcilingMode = false;
  }
}
 
Example 18
Source File: CassandraClusterInfo.java    From hdfs2cass with Apache License 2.0 4 votes vote down vote up
public void init(final String keyspace, final String columnFamily) {

    this.keyspace = keyspace;
    this.columnFamily = columnFamily;

    // connect to the cluster
    Cluster.Builder clusterBuilder = Cluster.builder();
    clusterBuilder.addContactPoints(host);
    if (port != -1) {
      clusterBuilder.withPort(port);
    }

    // ask for some metadata
    logger.info("getting cluster metadata for {}.{}", keyspace, columnFamily);
    final TableMetadata tableMetadata;
    try (final Cluster cluster = clusterBuilder.build()) {
      Metadata clusterMetadata = cluster.getMetadata();
      KeyspaceMetadata keyspaceMetadata = clusterMetadata.getKeyspace('"' + keyspace + '"');
      tableMetadata = keyspaceMetadata.getTable('"' + columnFamily + '"');
      cqlSchema = tableMetadata.asCQLQuery();
      partitionerClass = clusterMetadata.getPartitioner();
      Class.forName(partitionerClass);
      numClusterNodes = clusterMetadata.getAllHosts().size();
      columns = tableMetadata.getColumns();
    } catch (ClassNotFoundException cnfe) {
      throw new CrunchRuntimeException("No such partitioner: " + partitionerClass, cnfe);
    } catch (NullPointerException npe) {
      String msg = String.format("No such keyspace/table: %s/%s", keyspace, columnFamily);
      throw new CrunchRuntimeException(msg, npe);
    }

    // map the partition key columns
    final List<ColumnMetadata> partitionKeyColumns = tableMetadata.getPartitionKey();
    partitionKeyIndexes = new int[partitionKeyColumns.size()];
    for (int i = 0; i < partitionKeyColumns.size(); i++) {
      final String keyColName = partitionKeyColumns.get(i).getName();
      int j;
      for (j = 0; j < columns.size(); j++) {
        if (columns.get(j).getName().equals(keyColName)) {
          partitionKeyIndexes[i] = j;
          logger.info("partition key column {} index {}", keyColName, j);
          break;
        }
      }
      if (j == columns.size()) {
        throw new CrunchRuntimeException("no matching column for key " + keyColName);
      }
    }
  }