public void createKeyspace(String keyspaceName, int replicationFactor) {
   // If the keyspace definition does not already exist then create it
   if (this._cluster.describeKeyspace(keyspaceName) == null) {
     KeyspaceDefinition ksDefn =
         HFactory.createKeyspaceDefinition(
             keyspaceName,
             ThriftKsDef.DEF_STRATEGY_CLASS,
             replicationFactor,
             new ArrayList<
                 ColumnFamilyDefinition>()); // HFactory.createKeyspaceDefinition( keyspaceName );
     this._cluster.addKeyspace(ksDefn);
   }
 }
Ejemplo n.º 2
0
 void addKeyspacetoCassandra() throws InvalidRequestException, TException {
   try {
     ColumnFamilyDefinition cfDef =
         HFactory.createColumnFamilyDefinition(
             keyspaceName, cfdatapoint, ComparatorType.BYTESTYPE);
     int replicationFactor = 1;
     KeyspaceDefinition newKeyspace =
         HFactory.createKeyspaceDefinition(
             keyspaceName,
             ThriftKsDef.DEF_STRATEGY_CLASS,
             replicationFactor,
             Arrays.asList(cfDef));
     // Add the schema to the cluster.
     myCluster.addKeyspace(newKeyspace);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 3
0
  private void init(Cluster cluster) {
    if (cluster.describeKeyspace("SiddhiSnapshots") == null) {
      log.info("Adding  keyspace SiddhiSnapshots");
      cluster.addKeyspace(HFactory.createKeyspaceDefinition("SiddhiSnapshots"));
      keyspace = HFactory.createKeyspace("SiddhiSnapshots", cluster);
      cluster.addColumnFamily(
          HFactory.createColumnFamilyDefinition(keyspace.getKeyspaceName(), COLUMN_FAMILY_NAME));
      cluster.addColumnFamily(
          HFactory.createColumnFamilyDefinition(
              keyspace.getKeyspaceName(), INDEX_COLUMN_FAMILY_NAME));

    } else {
      if (log.isDebugEnabled()) {
        log.debug("keyspace SiddhiSnapshots exists");
      }
      keyspace = HFactory.createKeyspace("SiddhiSnapshots", cluster);
    }
  }
Ejemplo n.º 4
0
 public static void addKeySpace(Cluster myCluster) {
   List<ColumnFamilyDefinition> cfDefs = new ArrayList<ColumnFamilyDefinition>();
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "global", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign1", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign2", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign3", ComparatorType.getByClassName("DateType")));
   KeyspaceDefinition ksdef =
       HFactory.createKeyspaceDefinition(
           "deneme", "org.apache.cassandra.locator.SimpleStrategy", 1, cfDefs);
   myCluster.addKeyspace(ksdef, true);
 }
Ejemplo n.º 5
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();
    }
  }