boolean checkExist(String cfname) {
   List<ColumnFamilyDefinition> lcf = ksdef.getCfDefs();
   Iterator<ColumnFamilyDefinition> it = lcf.iterator();
   while (it.hasNext()) {
     ColumnFamilyDefinition cf = it.next();
     if (cf.getName().equals(cfname)) return true;
   }
   return false;
 }
 ColumnFamilyDefinition getCfdf(String cfname) {
   cfname = cfname.toLowerCase();
   List<ColumnFamilyDefinition> lcf = ksdef.getCfDefs();
   Iterator<ColumnFamilyDefinition> it = lcf.iterator();
   while (it.hasNext()) {
     ColumnFamilyDefinition cf = it.next();
     if (cf.getName().equals(cfname)) return cf;
   }
   return null;
 }
Example #3
0
  public Map<String, Map<String, String>> getColumnFamilies(String keyspace) {
    KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.toString());
    List<ColumnFamilyDefinition> cfDef = keyspaceDef.getCfDefs();
    Map<String, Map<String, String>> rval = new HashMap<String, Map<String, String>>();
    for (ColumnFamilyDefinition cf : cfDef) {
      String cfurl = keyPrefix + '/' + keyspace + '/' + cf.getName();

      String comment = cf.getComment();
      Map<String, String> commentMap = new HashMap<String, String>();
      commentMap.put("comment", comment);
      rval.put(cfurl, commentMap);
    }
    return rval;
  }
Example #4
0
  public Map getColumnFamily(String keyspace, String cf) throws CharacterCodingException {
    KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.toString());
    List<ColumnFamilyDefinition> cfDef = keyspaceDef.getCfDefs();
    Map rval = new HashMap();
    for (ColumnFamilyDefinition cfentry : cfDef) {
      if (cfentry.getName().equals(cf)) {
        // Handle Metadata
        Charset charset = Charset.forName("UTF8"); // TODO add handling for other types
        CharsetDecoder decoder = charset.newDecoder();
        List<Map<String, String>> cfmetaList = new ArrayList<Map<String, String>>(3);
        for (ColumnDefinition colDef : cfentry.getColumnMetadata()) {
          Map<String, String> cfmeta = new HashMap<String, String>();
          String colName = decoder.decode(colDef.getName()).toString();
          cfmeta.put("column_name", colName);
          cfmeta.put("validation_class", colDef.getValidationClass());
          ColumnIndexType idxType = colDef.getIndexType();
          if (idxType != null) {
            cfmeta.put("index_type", idxType.toString());
            cfmeta.put("index_name", colDef.getIndexName());
          }
          cfmetaList.add(cfmeta);
        }

        rval.put("column_metadata", cfmetaList);
        // rval.put("metadata", cfmeta);
        // Handle page link
        Map<String, String> link = new HashMap<String, String>();
        link.put(
            "href",
            keyPrefix + '/' + keyspace + '/' + cf + "/?" + numRowsVar + "=" + defaultNumRows);
        rval.put("browse", link);
        // Handle search template
        // TODO check for existence of indexed column, use one as example col.
        Map<String, String> search = new HashMap<String, String>();
        search.put("href", keyPrefix + '/' + keyspace + '/' + cf + "[email protected]");
        // TODO I mean it, make the above link better.
        rval.put("search", search);
        break;
      }
    }
    return rval;
  }
Example #5
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);
    }
  }
Example #6
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);
    }
  }
