org.apache.cassandra.thrift.CfDef Java Examples

The following examples show how to use org.apache.cassandra.thrift.CfDef. 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: CassandraColumnMetaData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Static utility routine that returns a list of column families that exist in
 * the keyspace encapsulated in the supplied connection
 * 
 * @param conn the connection to use
 * @return a list of column families (tables)
 * @throws Exception if a problem occurs
 */
public static List<String> getColumnFamilyNames(CassandraConnection conn)
    throws Exception {

  KsDef keySpace = conn.describeKeyspace();
  List<CfDef> colFams = null;
  if (keySpace != null) {
    colFams = keySpace.getCf_defs();
  } else {
    throw new Exception(BaseMessages.getString(PKG,
        "CassandraColumnMetaData.Error.UnableToGetMetaDataForKeyspace",
        conn.m_keyspaceName));
  }

  List<String> colFamNames = new ArrayList<String>();
  for (CfDef fam : colFams) {
    colFamNames.add(fam.getName());
  }

  return colFamNames;
}
 
Example #2
Source File: CassandraSchemaMgr.java    From Doradus with Apache License 2.0 6 votes vote down vote up
/**
 * Return true if the given store name currently exists in the given keyspace. This
 * method can be used with a connection connected to any keyspace.
 * 
 * @param dbConn    Database connection to use.
 * @param cfName    Candidate ColumnFamily name.
 * @return          True if the CF exists in the database.
 */
public boolean columnFamilyExists(DBConn dbConn, String keyspace, String cfName) {
    KsDef ksDef = null;
    try {
        ksDef = dbConn.getClientSession().describe_keyspace(keyspace);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to get keyspace definition for '" + keyspace + "'", ex);
    }
    
    List<CfDef> cfDefList = ksDef.getCf_defs();
    for (CfDef cfDef : cfDefList) {
        if (cfDef.getName().equals(cfName)) {
            return true;
        }
    }
    return false;
}
 
Example #3
Source File: CliCompiler.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static String getColumnFamily(String cfName, Iterable<CfDef> cfDefs)
{
    int matches = 0;
    String lastMatchedName = "";

    for (CfDef cfDef : cfDefs)
    {
        if (cfDef.name.equals(cfName))
        {
            return cfName;
        }
        else if (cfDef.name.toUpperCase().equals(cfName.toUpperCase()))
        {
            lastMatchedName = cfDef.name;
            matches++;
        }
    }

    if (matches > 1 || matches == 0)
        throw new RuntimeException(cfName + " not found in current keyspace.");

    return lastMatchedName;
}
 
Example #4
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/** applies implicit defaults to cf definition. useful in updates */
private static void applyImplicitDefaults(org.apache.cassandra.thrift.CfDef cf_def)
{
    if (!cf_def.isSetComment())
        cf_def.setComment("");
    if (!cf_def.isSetMin_compaction_threshold())
        cf_def.setMin_compaction_threshold(CFMetaData.DEFAULT_MIN_COMPACTION_THRESHOLD);
    if (!cf_def.isSetMax_compaction_threshold())
        cf_def.setMax_compaction_threshold(CFMetaData.DEFAULT_MAX_COMPACTION_THRESHOLD);
    if (cf_def.compaction_strategy == null)
        cf_def.compaction_strategy = DEFAULT_COMPACTION_STRATEGY_CLASS.getSimpleName();
    if (cf_def.compaction_strategy_options == null)
        cf_def.compaction_strategy_options = Collections.emptyMap();
    if (!cf_def.isSetCompression_options())
    {
        cf_def.setCompression_options(new HashMap<String, String>()
        {{
            if (DEFAULT_COMPRESSOR != null)
                put(CompressionParameters.SSTABLE_COMPRESSION, DEFAULT_COMPRESSOR);
        }});
    }
    if (!cf_def.isSetDefault_time_to_live())
        cf_def.setDefault_time_to_live(CFMetaData.DEFAULT_DEFAULT_TIME_TO_LIVE);
    if (!cf_def.isSetDclocal_read_repair_chance())
        cf_def.setDclocal_read_repair_chance(CFMetaData.DEFAULT_DCLOCAL_READ_REPAIR_CHANCE);

    // if index_interval was set, use that for the min_index_interval default
    if (!cf_def.isSetMin_index_interval())
    {
        if (cf_def.isSetIndex_interval())
            cf_def.setMin_index_interval(cf_def.getIndex_interval());
        else
            cf_def.setMin_index_interval(CFMetaData.DEFAULT_MIN_INDEX_INTERVAL);
    }
    if (!cf_def.isSetMax_index_interval())
    {
        // ensure the max is at least as large as the min
        cf_def.setMax_index_interval(Math.max(cf_def.min_index_interval, CFMetaData.DEFAULT_MAX_INDEX_INTERVAL));
    }
}
 
