Example #1
0
CassError insert_into_batch_with_prepared(CassSession* session, const CassPrepared* prepared, const Pair* pairs) {
  CassError rc = CASS_OK;
  CassFuture* future = NULL;
  CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);

  const Pair* pair;
  for (pair = pairs; pair->key != NULL; pair++) {
    CassStatement* statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, pair->key);
    cass_statement_bind_string(statement, 1, pair->value);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement = cass_statement_new("INSERT INTO examples.pairs (key, value) VALUES ('c', '3')", 0);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement = cass_statement_new("INSERT INTO examples.pairs (key, value) VALUES (?, ?)", 2);
    cass_statement_bind_string(statement, 0, "d");
    cass_statement_bind_string(statement, 1, "4");
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  future = cass_session_execute_batch(session, batch);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    print_error(future);
  }

  cass_future_free(future);
  cass_batch_free(batch);

  return rc;
}
 void execute(CassSession *session, const char *what)
 {
     CassandraFuture futureBatch(cass_session_execute_batch(session, batch));
     futureBatch.wait(what);
 }
static int
librdf_storage_cassandra_add_statements(librdf_storage* storage,
                                     librdf_stream* statement_stream)
{


    librdf_storage_cassandra_instance* context;
    context = (librdf_storage_cassandra_instance*)storage->instance;

    CassBatch* batch = 0;

    const int batch_size = 10;
    int rows = 0;

    for(; !librdf_stream_end(statement_stream);
	librdf_stream_next(statement_stream)) {

	librdf_statement* statement;
	librdf_node* context_node;
    
	statement = librdf_stream_get_object(statement_stream);
	context_node = librdf_stream_get_context2(statement_stream);

	if(!statement) {
	    break;
	}

	char* s;
	char* p;
	char* o;
	char* c;
	statement_helper(storage, statement, context_node, &s, &p, &o, &c);

	if (batch == 0)
	    batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);

	char* query = "INSERT INTO rdf.spo (s, p, o) VALUES (?, ?, ?);";
	CassStatement* stmt = cass_statement_new(query, 3);
	cass_statement_bind_string(stmt, 0, s);
	cass_statement_bind_string(stmt, 1, p);
	cass_statement_bind_string(stmt, 2, o);
	cass_batch_add_statement(batch, stmt);
	cass_statement_free(stmt);

	query = "INSERT INTO rdf.pos (s, p, o) VALUES (?, ?, ?);";
	stmt = cass_statement_new(query, 3);
	cass_statement_bind_string(stmt, 0, s);
	cass_statement_bind_string(stmt, 1, p);
	cass_statement_bind_string(stmt, 2, o);
	cass_batch_add_statement(batch, stmt);
	cass_statement_free(stmt);

	query = "INSERT INTO rdf.osp (s, p, o) VALUES (?, ?, ?);";
	stmt = cass_statement_new(query, 3);
	cass_statement_bind_string(stmt, 0, s);
	cass_statement_bind_string(stmt, 1, p);
	cass_statement_bind_string(stmt, 2, o);
	cass_batch_add_statement(batch, stmt);
	cass_statement_free(stmt);

	free(s);
	free(p);
	free(o);

	if (++rows > batch_size) {
	    
	    CassFuture* future = cass_session_execute_batch(context->session,
							    batch);
	    cass_batch_free(batch);

	    CassError rc = cass_future_error_code(future);

	    if (rc != CASS_OK) {
		fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc));
		const char* msg;
		size_t msg_len;
		cass_future_error_message(future, &msg, &msg_len);
		fprintf(stderr, "Cassandra: %*s\n", msg_len, msg);
		cass_future_free(future);
		return -1;
	    }
    
	    cass_future_free(future);

	    batch = 0;

	}

    }
    
    if (batch) {
	    
	CassFuture* future = cass_session_execute_batch(context->session,
							batch);
	cass_batch_free(batch);
	
	CassError rc = cass_future_error_code(future);
	
	if (rc != CASS_OK) {
	    fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc));
	    const char* msg;
	    size_t msg_len;
	    cass_future_error_message(future, &msg, &msg_len);
	    fprintf(stderr, "Cassandra: %*s\n", msg_len, msg);
	    cass_future_free(future);
	    return -1;
	}
	
	cass_future_free(future);

    }

    return 0;

}