示例#1
0
int main(int argc, char* argv[]) {
  CassFuture* connect_future = NULL;
  CassCluster* cluster = cass_cluster_new();
  CassSession* session = cass_session_new();
  char* hosts = "127.0.0.1";
  if (argc > 1) {
    hosts = argv[1];
  }
  cass_cluster_set_contact_points(cluster, hosts);

  connect_future = cass_session_connect(session, cluster);

  if (cass_future_error_code(connect_future) == CASS_OK) {
    CassFuture* close_future = NULL;

    execute_query(session,
                  "CREATE KEYSPACE examples WITH replication = { \
                  'class': 'SimpleStrategy', 'replication_factor': '3' };");

    print_keyspace(session, "examples");

    execute_query(session,
                  "CREATE TABLE examples.schema_meta (key text, \
                  value bigint, \
                  PRIMARY KEY (key));");

    execute_query(session,
                  "CREATE FUNCTION examples.avg_state(state tuple<int, bigint>, val int) \
                  CALLED ON NULL INPUT RETURNS tuple<int, bigint> \
                  LANGUAGE java AS \
                    'if (val != null) { \
                      state.setInt(0, state.getInt(0) + 1); \
                      state.setLong(1, state.getLong(1) + val.intValue()); \
                    } \
示例#2
0
  CassandraFuture connectToCluster(const std::string &contactPoints) const {
    /* Add contact points */
    cass_cluster_set_contact_points(cluster_.get(), contactPoints.c_str());

    /* Provide the cluster object as configuration to connect the session */
    return createFuture(cass_session_connect(session_.get(), cluster_.get()));
  }
示例#3
0
int main() {
  CassFuture* connect_future = NULL;
  CassCluster* cluster = cass_cluster_new();
  CassSession* session = cass_session_new();
  cass_cluster_set_contact_points(cluster, "127.0.0.1");

  connect_future = cass_session_connect(session, cluster);

  if (cass_future_error_code(connect_future) == CASS_OK) {
    CassFuture* close_future = NULL;

    execute_query(session,
                  "CREATE KEYSPACE examples WITH replication = { \
                  'class': 'SimpleStrategy', 'replication_factor': '3' };");

    print_keyspace(session, "examples");

    execute_query(session,
                  "CREATE TABLE examples.schema_meta (key text, \
                  value bigint, \
                  PRIMARY KEY (key));");

    print_table(session, "examples", "schema_meta");

    /* Close the session */
    close_future = cass_session_close(session);
    cass_future_wait(close_future);
    cass_future_free(close_future);
  } else {
示例#4
0
void DbCassandra::Initialize( const std::string &configPath )
{
    ParseConfig( configPath );
    const std::string remotes = config_.get<std::string>( "remotes" );

    db_ = std::make_shared<DbConnection>();

    auto cluster = cass_cluster_new();
    db_->SetCluster( cluster );
    auto session = cass_session_new();
    db_->SetSession( session );

    cass_cluster_set_contact_points( cluster, remotes.c_str() );

    CassFuture *connect_future = cass_session_connect( session, cluster );
    const CassError err = cass_future_error_code( connect_future );
    cass_future_free( connect_future );
    if ( err != CASS_OK )
        throw std::logic_error( cass_error_desc( err ) );

    const CassPrepared *prepared = nullptr;
    if ( PrepareQuery( session, "INSERT INTO prun.jobs (job_id, job_descr) VALUES (?, ?);", reinterpret_cast<const CassPrepared**>( &prepared ) ) != CASS_OK )
        throw std::logic_error( cass_error_desc( err ) );
    db_->SetPreparedInsert( prepared );

    if ( PrepareQuery( session, "DELETE FROM prun.jobs WHERE job_id = ?;", reinterpret_cast<const CassPrepared**>( &prepared ) ) != CASS_OK )
        throw std::logic_error( cass_error_desc( err ) );
    db_->SetPreparedDelete( prepared );
}
示例#5
0
CassCluster* create_cluster() {
  CassCluster* cluster = cass_cluster_new();
  cass_cluster_set_contact_points(cluster, "127.0.0.1");
  cass_cluster_set_credentials(cluster, "cassandra", "cassandra");
  cass_cluster_set_num_threads_io(cluster, NUM_IO_WORKER_THREADS);
  cass_cluster_set_queue_size_io(cluster, 10000);
  cass_cluster_set_pending_requests_low_water_mark(cluster, 5000);
  cass_cluster_set_pending_requests_high_water_mark(cluster, 10000);
  cass_cluster_set_core_connections_per_host(cluster, 1);
  cass_cluster_set_max_connections_per_host(cluster, 2);
  cass_cluster_set_max_requests_per_flush(cluster, 10000);
  return cluster;
}
  /**
   * Get a session that is only connected to the given node.
   *
   * @param node The node the session should be connected to
   * @return The connected session
   */
  const test_utils::CassSessionPtr& session_for_node(int node) {
    if (node >= static_cast<int>(sessions.size()) || !sessions[node]) {
      sessions.resize(node + 1);

      std::stringstream ip_address;
      ip_address << ccm->get_ip_prefix() << node;

      test_utils::CassClusterPtr cluster(cass_cluster_new());
      cass_cluster_set_contact_points(cluster.get(), ip_address.str().c_str());
      cass_cluster_set_whitelist_filtering(cluster.get(), ip_address.str().c_str());
      sessions[node] = test_utils::create_session(cluster.get());
    }

    return sessions[node];
  }
示例#7
0
bool Connection::open()
{
	if (_is_open) {
		return true;
	}

	CassCluster* cluster = cass_cluster_new();

	cass_cluster_set_protocol_version(cluster, 2);
	cass_cluster_set_contact_points(cluster, _host_string);
	cass_cluster_set_port(cluster, _port);

	CassFuture* future = cass_cluster_connect(cluster);

	_session = cass_future_get_session(future);
	_is_open = cass_future_error_code(future) == CASS_OK && _session != nullptr;

	cass_future_free(future);
	cass_cluster_free(cluster);

	return _is_open;
}
void CassDriver::CreateCluster() {
  int pos = FLAGS_contact_ip_and_port.find(':');
  if (pos == std::string::npos) {
    printf("Wrong format in contact_ip_and_port, use ip:port !\n");
    return;
  }
  std::string contact_point = FLAGS_contact_ip_and_port.substr(0, pos);
  std::string str_port = FLAGS_contact_ip_and_port.substr(pos + 1);
  int port = std::atoi(str_port.c_str());
  cluster_ = cass_cluster_new();
  cass_cluster_set_contact_points(cluster_, contact_point.c_str());
  cass_cluster_set_port(cluster_, port);
  cass_cluster_set_log_level(cluster_, ParseLogLevel(FLAGS_log_level));
  cass_cluster_set_num_threads_io(cluster_, FLAGS_num_threads_io);
  cass_cluster_set_queue_size_io(cluster_, FLAGS_queue_size_io);
  cass_cluster_set_pending_requests_low_water_mark(cluster_,
                                                   FLAGS_pending_req_low);
  cass_cluster_set_pending_requests_high_water_mark(cluster_,
                                                   FLAGS_pending_req_high);
  cass_cluster_set_core_connections_per_host(cluster_, FLAGS_core_connections);
  cass_cluster_set_max_connections_per_host(cluster_, FLAGS_max_connections);
}
CassError CassandraClient::connect(const std::string nodes) {
    CassError rc = CASS_OK;
    logger.information("Connecting to " + nodes);

    cluster = cass_cluster_new();
    session = cass_session_new();

    CassFuture* connect_future = NULL;
    // Optional: use multiple nodes for better load balancing. Just enter more IPs here /re
    // cass_cluster_set_contact_points(cluster, "127.0.0.1");
    cass_cluster_set_contact_points(cluster, nodes.c_str());
    connect_future = cass_session_connect(session, cluster);
    cass_future_wait(connect_future);

    rc = cass_future_error_code(connect_future);
    if (rc == CASS_OK) {
        logger.information("Connected!");
    } else {
        return printError(rc);
    }
    cass_future_free(connect_future);
    return rc;
}
示例#10
0
CassCluster* create_cluster() {
  CassCluster* cluster = cass_cluster_new();
  cass_cluster_set_contact_points(cluster, "127.0.0.1,127.0.0.2,127.0.0.3");
  return cluster;
}
示例#11
0
文件: ssl.c 项目: PlexChat/cpp-driver
int main(int argc, char* argv[]) {
  /* Setup and connect to cluster */
  CassFuture* connect_future = NULL;
  CassCluster* cluster = cass_cluster_new();
  CassSession* session = cass_session_new();
  CassSsl* ssl = cass_ssl_new();
  char* hosts = "127.0.0.1";
  if (argc > 1) {
    hosts = argv[1];
  }

  cass_cluster_set_contact_points(cluster, hosts);

  /* Only verify the certification and not the identity */
  cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_PEER_CERT);

  if (!load_trusted_cert_file("cert.pem", ssl)) {
    fprintf(stderr, "Failed to load certificate disabling peer verification\n");
    cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_NONE);
  }

  cass_cluster_set_ssl(cluster, ssl);

  connect_future = cass_session_connect(session, cluster);

  if (cass_future_error_code(connect_future) == CASS_OK) {
    CassFuture* close_future = NULL;

    /* Build statement and execute query */
    const char* query = "SELECT release_version FROM system.local";
    CassStatement* statement = cass_statement_new(query, 0);

    CassFuture* result_future = cass_session_execute(session, statement);

    if (cass_future_error_code(result_future) == CASS_OK) {
      /* Retrieve result set and get the first row */
      const CassResult* result = cass_future_get_result(result_future);
      const CassRow* row = cass_result_first_row(result);

      if (row) {
        const CassValue* value = cass_row_get_column_by_name(row, "release_version");

        const char* release_version;
        size_t release_version_length;
        cass_value_get_string(value, &release_version, &release_version_length);
        printf("release_version: '%.*s'\n", (int)release_version_length,
               release_version);
      }

      cass_result_free(result);
    } else {
      /* Handle error */
      const char* message;
      size_t message_length;
      cass_future_error_message(result_future, &message, &message_length);
      fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length,
                                                            message);
    }

    cass_statement_free(statement);
    cass_future_free(result_future);

    /* Close the session */
    close_future = cass_session_close(session);
    cass_future_wait(close_future);
    cass_future_free(close_future);
  } else {
      /* Handle error */
      const char* message;
      size_t message_length;
      cass_future_error_message(connect_future, &message, &message_length);
      fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length,
                                                          message);
  }

  cass_future_free(connect_future);
  cass_cluster_free(cluster);
  cass_session_free(session);
  cass_ssl_free(ssl);

  return 0;
}
示例#12
0
 /**
  * Assign/Append the contact points; passing an empty string will clear
  * the contact points
  *
  * @param contact_points A comma delimited list of hosts (addresses or
  *                       names
  * @return Cluster object
  */
 Cluster& with_contact_points(const std::string& contact_points) {
   EXPECT_EQ(CASS_OK, cass_cluster_set_contact_points(get(),
     contact_points.c_str()));
   return *this;
 }
示例#13
0
CassCluster* create_cluster(const char* hosts) {
  CassCluster* cluster = cass_cluster_new();
  cass_cluster_set_contact_points(cluster, hosts);
  return cluster;
}
示例#14
0
int main(int argc, char* argv[]) {
  /* Setup and connect to cluster */
  CassFuture* connect_future = NULL;
  CassCluster* cluster = cass_cluster_new();
  CassSession* session = cass_session_new();
  char* hosts = "127.0.0.1";
  if (argc > 1) {
    hosts = argv[1];
  }

  /* Add contact points */
  cass_cluster_set_contact_points(cluster, hosts);

  /* Provide the cluster object as configuration to connect the session */
  connect_future = cass_session_connect(session, cluster);

  if (cass_future_error_code(connect_future) == CASS_OK) {
    CassFuture* close_future = NULL;

    /* Build statement and execute query */
    const char* query = "SELECT keyspace_name "
                        "FROM system.schema_keyspaces;";
    CassStatement* statement = cass_statement_new(query, 0);

    CassFuture* result_future = cass_session_execute(session, statement);

    if (cass_future_error_code(result_future) == CASS_OK) {
      /* Retrieve result set and iterate over the rows */
      const CassResult* result = cass_future_get_result(result_future);
      CassIterator* rows = cass_iterator_from_result(result);

      while (cass_iterator_next(rows)) {
        const CassRow* row = cass_iterator_get_row(rows);
        const CassValue* value = cass_row_get_column_by_name(row, "keyspace_name");

        const char* keyspace_name;
        size_t keyspace_name_length;
        cass_value_get_string(value, &keyspace_name, &keyspace_name_length);
        printf("keyspace_name: '%.*s'\n", (int)keyspace_name_length,
                                               keyspace_name);
      }

      cass_result_free(result);
      cass_iterator_free(rows);
    } else {
      /* Handle error */
      const char* message;
      size_t message_length;
      cass_future_error_message(result_future, &message, &message_length);
      fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length,
                                                            message);
    }

    cass_statement_free(statement);
    cass_future_free(result_future);

    /* Close the session */
    close_future = cass_session_close(session);
    cass_future_wait(close_future);
    cass_future_free(close_future);
  } else {
    /* Handle error */
    const char* message;
    size_t message_length;
    cass_future_error_message(connect_future, &message, &message_length);
    fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length,
                                                        message);
  }

  cass_future_free(connect_future);
  cass_cluster_free(cluster);
  cass_session_free(session);

  return 0;
}
示例#15
0
/* functions implementing storage api */
static int
librdf_storage_cassandra_init(librdf_storage* storage, const char *name,
                           librdf_hash* options)
{

    char *name_copy;
    librdf_storage_cassandra_instance* context;
  
    if(!name) {
	if(options)
	    librdf_free_hash(options);
	return 1;
    }
  
    context = LIBRDF_CALLOC(librdf_storage_cassandra_instance*, 1,
			    sizeof(*context));
    if(!context) {
	if(options)
	    librdf_free_hash(options);
	return 1;
    }

    librdf_storage_set_instance(storage, context);
  
    context->storage = storage;
    context->name_len = strlen(name);
//    context->transaction = 0;

    name_copy = LIBRDF_MALLOC(char*, context->name_len + 1);
    if(!name_copy) {
	if(options)
	    librdf_free_hash(options);
	return 1;
    }

    strcpy(name_copy, name);
    context->name = name_copy;

    // Add options here.

    /* no more options, might as well free them now */
    if(options)
	librdf_free_hash(options);

    // FIXME: Hard-coded;
    context->session = cass_session_new();
    context->cluster = cass_cluster_new();

    cass_cluster_set_contact_points(context->cluster, name);

    CassFuture* future = cass_session_connect(context->session,
					      context->cluster);

    CassError rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
	fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc));
	cass_cluster_free(context->cluster);
	cass_session_free(context->session);
	free(context->name);
	free(context);
	return 1;
    }

    cass_future_free(future);

    return 0;

}
示例#16
0
void initialize_contact_points(CassCluster* cluster, std::string prefix, unsigned int num_nodes_dc1, unsigned int num_nodes_dc2) {
  for (unsigned int i = 0; i < num_nodes_dc1; ++i) {
    std::string contact_point(prefix + boost::lexical_cast<std::string>(i + 1));
    cass_cluster_set_contact_points(cluster, contact_point.c_str());
  }
}