Example #5
Source File: KSMetaData.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public KsDef toThrift()
{
    List<CfDef> cfDefs = new ArrayList<>(cfMetaData.size());
    for (CFMetaData cfm : cfMetaData().values())
    {
        // Don't expose CF that cannot be correctly handle by thrift; see CASSANDRA-4377 for further details
        if (cfm.isThriftCompatible())
            cfDefs.add(cfm.toThrift());
    }
    KsDef ksdef = new KsDef(name, strategyClass.getName(), cfDefs);
    ksdef.setStrategy_options(strategyOptions);
    ksdef.setDurable_writes(durableWrites);

    return ksdef;
}
 
Example #6
Source File: CliUtils.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns set of column family names in specified keySpace.
 * @param keySpace - keyspace definition to get column family names from.
 * @return Set - column family names
 */
public static Set<String> getCfNamesByKeySpace(KsDef keySpace)
{
    Set<String> names = new LinkedHashSet<String>();

    for (CfDef cfDef : keySpace.getCf_defs())
    {
        names.add(cfDef.getName());
    }

    return names;
}
 
Example #7
Source File: CFMetaDataTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testThriftConversion() throws Exception
{
    CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName())
                             .setComment("Test comment")
                             .setColumn_metadata(columnDefs)
                             .setKeyspace(KEYSPACE)
                             .setName(COLUMN_FAMILY);

    // convert Thrift to CFMetaData
    CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef);

    CfDef thriftCfDef = new CfDef();
    thriftCfDef.keyspace = KEYSPACE;
    thriftCfDef.name = COLUMN_FAMILY;
    thriftCfDef.default_validation_class = cfDef.default_validation_class;
    thriftCfDef.comment = cfDef.comment;
    thriftCfDef.column_metadata = new ArrayList<ColumnDef>();
    for (ColumnDef columnDef : columnDefs)
    {
        ColumnDef c = new ColumnDef();
        c.name = ByteBufferUtil.clone(columnDef.name);
        c.validation_class = columnDef.getValidation_class();
        c.index_name = columnDef.getIndex_name();
        c.index_type = IndexType.KEYS;
        thriftCfDef.column_metadata.add(c);
    }

    CfDef converted = cfMetaData.toThrift();

    assertEquals(thriftCfDef.keyspace, converted.keyspace);
    assertEquals(thriftCfDef.name, converted.name);
    assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class);
    assertEquals(thriftCfDef.comment, converted.comment);
    assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata));
}
 
Example #8
Source File: CassandraColumnMetaData.java    From learning-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Static utility routine for checking for the existence of a column family
 * (table)
 * 
 * @param conn the connection to use
 * @param columnFamily the column family to check for
 * @return true if the supplied column family name exists in the keyspace
 * @throws Exception if a problem occurs
 */
public static boolean columnFamilyExists(CassandraConnection conn,
    String columnFamily) throws Exception {

  boolean found = false;

  // column families
  KsDef keySpace = conn.describeKeyspace();
  List<CfDef> colFams = null;
  if (keySpace != null) {
    colFams = keySpace.getCf_defs();
  } else {
    throw new Exception(BaseMessages.getString(PKG,
        "CassandraColumnMetaData.Error.UnableToGetMetaDataForKeyspace",
        conn.m_keyspaceName));
  }

  // look for the requested column family
  for (CfDef fam : colFams) {
    String columnFamilyName = fam.getName(); // table name
    if (columnFamilyName.equals(columnFamily)) {
      found = true;
      break;
    }
  }

  return found;
}
 
Example #9
Source File: ColumnFamilyWideRowRecordReader.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
private CfDef findCfDef(KsDef ks_def, String cfName)
{
    for (CfDef cfDef : ks_def.cf_defs)
    {
        if (cfDef.name.equals(cfName)) {
          return cfDef;
        }
    }

   return null;
}
 
Example #10
Source File: CassandraManager.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Get Column family based on the configuration in the table. If nothing is found, return null.
 */
private CfDef getColumnFamily(KsDef ks) {
  for (CfDef cf : ks.getCf_defs()) {
    if (cf.getName().equalsIgnoreCase(columnFamilyName)) {
      return cf;
    }
  }

  return null;
}
 
Example #11
Source File: CassandraManager.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Get CfDef based on the configuration in the table.
 */
private CfDef getCfDef() throws MetaException {
  CfDef cf = new CfDef();
  cf.setKeyspace(keyspace);
  cf.setName(columnFamilyName);

  cf.setColumn_type(getColumnType());

  return cf;
}
 
Example #12
Source File: CassandraManager.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Create the column family if it doesn't exist.
 * @param ks
 * @return
 * @throws MetaException
 */