Example #7
0
  /**
   * @param outputTransportMessageConfiguration - topic name to publish messages
   * @param message - is and Object[]{Event, EventDefinition}
   * @param outputTransportAdaptorConfiguration
   * @param tenantId
   */
  public void publish(
      OutputTransportAdaptorMessageConfiguration outputTransportMessageConfiguration,
      Object message,
      OutputTransportAdaptorConfiguration outputTransportAdaptorConfiguration,
      int tenantId) {
    if (message instanceof Map) {

      ConcurrentHashMap<String, TransportAdaptorInfo> cassandraClusterCache =
          tenantedCassandraClusterCache.get(tenantId);
      if (null == cassandraClusterCache) {
        cassandraClusterCache = new ConcurrentHashMap<String, TransportAdaptorInfo>();
        if (null != tenantedCassandraClusterCache.putIfAbsent(tenantId, cassandraClusterCache)) {
          cassandraClusterCache = tenantedCassandraClusterCache.get(tenantId);
        }
      }

      TransportAdaptorInfo transportAdaptorInfo =
          cassandraClusterCache.get(outputTransportAdaptorConfiguration.getName());
      if (null == transportAdaptorInfo) {
        Map<String, String> properties = outputTransportAdaptorConfiguration.getOutputProperties();

        Map<String, String> credentials = new HashMap<String, String>();
        credentials.put(
            "username",
            properties.get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_USER_NAME));
        credentials.put(
            "password",
            properties.get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_PASSWORD));

        Cluster cluster =
            HFactory.createCluster(
                properties.get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_CLUSTER_NAME),
                new CassandraHostConfigurator(
                    properties.get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_HOSTNAME)
                        + ":"
                        + properties.get(
                            CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_PORT)),
                credentials);

        String indexAllColumnsString =
            properties.get(
                CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_INDEX_ALL_COLUMNS);
        boolean indexAllColumns = false;
        if (indexAllColumnsString != null && indexAllColumnsString.equals("true")) {
          indexAllColumns = true;
        }
        transportAdaptorInfo = new TransportAdaptorInfo(cluster, indexAllColumns);
        if (null
            != cassandraClusterCache.putIfAbsent(
                outputTransportAdaptorConfiguration.getName(), transportAdaptorInfo)) {
          transportAdaptorInfo =
              cassandraClusterCache.get(outputTransportAdaptorConfiguration.getName());
        } else {
          log.info("Initiated Cassandra Writer " + outputTransportAdaptorConfiguration.getName());
        }
      }

      String keySpaceName =
          outputTransportMessageConfiguration
              .getOutputMessageProperties()
              .get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_KEY_SPACE_NAME);
      String columnFamilyName =
          outputTransportMessageConfiguration
              .getOutputMessageProperties()
              .get(CassandraTransportAdaptorConstants.TRANSPORT_CASSANDRA_COLUMN_FAMILY_NAME);
      MessageInfo messageInfo =
          transportAdaptorInfo.getMessageInfoMap().get(outputTransportMessageConfiguration);
      if (null == messageInfo) {
        Keyspace keyspace =
            HFactory.createKeyspace(keySpaceName, transportAdaptorInfo.getCluster());
        messageInfo = new MessageInfo(keyspace);
        if (null
            != transportAdaptorInfo
                .getMessageInfoMap()
                .putIfAbsent(outputTransportMessageConfiguration, messageInfo)) {
          messageInfo =
              transportAdaptorInfo.getMessageInfoMap().get(outputTransportMessageConfiguration);
        }
      }

      if (transportAdaptorInfo.getCluster().describeKeyspace(keySpaceName) == null) {
        BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
        columnFamilyDefinition.setKeyspaceName(keySpaceName);
        columnFamilyDefinition.setName(columnFamilyName);
        columnFamilyDefinition.setComparatorType(ComparatorType.UTF8TYPE);
        columnFamilyDefinition.setDefaultValidationClass(ComparatorType.UTF8TYPE.getClassName());
        columnFamilyDefinition.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());

        ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);

        KeyspaceDefinition keyspaceDefinition =
            HFactory.createKeyspaceDefinition(
                keySpaceName,
                "org.apache.cassandra.locator.SimpleStrategy",
                1,
                Arrays.asList(cfDef));
        transportAdaptorInfo.getCluster().addKeyspace(keyspaceDefinition);
        KeyspaceDefinition fromCluster =
            transportAdaptorInfo.getCluster().describeKeyspace(keySpaceName);
        messageInfo.setColumnFamilyDefinition(
            new BasicColumnFamilyDefinition(fromCluster.getCfDefs().get(0)));
      } else {
        KeyspaceDefinition fromCluster =
            transportAdaptorInfo.getCluster().describeKeyspace(keySpaceName);
        for (ColumnFamilyDefinition columnFamilyDefinition : fromCluster.getCfDefs()) {
          if (columnFamilyDefinition.getName().equals(columnFamilyName)) {
            messageInfo.setColumnFamilyDefinition(
                new BasicColumnFamilyDefinition(columnFamilyDefinition));
            break;
          }
        }
      }

      Mutator<String> mutator = HFactory.createMutator(messageInfo.getKeyspace(), sser);
      String uuid = UUID.randomUUID().toString();
      for (Map.Entry<String, Object> entry : ((Map<String, Object>) message).entrySet()) {

        if (transportAdaptorInfo.isIndexAllColumns()
            && !messageInfo.getColumnNames().contains(entry.getKey())) {
          BasicColumnFamilyDefinition columnFamilyDefinition =
              messageInfo.getColumnFamilyDefinition();
          BasicColumnDefinition columnDefinition = new BasicColumnDefinition();
          columnDefinition.setName(StringSerializer.get().toByteBuffer(entry.getKey()));
          columnDefinition.setIndexType(ColumnIndexType.KEYS);
          columnDefinition.setIndexName(
              keySpaceName + "_" + columnFamilyName + "_" + entry.getKey() + "_Index");
          columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());
          columnFamilyDefinition.addColumnDefinition(columnDefinition);
          transportAdaptorInfo
              .getCluster()
              .updateColumnFamily(new ThriftCfDef(columnFamilyDefinition));
          messageInfo.getColumnNames().add(entry.getKey());
        }
        mutator.insert(
            uuid,
            columnFamilyName,
            HFactory.createStringColumn(entry.getKey(), entry.getValue().toString()));
      }

      mutator.execute();
    }
  }