Exemple #1
0
  /**
   * Add new Column<long,long> to a given row in a given cassandra column family
   *
   * @param columnFamily column family name
   * @param row row name
   * @param key long key value of the column
   * @param value long value of the column
   * @param keyspace Cassandra KeySpace
   * @throws CassandraDataAccessException
   */
  public static void addLongContentToRow(
      String columnFamily, String row, long key, long value, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no keySpace provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.insert(
          row, columnFamily, HFactory.createColumn(key, value, longSerializer, longSerializer));
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding long content to a row", e);
    }
  }
 public void put(String columnFamily, String key, byte[] value) {
   Mutator<String> mutator = HFactory.createMutator(this._keyspace, StringSerializer.get());
   mutator.insert(
       key,
       columnFamily,
       HFactory.createColumn(
           DEFAULT_COLUMN_NAME, value, StringSerializer.get(), BytesArraySerializer.get()));
 }
 @Override
 public void save(
     PersistenceManagementEvent persistenceManagementEvent,
     String nodeID,
     PersistenceObject persistenceObject) {
   Mutator<String> mutator = HFactory.createMutator(keyspace, sser);
   mutator.insert(
       persistenceManagementEvent.getRevision(),
       COLUMN_FAMILY_NAME,
       HFactory.createColumn(nodeID, ByteSerializer.OToB(persistenceObject), sser, bser));
   mutator.insert(
       persistenceManagementEvent.getExecutionPlanIdentifier(),
       INDEX_COLUMN_FAMILY_NAME,
       HFactory.createColumn(
           persistenceManagementEvent.getRevision(),
           String.valueOf(System.currentTimeMillis()),
           sser,
           sser));
   mutator.execute();
 }
 void insertData(String dpname, Date d, Timestamp u, double value) {
   try {
     long timeInMicroSeconds = u.getTime();
     // UUID timeUUIDColumnName = TimeUUIDUtils.getTimeUUID(timeInMicroSeconds);
     System.out.println(d + "\t" + timeInMicroSeconds + "\t" + value);
     DateSerializer ds = new DateSerializer();
     Mutator<Date> mu = HFactory.createMutator(keyspace, ds);
     mu.insert(d, dpname, HFactory.createColumn(timeInMicroSeconds, value));
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  @Override
  public void insertTatamibotConfiguration(TatamibotConfiguration tatamibotConfiguration) {
    UUID tatamibotConfigurationId = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
    Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get());
    mutator.insert(
        tatamibotConfiguration.getDomain(),
        ColumnFamilyKeys.DOMAIN_TATAMIBOT_CF,
        HFactory.createColumn(
            tatamibotConfigurationId, "", UUIDSerializer.get(), StringSerializer.get()));

    tatamibotConfiguration.setTatamibotConfigurationId(tatamibotConfigurationId.toString());
    em.persist(tatamibotConfiguration);
  }
  @Override
  @CacheEvict(value = "domain-tags-cache", key = "#domain")
  public void addTag(String domain, String tag) {
    HColumn<UUID, String> column =
        HFactory.createColumn(
            TimeUUIDUtils.getUniqueTimeUUIDinMillis(),
            tag,
            COLUMN_TTL,
            UUIDSerializer.get(),
            StringSerializer.get());

    Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get());

    mutator.insert(domain, TRENDS_CF, column);
  }
 @SuppressWarnings("unchecked")
 public void setAmountChunksWithContent(String owner_name, String fileName, long amountChunks) {
   try {
     Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer);
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(
                 HFactory.createStringColumn("chunks_with_content", String.valueOf(amountChunks))),
             stringSerializer,
             stringSerializer,
             stringSerializer));
   } catch (HectorException e) {
     log.error("Data was not inserted");
     e.printStackTrace();
   }
 }
  /**
   * Check if the tags exists and add them if they don't exist.
   *
   * @param keyspaceOperator
   * @param columnfamily
   * @param columnfamilyId
   * @param tags
   * @return
   */
  private int addTags(
      Keyspace keyspaceOperator, String columnfamily, String columnfamilyId, List<String> tags) {

    // Get current user tags
    List<String> currentTags =
        getTags(
            keyspaceOperator,
            columnfamily,
            columnfamilyId,
            TAG_COLUMN_MIN_NAME,
            TAG_COLUMN_MAX_NAME);

    // Get the number of tags of the user
    int nbColumns = currentTags.size();
    int nbAddedTags = 0;

    Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get());

    for (String tag : tags) {
      String tagLowerCase = tag.toLowerCase();

      // Check if the tag already exists
      if (currentTags.contains(tagLowerCase)) {
        // This tag already exists => we don't add it!
        continue;
      }

      // Add the new tag
      nbColumns++;
      String columnName = COLUMN_TAG_PREFIX + nbColumns;

      mutator.insert(
          columnfamilyId,
          columnfamily,
          HFactory.createColumn(
              columnName, tagLowerCase, StringSerializer.get(), StringSerializer.get()));
      nbAddedTags++;
    }

    return nbAddedTags;
  }
 /** Inserts a new row on the UserFiles SuperColumn Family */
 @SuppressWarnings("unchecked")
 public void insertRow(
     String owner_name,
     String fileName,
     String fileID,
     String size,
     String chunks,
     String version,
     int defaultChunkSize) {
   try {
     Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer);
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(HFactory.createStringColumn("file_id", fileID)),
             stringSerializer,
             stringSerializer,
             stringSerializer));
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(HFactory.createStringColumn("size", size)),
             stringSerializer,
             stringSerializer,
             stringSerializer));
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(HFactory.createStringColumn("chunks", chunks)),
             stringSerializer,
             stringSerializer,
             stringSerializer));
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(HFactory.createStringColumn("version", version)),
             stringSerializer,
             stringSerializer,
             stringSerializer));
     mutator.insert(
         owner_name,
         "UserFiles",
         HFactory.createSuperColumn(
             fileName,
             Arrays.asList(
                 HFactory.createStringColumn(
                     "default_chunk_size", String.valueOf(defaultChunkSize))),
             stringSerializer,
             stringSerializer,
             stringSerializer));
   } catch (HectorException e) {
     log.error("Data was not inserted");
     e.printStackTrace();
   }
 }
Exemple #10
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();
    }
  }
Exemple #11
0
 public void setColumn(String keyspace, String cf, String key, String col, String colVal) {
   Keyspace ks = getKeyspaceForName(keyspace);
   Mutator<String> mutator = HFactory.createMutator(ks, s);
   mutator.insert(key, cf, HFactory.createStringColumn(col, colVal));
 }