public CfDef createCFIfNotFound(KsDef ks) throws MetaException {
  CfDef cf = getColumnFamily(ks);
  if (cf == null) {
    return createColumnFamily();
  } else {
    return cf;
  }
}
 
Example #13
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static CFMetaData fromThrift(CfDef cf_def) throws InvalidRequestException, ConfigurationException
{
    return internalFromThrift(cf_def, Collections.<ColumnDefinition>emptyList());
}
 
Example #14
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static CFMetaData fromThriftForUpdate(CfDef cf_def, CFMetaData toUpdate) throws InvalidRequestException, ConfigurationException
{
    return internalFromThrift(cf_def, toUpdate.allColumns());
}
 
Example #15
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static CFMetaData internalFromThrift(CfDef cf_def, Collection<ColumnDefinition> previousCQLMetadata) throws InvalidRequestException, ConfigurationException
{
    ColumnFamilyType cfType = ColumnFamilyType.create(cf_def.column_type);
    if (cfType == null)
        throw new InvalidRequestException("Invalid column type " + cf_def.column_type);

    applyImplicitDefaults(cf_def);

    try
    {
        AbstractType<?> rawComparator = TypeParser.parse(cf_def.comparator_type);
        AbstractType<?> subComparator = cfType == ColumnFamilyType.Standard
                                      ? null
                                      : cf_def.subcomparator_type == null ? BytesType.instance : TypeParser.parse(cf_def.subcomparator_type);

        AbstractType<?> fullRawComparator = makeRawAbstractType(rawComparator, subComparator);

        AbstractType<?> keyValidator = cf_def.isSetKey_validation_class() ? TypeParser.parse(cf_def.key_validation_class) : null;

        // Convert the REGULAR definitions from the input CfDef
        List<ColumnDefinition> defs = ColumnDefinition.fromThrift(cf_def.keyspace, cf_def.name, rawComparator, subComparator, cf_def.column_metadata);

        // Add the keyAlias if there is one, since that's on CQL metadata that thrift can actually change (for
        // historical reasons)
        boolean hasKeyAlias = cf_def.isSetKey_alias() && keyValidator != null && !(keyValidator instanceof CompositeType);
        if (hasKeyAlias)
            defs.add(ColumnDefinition.partitionKeyDef(cf_def.keyspace, cf_def.name, cf_def.key_alias, keyValidator, null));

        // Now add any CQL metadata that we want to copy, skipping the keyAlias if there was one
        for (ColumnDefinition def : previousCQLMetadata)
        {
            // isPartOfCellName basically means 'is not just a CQL metadata'
            if (def.isPartOfCellName())
                continue;

            if (def.kind == ColumnDefinition.Kind.PARTITION_KEY && hasKeyAlias)
                continue;

            defs.add(def);
        }

        CellNameType comparator = CellNames.fromAbstractType(fullRawComparator, calculateIsDense(fullRawComparator, defs));

        UUID cfId = Schema.instance.getId(cf_def.keyspace, cf_def.name);
        if (cfId == null)
            cfId = UUIDGen.getTimeUUID();

        CFMetaData newCFMD = new CFMetaData(cf_def.keyspace, cf_def.name, cfType, comparator, cfId);

        newCFMD.addAllColumnDefinitions(defs);

        if (keyValidator != null)
            newCFMD.keyValidator(keyValidator);
        if (cf_def.isSetGc_grace_seconds())
            newCFMD.gcGraceSeconds(cf_def.gc_grace_seconds);
        if (cf_def.isSetMin_compaction_threshold())
            newCFMD.minCompactionThreshold(cf_def.min_compaction_threshold);
        if (cf_def.isSetMax_compaction_threshold())
            newCFMD.maxCompactionThreshold(cf_def.max_compaction_threshold);
        if (cf_def.isSetCompaction_strategy())
            newCFMD.compactionStrategyClass(createCompactionStrategy(cf_def.compaction_strategy));
        if (cf_def.isSetCompaction_strategy_options())
            newCFMD.compactionStrategyOptions(new HashMap<>(cf_def.compaction_strategy_options));
        if (cf_def.isSetBloom_filter_fp_chance())
            newCFMD.bloomFilterFpChance(cf_def.bloom_filter_fp_chance);
        if (cf_def.isSetMemtable_flush_period_in_ms())
            newCFMD.memtableFlushPeriod(cf_def.memtable_flush_period_in_ms);
        if (cf_def.isSetCaching() || cf_def.isSetCells_per_row_to_cache())
            newCFMD.caching(CachingOptions.fromThrift(cf_def.caching, cf_def.cells_per_row_to_cache));
        if (cf_def.isSetRead_repair_chance())
            newCFMD.readRepairChance(cf_def.read_repair_chance);
        if (cf_def.isSetDefault_time_to_live())
            newCFMD.defaultTimeToLive(cf_def.default_time_to_live);
        if (cf_def.isSetDclocal_read_repair_chance())
            newCFMD.dcLocalReadRepairChance(cf_def.dclocal_read_repair_chance);
        if (cf_def.isSetMin_index_interval())
            newCFMD.minIndexInterval(cf_def.min_index_interval);
        if (cf_def.isSetMax_index_interval())
            newCFMD.maxIndexInterval(cf_def.max_index_interval);
        if (cf_def.isSetSpeculative_retry())
            newCFMD.speculativeRetry(SpeculativeRetry.fromString(cf_def.speculative_retry));
        if (cf_def.isSetTriggers())
            newCFMD.triggers(TriggerDefinition.fromThrift(cf_def.triggers));

        return newCFMD.comment(cf_def.comment)
                      .defaultValidator(TypeParser.parse(cf_def.default_validation_class))
                      .compressionParameters(CompressionParameters.create(cf_def.compression_options))
                      .rebuild();
    }
    catch (SyntaxException | MarshalException e)
    {
        throw new ConfigurationException(e.getMessage());
    }
}
 
