Java Code Examples for com.datastax.driver.core.querybuilder.Insert#value()

The following examples show how to use com.datastax.driver.core.querybuilder.Insert#value() . 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: CassandraPageSink.java    From presto with Apache License 2.0 5 votes vote down vote up
public CassandraPageSink(
        CassandraSession cassandraSession,
        ProtocolVersion protocolVersion,
        String schemaName,
        String tableName,
        List<String> columnNames,
        List<Type> columnTypes,
        boolean generateUuid)
{
    this.cassandraSession = requireNonNull(cassandraSession, "cassandraSession");
    requireNonNull(schemaName, "schemaName is null");
    requireNonNull(tableName, "tableName is null");
    requireNonNull(columnNames, "columnNames is null");
    this.columnTypes = ImmutableList.copyOf(requireNonNull(columnTypes, "columnTypes is null"));
    this.generateUuid = generateUuid;

    if (protocolVersion.toInt() <= ProtocolVersion.V3.toInt()) {
        this.toCassandraDate = value -> DATE_FORMATTER.print(TimeUnit.DAYS.toMillis(value));
    }
    else {
        this.toCassandraDate = value -> LocalDate.fromDaysSinceEpoch(toIntExact(value));
    }

    Insert insert = insertInto(validSchemaName(schemaName), validTableName(tableName));
    if (generateUuid) {
        insert.value(ID_COLUMN_NAME, bindMarker());
    }
    for (int i = 0; i < columnNames.size(); i++) {
        String columnName = columnNames.get(i);
        checkArgument(columnName != null, "columnName is null at position: %s", i);
        insert.value(validColumnName(columnName), bindMarker());
    }
    this.insert = cassandraSession.prepare(insert);
}
 
Example 2
Source File: CassandraEventRecorder.java    From eventapis with Apache License 2.0 5 votes vote down vote up
private Insert createInsertQuery(EntityEvent entityEvent) {
    Insert insert = QueryBuilder.insertInto(tableName);
    insert.value(ENTITY_ID, entityEvent.getEventKey().getEntityId());
    insert.value(VERSION, entityEvent.getEventKey().getVersion());
    insert.value(OP_ID, entityEvent.getOpId());
    insert.value(OP_DATE, entityEvent.getOpDate());
    insert.value(EVENT_TYPE, entityEvent.getEventType());
    insert.value(STATUS, entityEvent.getStatus().name());
    insert.value(AUDIT_INFO, entityEvent.getAuditInfo());
    insert.value(EVENT_DATA, entityEvent.getEventData());
    insert.ifNotExists();
    return insert;
}
 
Example 3
Source File: WordCountAndSourceMapper.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
@Override
public Statement map(List<String> keys, Number value) {
    Insert statement = QueryBuilder.insertInto(KEYSPACE_NAME, TABLE_NAME);
    statement.value(WORD_KEY_NAME, keys.get(0));
    statement.value(SOURCE_KEY_NAME, keys.get(1));
    statement.value(VALUE_NAME, value);
    return statement;
}
 
Example 4
Source File: SaveToCassandraOperationsService.java    From Decision with Apache License 2.0 5 votes vote down vote up
public Insert createInsertStatement(String streamName, List<ColumnNameTypeValue> columns, String timestampColumnName) {
    Insert insert = QueryBuilder.insertInto(addQuotes(STREAMING.STREAMING_KEYSPACE_NAME), addQuotes(streamName));
    for (ColumnNameTypeValue column : columns) {
        insert.value(addQuotes(column.getColumn()), column.getValue());
    }
    insert.value(addQuotes(timestampColumnName), UUIDs.timeBased());
    return insert;
}
 
Example 5
Source File: CassandraTable.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public void save(final List<Event> events) {
  final BatchStatement batch = new BatchStatement();
  for (final Event event : events) {
    final Map<String, Object> parsedEvent = parse(event);
    if (parsedEvent.isEmpty()) {
      log.warn("Event {} could not be mapped. Suggestion: Cassandra is case sensitive, so maybe you can check field names.", event);
      continue;
    }
    if (!hasPrimaryKey(parsedEvent)) {
      break;
    }
    final Insert insert = QueryBuilder.insertInto(table);
    for (final Map.Entry<String, Object> entry : parsedEvent.entrySet()) {
      insert.value(entry.getKey(), entry.getValue());
    }
    if (log.isTraceEnabled()) {
      log.trace("Preparing insert for table {}: {}", table.getName(), insert.getQueryString());
    }
    batch.add(insert);
  }
  if (batch.getStatements().isEmpty()) {
    log.warn("No event produced insert query for table {}", table.getName());
    return;
  }
  batch.setConsistencyLevel(consistencyLevel);
  session.execute(batch);
}
 
Example 6
Source File: CassandraMetadataWriter.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final GeoWaveMetadata metadata) {
  final Insert insert = operations.getInsert(tableName);
  insert.value(PRIMARY_ID_KEY, ByteBuffer.wrap(metadata.getPrimaryId()));
  if (metadata.getSecondaryId() != null) {
    insert.value(SECONDARY_ID_KEY, ByteBuffer.wrap(metadata.getSecondaryId()));
    insert.value(TIMESTAMP_ID_KEY, QueryBuilder.now());
    if ((metadata.getVisibility() != null) && (metadata.getVisibility().length > 0)) {
      insert.value(VISIBILITY_KEY, ByteBuffer.wrap(metadata.getVisibility()));
    }
  }

  insert.value(VALUE_KEY, ByteBuffer.wrap(metadata.getValue()));
  operations.getSession().execute(insert);
}
 
Example 7
Source File: PutCassandraRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Statement generateInsert(String cassandraTable, RecordSchema schema, Map<String, Object> recordContentMap) {
    Insert insertQuery;
    if (cassandraTable.contains(".")) {
        String[] keyspaceAndTable = cassandraTable.split("\\.");
        insertQuery = QueryBuilder.insertInto(keyspaceAndTable[0], keyspaceAndTable[1]);
    } else {
        insertQuery = QueryBuilder.insertInto(cassandraTable);
    }
    for (String fieldName : schema.getFieldNames()) {
        insertQuery.value(fieldName, recordContentMap.get(fieldName));
    }
    return insertQuery;
}
 
Example 8
Source File: CassandraVacationDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private <T> Insert applyPatchForField(String field, Optional<T> value, Insert insert) {
    return insert.value(field, value.orElse(null));
}
 
Example 9
Source File: CassandraPersistWriter.java    From streams with Apache License 2.0 4 votes vote down vote up
private void createInsertStatement() {
  Insert insertBuilder = QueryBuilder.insertInto(config.getTable());
  insertBuilder.value(config.getPartitionKeyColumn(), new Object());
  insertBuilder.value(config.getColumn(), new Object());
  insertStatement = session.prepare(insertBuilder.getQueryString());
}