org.apache.cassandra.thrift.KsDef Java Examples

The following examples show how to use org.apache.cassandra.thrift.KsDef. 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 getKeySpace(String ksName, List<KsDef> keyspaces)
{
    int matches = 0;
    String lastMatchedName = "";

    for (KsDef ksDef : keyspaces)
    {
        if (ksDef.name.equals(ksName))
        {
            return ksName;
        }
        else if (ksDef.name.toUpperCase().equals(ksName.toUpperCase()))
        {
            lastMatchedName = ksDef.name;
            matches++;
        }
    }

    if (matches > 1 || matches == 0)
        throw new RuntimeException("Keyspace '" + ksName + "' not found.");

    return lastMatchedName;
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
Source File: CassandraStorageHandler.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void preCreateTable(Table table) throws MetaException {
  boolean isExternal = MetaStoreUtils.isExternalTable(table);

  if (!isExternal) {
    throw new MetaException("Cassandra tables must be external.");
  }

  if (table.getSd().getLocation() != null) {
    throw new MetaException("LOCATION may not be specified for Cassandra.");
  }

  CassandraManager manager = new CassandraManager(table);

  try {
    //open connection to cassandra
    manager.openConnection();
    KsDef ks = manager.getKeyspaceDesc();

    //create the column family if it doesn't exist.
    manager.createCFIfNotFound(ks);
  } catch(NotFoundException e) {
    manager.createKeyspaceWithColumns();
  } finally {
    manager.closeConnection();
  }
}
 
Example #9
Source File: CassandraSchemaMgr.java    From Doradus with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new keyspace with the given name. The keyspace is created with parameters
 * defined for our DBService instance, if any. This method should be used with a
 * no-keyspace DB connection.
 * 
 * @param dbConn    Database connection to use.
 * @param keyspace  Name of new keyspace.
 */
public void createKeyspace(DBConn dbConn, String keyspace) {
    m_logger.info("Creating Keyspace '{}'", keyspace);
    try {
        KsDef ksDef = setKeySpaceOptions(keyspace);
        dbConn.getClientSession().system_add_keyspace(ksDef);
        waitForSchemaPropagation(dbConn);
        Thread.sleep(1000);  // wait for gossip to other Cassandra nodes
    } catch (Exception ex) {
        String errMsg = "Failed to create Keyspace '" + keyspace + "'"; 
        m_logger.error(errMsg, ex);
        throw new RuntimeException(errMsg, ex);
    }
}
 
Example #10
Source File: CassandraSchemaMgr.java    From Doradus with Apache License 2.0 5 votes vote down vote up
/**
 * Get a list of all known keyspaces. This method can be used with any DB connection.
 * 
 * @param  dbConn   Database connection to use.
 * @return          List of all known keyspaces, empty if none.
 */
public Collection<String> getKeyspaces(DBConn dbConn) {
    List<String> result = new ArrayList<>();
    try {
        for (KsDef ksDef : dbConn.getClientSession().describe_keyspaces()) {
            result.add(ksDef.getName());
        }
    } catch (Exception e) {
        String errMsg = "Failed to get keyspace description";
        m_logger.error(errMsg, e);
        throw new RuntimeException(errMsg, e);
    }
    return result;
}
 
Example #11
Source File: CliUtils.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the statement from cli and return KsDef
 *
 * @param keyspaceName - name of the keyspace to lookup
 * @param keyspaces - List of known keyspaces
 *
 * @return metadata about keyspace or null
 */
public static KsDef getKeySpaceDef(String keyspaceName, List<KsDef> keyspaces)
{
    keyspaceName = keyspaceName.toUpperCase();

    for (KsDef ksDef : keyspaces)
    {
        if (ksDef.name.toUpperCase().equals(keyspaceName))
            return ksDef;
    }

    return null;
}
 
Example #12
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Nullable
public KsDef describeKeyspace(String keyspace) {
    try {
        return _client.describe_keyspace(keyspace);
    } catch (Exception e) {
        return null;  // Does not exist
    }
}
 
Example #13
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 #14
Source File: KSMetaData.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static KSMetaData fromThrift(KsDef ksd, CFMetaData... cfDefs) throws ConfigurationException
{
    Class<? extends AbstractReplicationStrategy> cls = AbstractReplicationStrategy.getClass(ksd.strategy_class);
    if (cls.equals(LocalStrategy.class))
        throw new ConfigurationException("Unable to use given strategy class: LocalStrategy is reserved for internal use.");

    return new KSMetaData(ksd.name,
                          cls,
                          ksd.strategy_options == null ? Collections.<String, String>emptyMap() : ksd.strategy_options,
                          ksd.durable_writes,
                          Arrays.asList(cfDefs));
}
 
Example #15
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 #16
Source File: CassandraConnection.java    From learning-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Get a keyspace definition for the set keyspace
 * 
 * @return a keyspace definition
 * @throws Exception if a problem occurs
 */
public KsDef describeKeyspace() throws Exception {
  if (m_keyspaceName == null || m_keyspaceName.length() == 0) {
    throw new Exception("No keyspace has been set!");
  }
  
  return m_client.describe_keyspace(m_keyspaceName);
}
 
Example #17
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
public void systemUpdateKeyspace(KsDef keyspaceDefinition) {
    try {
        _client.system_update_keyspace(keyspaceDefinition);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
 
Example #18
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
public void systemAddKeyspace(KsDef keyspaceDefinition) {
    try {
        _client.system_add_keyspace(keyspaceDefinition);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
 
Example #19
Source File: CliCompiler.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static String getKeySpace(Tree statement, List<KsDef> keyspaces)
{
    return getKeySpace(CliUtils.unescapeSQLString(statement.getChild(0).getText()), keyspaces);
}
 
Example #20
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;
}