Example #16
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public org.apache.cassandra.thrift.CfDef toThrift()
{
    org.apache.cassandra.thrift.CfDef def = new org.apache.cassandra.thrift.CfDef(ksName, cfName);
    def.setColumn_type(cfType.name());

    if (isSuper())
    {
        def.setComparator_type(comparator.subtype(0).toString());
        def.setSubcomparator_type(comparator.subtype(1).toString());
    }
    else
    {
        def.setComparator_type(comparator.toString());
    }

    def.setComment(Strings.nullToEmpty(comment));
    def.setRead_repair_chance(readRepairChance);
    def.setDclocal_read_repair_chance(dcLocalReadRepairChance);
    def.setGc_grace_seconds(gcGraceSeconds);
    def.setDefault_validation_class(defaultValidator == null ? null : defaultValidator.toString());
    def.setKey_validation_class(keyValidator.toString());
    def.setMin_compaction_threshold(minCompactionThreshold);
    def.setMax_compaction_threshold(maxCompactionThreshold);
    // We only return the alias if only one is set since thrift don't know about multiple key aliases
    if (partitionKeyColumns.size() == 1)
        def.setKey_alias(partitionKeyColumns.get(0).name.bytes);
    def.setColumn_metadata(ColumnDefinition.toThrift(columnMetadata));
    def.setCompaction_strategy(compactionStrategyClass.getName());
    def.setCompaction_strategy_options(new HashMap<>(compactionStrategyOptions));
    def.setCompression_options(compressionParameters.asThriftOptions());
    if (bloomFilterFpChance != null)
        def.setBloom_filter_fp_chance(bloomFilterFpChance);
    def.setMin_index_interval(minIndexInterval);
    def.setMax_index_interval(maxIndexInterval);
    def.setMemtable_flush_period_in_ms(memtableFlushPeriod);
    def.setCaching(caching.toThriftCaching());
    def.setCells_per_row_to_cache(caching.toThriftCellsPerRow());
    def.setDefault_time_to_live(defaultTimeToLive);
    def.setSpeculative_retry(speculativeRetry.toString());
    def.setTriggers(TriggerDefinition.toThrift(triggers));

    return def;
}
 
Example #17
Source File: CliCompiler.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static String getColumnFamily(Tree astNode, Iterable<CfDef> cfDefs)
{
    return getColumnFamily(CliUtils.unescapeSQLString(astNode.getChild(0).getText()), cfDefs);
}
 
Example #18
Source File: CassandraProxyClient.java    From Hive-Cassandra with Apache License 2.0 1 votes vote down vote up
/**
 * Create a temporary keyspace. This will only be called when there is no keyspace except system
 * defined on (new cluster).
 * However we need a keyspace to call describe_ring to get all servers from the ring.
 *
 * @return the temporary keyspace
 * @throws InvalidRequestException
 *           error
 * @throws TException
 *           error
 * @throws SchemaDisagreementException
 * @throws InterruptedException
 *           error
 */
private KsDef createTmpKs() throws InvalidRequestException, TException, SchemaDisagreementException {

  Map<String, String> stratOpts = new HashMap<String, String>();
  stratOpts.put("replication_factor", "1");

  KsDef tmpKs = new KsDef("proxy_client_ks", "org.apache.cassandra.locator.SimpleStrategy",
      Arrays.asList(new CfDef[] {})).setStrategy_options(stratOpts);

  clientHolder.getClient().system_add_keyspace(tmpKs);

  return tmpKs;
}