コード例 #1
0
  /**
   * Create a Column family for cassandra counters in a given Cluster intance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createCounterColumnFamily(String name, String keySpace, Cluster cluster)
      throws CassandraDataAccessException {
    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }
    ColumnFamilyDefinition cfDef =
        HFactory.createColumnFamilyDefinition(keySpace, name, ComparatorType.COUNTERTYPE);
    cfDef.setComparatorType(ComparatorType.UTF8TYPE);
    cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName());
    cfDef.setColumnType(ColumnType.STANDARD);

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }
コード例 #2
0
  /**
   * Create a Column family in a Given Cluster instance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @param comparatorType Comparator
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createColumnFamily(
      String name, String keySpace, Cluster cluster, String comparatorType)
      throws CassandraDataAccessException {

    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }

    ColumnFamilyDefinition cfDef =
        new ThriftCfDef(keySpace, /*"Queue"*/ name, ComparatorType.getByClassName(comparatorType));

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }