Java Code Examples for com.datastax.oss.driver.api.core.cql.PreparedStatement#bind()

The following examples show how to use com.datastax.oss.driver.api.core.cql.PreparedStatement#bind() . 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: ScannerImpl.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@Override
public void activate() {
  super.activate();
  if (isRemembering() || isHashing()) {
    CqlSession session = getCassandra().getSession();
    List<DocKey> strandedDocs = new ArrayList<>();
    PreparedStatement preparedQuery = getCassandra().getPreparedQuery(RESET_PROCESSING_Q);
    BoundStatement statement =  preparedQuery.bind(getName());
    ResultSet procRs = session.execute(statement);
    strandedDocs.addAll(procRs.all().stream()
        .map((row) -> new DocKey(row.getString(0), row.getString(1))).collect(Collectors.toList()));
    preparedQuery = getCassandra().getPreparedQuery(RESET_ERROR_Q);
    statement = preparedQuery.bind(getName());
    ResultSet errorRs = session.execute(statement);
    strandedDocs.addAll(errorRs.all().stream()
        .map((row) -> new DocKey(row.getString(0), row.getString(1))).collect(Collectors.toList()));
    preparedQuery = getCassandra().getPreparedQuery(RESET_BATCHED_Q);
    statement = preparedQuery.bind(getName());
    ResultSet batchedRs = session.execute(statement);
    strandedDocs.addAll(batchedRs.all().stream()
        .map((row) -> new DocKey(row.getString(0), row.getString(1))).collect(Collectors.toList()));

    preparedQuery = getCassandra().getPreparedQuery(RESET_DOCS_U);
    // todo: batch
    for (DocKey docId : strandedDocs) {
      statement = preparedQuery.bind(docId.docid, docId.scanner);
      session.execute(statement);
    }

  }
}
 
Example 2
Source File: ScannerImpl.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@Override
public void sendToNext(Document doc) {
  if (isRemembering()) {
    CqlSession session = getCassandra().getSession();
    PreparedStatement preparedQuery = getCassandra().getPreparedQuery(UPDATE_HASH_U);
    BoundStatement bind = preparedQuery.bind(doc.getHash(), doc.getId(), doc.getSourceScannerName());
    session.execute(bind);
  }
  superSendToNext(doc);
}
 
Example 3
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public DynamoDBResponse deleteItem(DeleteItemRequest dir) {
    logger.debug("delete item into JSON table");
    String tableName = dir.getTableName();
    TableDef tableDef = cassandraManager.getTableDef(tableName);

    PreparedStatement deleteStatement = tableDef.getDeleteStatement();

    AttributeDefinition partitionKeyAttr = tableDef.getPartitionKey();
    Optional<AttributeDefinition> maybeCusteringKeyAttr = tableDef.getClusteringKey();

    Map<String, AttributeValue> keys = dir.getKey();

    Object partitionKeyValue = getAttributeObject(
            ScalarAttributeType.fromValue(partitionKeyAttr.getAttributeType()),
            keys.get(partitionKeyAttr.getAttributeName())
    );

    BoundStatement boundStatement;

    if (maybeCusteringKeyAttr.isPresent())
    {
        Object clusteringKeyValue = getAttributeObject(
                ScalarAttributeType.fromValue(maybeCusteringKeyAttr.get().getAttributeType()),
                keys.get(maybeCusteringKeyAttr.get().getAttributeName())
        );

        boundStatement = deleteStatement.bind(partitionKeyValue, clusteringKeyValue);
    }
    else
    {
        boundStatement = deleteStatement.bind(partitionKeyValue);
    }

    ResultSet result = session().execute(boundStatement);

    if (result.wasApplied()){
        DeleteItemResult dres = new DeleteItemResult();
        return new DynamoDBResponse(dres, 200);
    }
    else return null;

}
 
Example 4
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public DynamoDBResponse getItem(GetItemRequest getItemRequest) {
    logger.debug("get item from JSON table");

    String tableName = getItemRequest.getTableName();
    TableDef tableDef = cassandraManager.getTableDef(tableName);
    PreparedStatement selectStatement = tableDef.getQueryRowStatement();

    AttributeDefinition partitionKeyDef = tableDef.getPartitionKey();
    Optional<AttributeDefinition> clusteringKeyDef = tableDef.getClusteringKey();

    Map<String, AttributeValue> keys = getItemRequest.getKey();

    AttributeValue partitionKey = keys.get(partitionKeyDef.getAttributeName());
    AttributeValue clusteringKey = clusteringKeyDef.isPresent() ?
            keys.get(clusteringKeyDef.get().getAttributeName()) : null;

    ScalarAttributeType partitionKeyType = ScalarAttributeType.valueOf(partitionKeyDef.getAttributeType());
    ScalarAttributeType clusteringKeyType = clusteringKeyDef.isPresent() ?
            ScalarAttributeType.valueOf(clusteringKeyDef.get().getAttributeType()) : null;

    BoundStatement boundStatement = clusteringKey == null ?
            selectStatement.bind(getAttributeObject(partitionKeyType, partitionKey)) :
            selectStatement.bind(getAttributeObject(partitionKeyType, partitionKey),
                    getAttributeObject(clusteringKeyType, clusteringKey));

    ResultSet result = session().execute(boundStatement);

    GetItemResult gir = new GetItemResult();
    Map<String, AttributeValue> item = new HashMap<>();
    ColumnDefinitions colDefs = result.getColumnDefinitions();

    Row row = result.one();

    //Case that nothing is found
    if (row == null)
        return new DynamoDBResponse(null, 200);

    Map<String, AttributeValue> keysSet = new HashMap<>();
    for (ColumnDefinition colDef : colDefs)
    {
        if (colDef.getName().asInternal().equals("json_blob"))
            continue;

        keysSet.put(colDef.getName().asInternal(), rowToAV(colDef, row));
    }

    try
    {
        item = blobToItemSet(row.getString("json_blob"));
        item.putAll(keysSet);

        gir.withItem(item);
        return new DynamoDBResponse(gir, 200);
    } catch (IOException e) {
        DynamoDBResponse ddbResponse = new DynamoDBResponse(gir, 500);
        String msg = String.format("GetItem failed", getItemRequest.getTableName());
        ddbResponse.setError(msg);
        return ddbResponse;
    }
}
 
Example 5
Source File: Cassandra4Test.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
private static void createKeyspace(final CqlSession session) {
  final PreparedStatement prepared = session.prepare("CREATE keyspace test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};");
  final BoundStatement bound = prepared.bind();
  session.execute(bound);
}