示例#1
0
def main():
	connection.setup([DB_URL], DB_NAME)

	table_name = sys.argv[1]
	columns = [column.split('=') for column in sys.argv[2:]]

	print(select(table_name, columns))
def open_cassandra_session():
    setup_env()
    connection.setup([CASSANDRA_CONFIG['ip']], CASSANDRA_CONFIG['keyspace'], protocol_version=3)
    SourceUserEntity.sync_table()
    RawRecentMediaEntity.sync_table()
    RawUserEntity.sync_table()
    RawUserRecentMediaEntity.sync_table()
示例#3
0
    def get_new_connection(self, connection_settings):

        contact_points = connection_settings.pop(
            'contact_points',
            self.default_settings['CONTACT_POINTS']
        )
        keyspace = connection_settings.pop(
            'keyspace',
            self.settings_dict['DEFAULT_KEYSPACE']
        )

        self.keyspace = keyspace
        self.session = connection.get_session()
        if not(self.session is None or self.session.is_shutdown):
            return CassandraCursor(self.session)
        connection.setup(
            contact_points,
            keyspace,
            **connection_settings
        )

        self.cluster = connection.get_cluster()
        self.session = connection.get_session()

        return CassandraCursor(self.session)
示例#4
0
def main():
    if len(sys.argv) < 2:
        print('There is no query')
        sys.exit()

    query = sys.argv[1]

    if query == 'docs':
        print(get_docs())
        print('=====')
        return
    
    connection.setup([DB_URL], DB_NAME)
    schema = graphene.Schema(query = FirstQuery, mutation = FirstMutation)
     
    '''
        query MyFirstQuery {
          position(hour : 20, second : 12){
            x,
            y,
            speed,
            time,
            date
          }
        }
    '''
    print(json.dumps(schema.execute(query).data, indent = 4, sort_keys = True))
    print('=====')
示例#5
0
    def get_new_connection(self, connection_settings):
        contact_points = connection_settings.pop(
            'contact_points',
            self.default_settings['CONTACT_POINTS']
        )
        keyspace = connection_settings.pop(
            'keyspace',
            self.settings_dict['DEFAULT_KEYSPACE']
        )

        self.keyspace = keyspace

        try:
            connection.get_connection()

        except CQLEngineException:
            connection.setup(
                contact_points,
                keyspace,
                **connection_settings
            )

        self.session = connection.get_session()
        self.cluster = connection.get_cluster()
        self.session.default_timeout = None  # Should be in config.
        return CassandraCursor(self.session)
示例#6
0
def setup_connection():
    connection.setup(
        hosts=settings.STREAM_CASSANDRA_HOSTS,
        consistency=settings.STREAM_CASSANDRA_CONSISTENCY_LEVEL,
        default_keyspace=settings.STREAM_DEFAULT_KEYSPACE,
        **settings.CASSANDRA_DRIVER_KWARGS
    )
示例#7
0
def main():
	connection.setup([DB_URL], DB_NAME)
	
	#fill_position()
	#fill_system_test()
	#fill_control_action()
	#fill_sensor_data()
	fill_shift_state()
示例#8
0
 def __init__(self):
     self.key_space = "cancer_app"
     connection.setup(["127.0.0.1"], self.key_space, protocol_version=3)
     sync_table(CancerDataEntity)
     sync_table(CancerGenderDeathsEntity)
     sync_table(CancerDeathsEntity)
     sync_table(CancerAgeDeathsEntity)
     sync_table(CancerAgeGenderRiskEntity)
示例#9
0
def main():
	connection.setup([DB_URL], DB_NAME)

	table_name = sys.argv[1]
	columns_getter = [column.split('=') for column in (item for item in sys.argv[2:] if item.find('->') < 0)]
	columns_setter = [column.split('->') for column in (item for item in sys.argv[2:] if item.find('=') < 0)]

	print(update(table_name, columns_getter, columns_setter))
示例#10
0
def main():
	connection.setup([DB_URL], DB_NAME)
	
	sync_table(SystemTest)
	sync_table(ControlAction)
	sync_table(Position)
	sync_table(SensorData)
	sync_table(ShiftState)
	sync_table(OperationState)
示例#11
0
    def setup(self):
        from cassandra.cqlengine import connection
        if connection.cluster is not None:
            # already connected
            return

        for option, value in self.session_options.items():
            setattr(Session, option, value)
        connection.setup(self.hosts, self.keyspace, **self.connection_options)
示例#12
0
def portfolio_stat():
    # Connect to Cassandra
    connection.setup(cassandra_configs['ips'], cassandra_configs['keyspace'])

    # Get data from Cassandra as serializable dictionaries
    asset_stat = list(map(dict, PortfolioStat.objects.all()))

    # Serve JSON on API endpoint
    return jsonify(asset_stat)
def fill_model(keyspace, brand):
    connection.setup(['127.0.0.1'], keyspace)

    beerometer = BeerometerModel.create(
        brand=brand,
        created_at=datetime.datetime.now()
    )

    return beerometer.to_dict()
示例#14
0
def get_data(request):
    content = request.get_json()
    connection.setup(CASSANDRA_HOSTS, content[ORG])
    obj = eval(
        content[TABLE]).objects.filter(**(content[BODY])).allow_filtering()
    obj_list = []
    for o in obj:
        obj_list.append(dict(o))
    return jsonify(obj_list)
    def setup(self):
        from cassandra.cqlengine import connection
        if connection.cluster is not None:
            # already connected
            return
        connection.setup(self.hosts, self.keyspace, **self.connection_options)

        for option, value in self.session_options.items():
            setattr(self.session, option, value)
 def __init__(self,
              hosts,
              keyspace='gunregistry',
              replication_factor=1,
              **cqlengine_params):
     self.keyspace = keyspace
     connection.setup(hosts, keyspace, **cqlengine_params)
     create_keyspace_simple(keyspace, replication_factor)
     sync_table(Record)
示例#17
0
 def connect(self, host, port, keyspace='events'):
     """ Connect with specific host & port """
     connection.setup([
         host,
     ], "cqlengine", protocol_version=3)
     self._db_connection = Cluster([
         host,
     ])
     self._db_cur = self._db_connection.connect(keyspace=keyspace)
示例#18
0
 def connect(self):
     cluster = Cluster()
     self.session = cluster.connect()
     connection.setup(["127.0.0.1"], "dataanalysis")
     drop_table(User)
     sync_table(User)
     user = User.create(username="******", created_at=datetime.now())
     logging.info("Connected to cluster.")
     print "Sucessfully connected."
示例#19
0
def main():
    connection.setup([DB_URL], DB_NAME)

    sync_table(SystemTest)
    sync_table(ControlAction)
    sync_table(Position)
    sync_table(SensorData)
    sync_table(ShiftState)
    sync_table(OperationState)
示例#20
0
 def reset_db():
     print('Connecting to cluster')
     connection.setup(['127.0.0.1'], 'system', protocol_version=3)
     print('Dropping existing keyspace')
     drop_keyspace(db_keyspace)
     print('Creating keyspace for project')
     create_keyspace_simple(db_keyspace,
                            replication_factor=replication_factor)
     sync_all_tables()
示例#21
0
def create_event_data_table():
  try:
    setup(['127.0.0.1'], 'sparkifydb', retry_connect=True)
    sync_table(model.table.EventDataModel)
    sync_table(model.table.UserAndSessionModel)
    sync_table(model.table.UserAndSongModel)
  except Exception as e:
    print(e)
    exit(1)
示例#22
0
def init_database(cluster, keyspace, db_user, db_pass):
    try:
        auth_provider = PlainTextAuthProvider(username=db_user,
                                              password=db_pass)
        connection.setup(cluster, keyspace, auth_provider=auth_provider)
        sync_table(User)
        sync_table(Tweet)
        sync_table(Relation)
    except Exception:
        raise
示例#23
0
def get_all_data(request):
    content = request.get_json()
    connection.setup(CASSANDRA_HOSTS, content[ORG])
    obj = eval(content[TABLE]).objects.all()
    obj_list = []
    for o in obj:
        obj_list.append(dict(o))
    if content[START_ID]:
        return jsonify(obj_list[int(content[START_ID]):int(content[END_ID])])
    return jsonify(obj_list)
示例#24
0
 def __init__(self, mongo_conf, cass_conf, neo_conf):
     #Create mongo client
     self.mongo_client = MongoClient(mongo_conf['host'], mongo_conf['port'])
     #Setup default connection to cassandra cluster
     connection.setup([cass_conf['host']],
                      cass_conf['default_keyspace'],
                      protocol_version=3)
     #setup connection to neo4j database
     self.graph = None  #Neo client graph
     self.setup_neo(neo_conf)
示例#25
0
    def post(self):

        data = request.get_json(silent=True)

        sender_id = data.get('user_id')
        audience = data.get('audience', Audience.DIRECT)
        receiver_id = data.get('receiver_id', [])
        asset = data.get('asset', {})
        message = data.get('message')

        if not sender_id:
            return make_response("Missing field user_id", 500)

        if not audience:
            response_message = "Must specify the audience. Audience can be: " + Audience.DIRECT + ", " \
                               + Audience.FRIENDS + ", " + Audience.PUBLIC
            return make_response(response_message, 500)

        if audience == Audience.DIRECT:
            receiver_ids = [receiver_id]

        if audience == Audience.FRIENDS:
            receiver_ids = self._get_friends(sender_id)

        if asset and (not asset.get('asset_name')):
            return make_response("Must specify asset.asset_name", 500)

        asset_name = asset.get('asset_name')
        # public not implemented yet

        connection.setup(hosts=CASSANDRA_HOSTS,
                         default_keyspace=MESSAGE_KEYSPACE)

        for receiver_id in receiver_ids:
            MessageByReceiver.create(receiver_id=receiver_id,
                                     sender_id=sender_id,
                                     audience=audience,
                                     message=message,
                                     asset_name=asset_name)
            MessageBySender.create(receiver_id=receiver_id,
                                   sender_id=sender_id,
                                   audience=audience,
                                   message=message,
                                   asset_name=asset_name)

            message_object = {
                "receiver_id": receiver_id,
                "sender_id": sender_id,
                "audience": audience,
                "message": message,
                "asset_name": asset_name
            }
            self._message_sent(message_object)

        return {"success": True}
示例#26
0
    def __init__(self,
                 host=['127.0.0.1'],
                 keyspace='apscheduler',
                 tablename_jobs='apscheduler_jobs',
                 tablename_scheduler='apscheduler_scheduler',
                 pickle_protocol=pickle.HIGHEST_PROTOCOL,
                 **connect_args):
        super(CassandraJobStore, self).__init__()
        self.utc = pytz.utc
        self.keyspace = keyspace
        self.tablename_jobs = tablename_jobs
        self.tablename_scheduler = tablename_scheduler
        self.pickle_protocol = pickle_protocol
        connection.setup(host, keyspace, **connect_args)
        self.conn = connection.session

        class APSchedulerJobs(Model):
            id = columns.UUID(partition_key=True)
            year = columns.Integer(primary_key=True)
            month = columns.Integer(primary_key=True)
            day = columns.Integer(primary_key=True)
            hour = columns.Integer(primary_key=True)
            next_run_time = columns.DateTime()
            job_state = columns.Blob()

        APSchedulerJobs.__keyspace__ = self.keyspace
        APSchedulerJobs.__table_name__ = self.tablename_jobs

        class APSchedulerSchedule(Model):
            year = columns.Integer(partition_key=True)
            month = columns.Integer(partition_key=True)
            day = columns.Integer(partition_key=True)
            hour = columns.Integer(partition_key=True)
            next_run_time = columns.DateTime(primary_key=True)
            id = columns.UUID(primary_key=True)
            job_state = columns.Blob(required=True)

        APSchedulerSchedule.__keyspace__ = self.keyspace
        APSchedulerSchedule.__table_name__ = self.tablename_scheduler

        self.jobs_t = APSchedulerJobs
        self.sched_t = APSchedulerSchedule

        sync_table(self.jobs_t)
        # insert materialized view
        mv = (
            "CREATE MATERIALIZED VIEW IF NOT EXISTS {0}.{1} AS " +
            "SELECT * " + "FROM {2} " +
            "WHERE year IS NOT NULL and month IS NOT NULL and day IS NOT NULL and hour IS NOT NULL and "
            + "next_run_time IS NOT NULL and id IS NOT NULL " +
            "PRIMARY KEY((year, month, day, hour), next_run_time, id);")
        self.conn.execute(
            mv.format(self.keyspace, self.tablename_scheduler,
                      self.tablename_jobs))
示例#27
0
 def connect(self):
     # Close existing clusters and sessions first
     if self.check_active:
         if connection.cluster and not connection.cluster.is_shutdown:
                 connection.cluster.shutdown()
         if connection.session and not connection.session.is_shutdown:
             connection.session.shutdown()
     # Get fresh connection
     connection.setup(self.nodes, self.keyspace)
     connection.cluster.row_factory = dict_factory
     connection.cluster.client_protocol_handler = NumpyProtocolHandler
示例#28
0
def connect_cassandra(role):
    auth_provider = PlainTextAuthProvider(username=str(role),
                                          password=str(role))
    connection.setup(['127.0.0.1'],
                     "sensors",
                     auth_provider=auth_provider,
                     protocol_version=3)
    sync_table(Users)
    sync_table(TemperatureSensor)
    sync_table(PressureSensor)
    return True
示例#29
0
def create_connection(use_gevent: bool = False) -> None:
    """Create a Session object for above Cluster."""
    connection_class = GeventConnection if use_gevent else AsyncoreConnection
    connection.setup(addresses,
                     MEDTAGGER_KEYSPACE,
                     port=port,
                     load_balancing_policy=RoundRobinPolicy(),
                     connection_class=connection_class,
                     connect_timeout=connect_timeout)
    session = connection.get_session()
    session.default_timeout = default_timeout
示例#30
0
    def setup_tables(self):
        key_space = self.env.config.get(ConfigKeys.KEY_SPACE,
                                        domain=ConfigKeys.STORAGE)
        hosts = self.env.config.get(ConfigKeys.HOST, domain=ConfigKeys.STORAGE)
        hosts = hosts.split(",")

        # required to specify execution profiles in future versions
        profiles = {
            # override the default so we can set consistency level later
            EXEC_PROFILE_DEFAULT:
            ExecutionProfile(
                load_balancing_policy=TokenAwarePolicy(
                    DCAwareRoundRobinPolicy()),
                retry_policy=RetryPolicy(),
                request_timeout=10.0,
                row_factory=Session._row_factory,  # noqa
                # should probably be changed to QUORUM when having more than 3 nodes in the cluster
                consistency_level=ConsistencyLevel.LOCAL_ONE,
            ),
            # TODO: there doesn't seem to be a way to specify execution profile when
            #  using the library's object mapping approach, only when writing pure
            #  cql queries:
            #  https://docs.datastax.com/en/developer/python-driver/3.24/execution_profiles/
            # batch profile has longer timeout since they are run async anyway
            "batch":
            ExecutionProfile(
                load_balancing_policy=TokenAwarePolicy(
                    DCAwareRoundRobinPolicy()),
                request_timeout=120.0,
                consistency_level=ConsistencyLevel.LOCAL_ONE,
            )
        }

        kwargs = {
            "default_keyspace": key_space,
            "protocol_version": 3,
            "retry_connect": True,
            "execution_profiles": profiles,
        }

        username = self._get_from_conf(ConfigKeys.USER, ConfigKeys.STORAGE)
        password = self._get_from_conf(ConfigKeys.PASSWORD, ConfigKeys.STORAGE)

        if password is not None:
            auth_provider = PlainTextAuthProvider(
                username=username,
                password=password,
            )
            kwargs["auth_provider"] = auth_provider

        connection.setup(hosts, **kwargs)

        sync_table(MessageModel)
        sync_table(AttachmentModel)
示例#31
0
    def create_cass_keyspace(cls):
        """
        Create Cassandra's testing keyspace.

        CE Cassandra doesn't currently have in-memory DBs, so we can't just use
        the factory like we do in SQL.
        """
        connection.setup(TESTING_IPS, INITIAL_KEYSPACE_NAME)
        # Make sure the keyspace is empty
        cls.teardown_cass_keyspace()
        management.create_keyspace_simple(TEMP_KEYSPACE_NAME, 1)
示例#32
0
    def test_legacy_insert_query(self):
        connection.setup(hosts=[CASSANDRA_IP],
                         default_keyspace=DEFAULT_KEYSPACE,
                         consistency=ConsistencyLevel.LOCAL_ONE)
        self.assertEqual(connection.get_connection().cluster._config_mode,
                         _ConfigMode.LEGACY)

        sync_table(ConnectionModel)
        ConnectionModel.objects.create(key=0, some_data='text0')
        ConnectionModel.objects.create(key=1, some_data='text1')
        self.assertEqual(ConnectionModel.objects(key=0)[0].some_data, 'text0')
示例#33
0
def connect():
    """Connect to Cassandra database."""

    try:
        connection.setup([core.configuration['database']['host']],
                         core.configuration['database']['keyspace'])
        log.info('Connected to Cassandra database!')
    except CQLEngineException:
        raise DatabaseConnectionError(
            'Cannot connect to Cassandra database at {}:{}.'.format(
                core.configuration['database']['host'],
                core.configuration['database']['keyspace']))
def fetch_cluster_cords(cluster):
    """
    This function is needed when one cluster belongs to multiple operators. Returns all cords the acronym belongs to.
    :param cluster: cluster name, acronym
    :return: array of objects with provided cluster
    """
    with open("config.yml", 'r') as yml_file:
        config = yaml.load(yml_file)['database_options']

    connection.setup([config['address']], config['keyspace'])
    cluster_array = ClusterList.objects.filter(acronym=cluster)
    return cluster_array
示例#35
0
def setup_package():
    warnings.simplefilter('always')  # for testing warnings, make sure all are let through
    os.environ[CQLENG_ALLOW_SCHEMA_MANAGEMENT] = '1'

    use_single_node()

    keyspace = 'cqlengine_test'
    connection.setup(['localhost'],
                     protocol_version=PROTOCOL_VERSION,
                     default_keyspace=keyspace)

    create_keyspace_simple(keyspace, 1)
示例#36
0
def _setup_cassandra(hosts, keyspace):
    """Setup connection to cassandra nodes.

    This function needs to be called before making any queries.

    :param hosts `list`: list of hosts to connect to.
    :param keyspace `unicode`: name of the keyspace to connect.
    """
    if not isinstance(hosts, list):
        raise ValueError("hosts only accepts list of ips.")
    connection.setup(hosts=hosts, default_keyspace=keyspace,
                     protocol_version=3)
    def setup(self):
        from cassandra.cqlengine import connection
        if connection.cluster is not None:
            # already connected
            return
        connection.setup(self.hosts, self.keyspace, **self.connection_options)

        consistency = self.connection_options.get('consistency')
        if consistency:
            self.session.default_consistency_level = consistency
        for option, value in self.session_options.items():
            setattr(self.session, option, value)
    def test_cqlengine_can_connect(self):
        class TestModel(Model):
            id = columns.Integer(primary_key=True)
            val = columns.Text()

        connection.setup(None,
                         "test",
                         cloud={'secure_connect_bundle': self.creds})
        create_keyspace_simple('test', 1)
        sync_table(TestModel)
        TestModel.objects.create(id=42, value='test')
        self.assertEqual(len(TestModel.objects.all()), 1)
示例#39
0
def build():
    # setup connection
    connection.setup(protocol_version=3, **settings.DATABASE['config'])
    # create keyspace, because we only have 2 nodes in 1 datacenter
    # then we use simple strategy
    create_keyspace_simple(**settings.DATABASE['keyspace'])

    # sync each model
    # if you alter field, this mechanism
    # wont sync your changes.
    for model in MODELS:
        sync_table(model)
    def setup(self):
        from cassandra.cqlengine import connection
        if connection.cluster is not None:
            # already connected
            return
        connection.setup(self.hosts, self.keyspace, **self.connection_options)

        consistency = self.connection_options.get('consistency')
        if consistency:
            self.session.default_consistency_level = consistency
        for option, value in self.session_options.items():
            setattr(self.session, option, value)
示例#41
0
def process_sample_coverage(job, addresses, keyspace, auth, sample, program, samples):
    connection.setup(addresses, keyspace, auth_provider=auth)

    with open("{}.sambamba_coverage.bed".format(samples[sample]['library_name']), 'rb') as coverage:
        reader = csv.reader(coverage, delimiter='\t')
        header = reader.next()
        threshold_indices = list()
        thresholds = list()
        index = 0
        for element in header:
            if element.startswith("percentage"):
                threshold = element.replace('percentage', '')
                threshold_indices.append(index)
                thresholds.append(int(threshold))
            index += 1

        for row in reader:
            threshold_data = defaultdict(float)
            index = 0
            for threshold in thresholds:
                threshold_data[threshold] = row[threshold_indices[index]]
                index += 1

            sample_data = SampleCoverage.create(sample=samples[sample]['sample_name'],
                                                library_name=samples[sample]['library_name'],
                                                run_id=samples[sample]['run_id'],
                                                num_libraries_in_run=samples[sample]['num_libraries_in_run'],
                                                sequencer_id=samples[sample]['sequencer'],
                                                program_name=program,
                                                extraction=samples[sample]['extraction'],
                                                panel=samples[sample]['panel'],
                                                target_pool=samples[sample]['target_pool'],
                                                amplicon=row[3],
                                                num_reads=row[4],
                                                mean_coverage=row[5],
                                                thresholds=thresholds,
                                                perc_bp_cov_at_thresholds=threshold_data)

            amplicon_data = AmpliconCoverage.create(amplicon=row[3],
                                                    sample=samples[sample]['sample_name'],
                                                    library_name=samples[sample]['library_name'],
                                                    run_id=samples[sample]['run_id'],
                                                    num_libraries_in_run=samples[sample]['num_libraries_in_run'],
                                                    sequencer_id=samples[sample]['sequencer'],
                                                    program_name=program,
                                                    extraction=samples[sample]['extraction'],
                                                    panel=samples[sample]['panel'],
                                                    target_pool=samples[sample]['target_pool'],
                                                    num_reads=row[4],
                                                    mean_coverage=row[5],
                                                    thresholds=thresholds,
                                                    perc_bp_cov_at_thresholds=threshold_data)
def process_sample_coverage(job, addresses, keyspace, auth, sample, program, samples):
    connection.setup(addresses, keyspace, auth_provider=auth)

    with open("{}.sambamba_coverage.bed".format(samples[sample]['library_name']), 'rb') as coverage:
        reader = csv.reader(coverage, delimiter='\t')
        header = reader.next()
        threshold_indices = list()
        thresholds = list()
        index = 0
        for element in header:
            if element.startswith("percentage"):
                threshold = element.replace('percentage', '')
                threshold_indices.append(index)
                thresholds.append(int(threshold))
            index += 1

        for row in reader:
            threshold_data = defaultdict(float)
            index = 0
            for threshold in thresholds:
                threshold_data[threshold] = row[threshold_indices[index]]
                index += 1

            sample_data = SampleCoverage.create(sample=samples[sample]['sample_name'],
                                                library_name=samples[sample]['library_name'],
                                                run_id=samples[sample]['run_id'],
                                                num_libraries_in_run=samples[sample]['num_libraries_in_run'],
                                                sequencer_id=samples[sample]['sequencer'],
                                                program_name=program,
                                                extraction=samples[sample]['extraction'],
                                                panel=samples[sample]['panel'],
                                                target_pool=samples[sample]['target_pool'],
                                                amplicon=row[3],
                                                num_reads=row[4],
                                                mean_coverage=row[5],
                                                thresholds=thresholds,
                                                perc_bp_cov_at_thresholds=threshold_data)

            amplicon_data = AmpliconCoverage.create(amplicon=row[3],
                                                    sample=samples[sample]['sample_name'],
                                                    library_name=samples[sample]['library_name'],
                                                    run_id=samples[sample]['run_id'],
                                                    num_libraries_in_run=samples[sample]['num_libraries_in_run'],
                                                    sequencer_id=samples[sample]['sequencer'],
                                                    program_name=program,
                                                    extraction=samples[sample]['extraction'],
                                                    panel=samples[sample]['panel'],
                                                    target_pool=samples[sample]['target_pool'],
                                                    num_reads=row[4],
                                                    mean_coverage=row[5],
                                                    thresholds=thresholds,
                                                    perc_bp_cov_at_thresholds=threshold_data)
def create_orgspace():
    session = get_session()
    # TODO: Replication strategy should be updated to 3
    query = SimpleStatement(
        "CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'DC1': 1};"
        % ("org", ))
    session.execute(query)
    session.shutdown()
    connection.setup(CASSANDRA_HOSTS, "org", protocol_version=3)
    # session = get_session(orgname)
    sync_table(org)
    connection.unregister_connection('default')
    return
示例#44
0
    def setup(self):
        with self.lock:
            self.session = connection.get_session()
            if not (self.session is None or self.session.is_shutdown):
                # already connected
                return

            for option, value in self.session_options.items():
                setattr(Session, option, value)
            connection.setup(self.hosts, self.keyspace,
                             **self.connection_options)
            self.session = connection.get_session()
            self.cluster = connection.get_cluster()
    def setup(self):
        with self.lock:
            self.session = connection.get_session()
            if not (self.session is None or self.session.is_shutdown):
                # already connected
                return

            for option, value in self.session_options.items():
                setattr(Session, option, value)
            connection.setup(self.hosts, self.keyspace,
                             **self.connection_options)
            self.session = connection.get_session()
            self.cluster = connection.get_cluster()
示例#46
0
    def get(self):
        user_id = get_user_id_from_jwt()
        connection.setup(hosts=CASSANDRA_HOSTS, default_keyspace=CHAT_KEYSPACE)
        chat_rows = ChatByUserId.filter(user_id=user_id)

        chat_list = []
        for chat in chat_rows:
            chat_list.append(chat.to_object())

        return {
            "results": len(chat_list),
            "chats": chat_list
        }
示例#47
0
def main():
    # create a keyspace "test"
    connection.setup(['140.121.101.164'], "weather1")
    management.create_keyspace_simple('weather1', 3)

    # connect to test keyspace
    connection.setup(['140.121.101.164'], "weather1", protocol_version=3)

    management.sync_type('test', Air)
    management.sync_type('test', Sun)
    management.sync_type('test', Rain)
    management.sync_type('test', Basic)
    management.sync_type('test', Value)
    management.sync_table(Weather)
示例#48
0
def setup_connection(set_default_keyspace=True):
    """Set 'cqlengine' connection settings"""
    if connection.session is not None:
        logger.warn("setup_connection(): connection already configured. "
                    "Will overwrite old settings")
    connection.setup(settings.CASSANDRA_CONNECTION['HOSTS'],
                     default_keyspace=settings.CASSANDRA_CONNECTION['KEYSPACE'],
                     **settings.CASSANDRA_CONNECTION['CLUSTER_KWARGS'])
    if set_default_keyspace:
        # Management commands that creates keyspaces requires a way to
        #  create connections when the keyspaces doesn't exists yet
        set_session_default_keyspace()

    logger.info("setup_connection(): cassandra.cqlengine.connection.setup() done")
示例#49
0
文件: utils.py 项目: BjoernT/cruton
def setup():
    """Run the connection setup."""
    cassandra_conf = CONF['data_store']
    cluster_connect = dict(hosts=cassandra_conf['cluster_node'],
                           port=cassandra_conf['port'],
                           default_keyspace=cassandra_conf['keyspace'],
                           executor_threads=6,
                           retry_connect=True,
                           lazy_connect=True)
    auth_provider = _auth_provider(conf=cassandra_conf)
    if auth_provider:
        cluster_connect['auth_provider'] = auth_provider
    connection.setup(**cluster_connect)
    return connection
示例#50
0
文件: app.py 项目: jwyx3/practices
def get_app():
    app = Flask(__name__)

    # routing
    api = Api(app)
    api.add_resource(CreateUrl, '/shorten')
    api.add_resource(GetUrl, '/<url_hash>')

    # create table
    connection.setup(['127.0.0.1'], _NAME, lazy_connect=True,
        retry_connect=True, protocol_version=3)
    sync_table(Url)

    return app
示例#51
0
文件: app.py 项目: jwyx3/practices
def get_app():
    app = Flask(__name__)

    # routing
    api = Api(app)
    api.add_resource(ListenMusic, '/listened')
    api.add_resource(GetTopK, '/topk')

    # create table
    connection.setup(['127.0.0.1'], _NAME, lazy_connect=True,
        retry_connect=True, protocol_version=3)
    sync_table(Music)
    sync_table(ListenLog)
    sync_table(TopkMusic)

    return app
示例#52
0
    def __init__(self, instance_id):
    	self.instance_id = int(instance_id)

        # setup cassadnra connection
        connection.setup(['127.0.0.1'], "estate1", consistency=ConsistencyLevel.ALL)

        #  TODO move this to an indipened reset DB script
        if self.instance_id == 0:
            print "FLUSH DB"
            # flush database by first node (for development)
            management.drop_keyspace("estate1")
            # create needed tables
            management.create_keyspace_simple("estate1", replication_factor=1, durable_writes=True)
            management.sync_table(KeyValueItem)

        print "ES: Initialized estate for instance: %s" % self.instance_id
示例#53
0
    def sync(cls):
        try:
            cassandra_host = connections['default'].settings_dict['HOST'].split(',')
            keyspace = connections['default'].settings_dict['NAME']
            user = connections['default'].settings_dict['USER']
            password = connections['default'].settings_dict['PASSWORD']
            auth_provider = PlainTextAuthProvider(username=user, password=password)

            if cql_cluster is not None:
                cql_cluster.shutdown()
            if cql_session is not None:
                cql_session.shutdown()
            connection.setup(cassandra_host, keyspace, auth_provider=auth_provider)
            sync_table(cls)
        except NoHostAvailable:
            pass
    def configure(self, binder):
        connection.setup(["cassandrahost"], "biketimer", protocol_version=4)
        cluster = Cluster(["cassandrahost"])
        session = cluster.connect("biketimer")

        users_repository_instance = CassandraUsersRepository(cluster, session)
        binder.bind(UsersRepository, to=users_repository_instance)

        spots_repository_instance = CassandraSpotsRepository(cluster, session)
        binder.bind(SpotsRepository, to=spots_repository_instance)

        runs_repository_instance = CassandraRunsRepository(cluster, session)
        binder.bind(RunsRepository, to=runs_repository_instance)

        checkpoint_passes_repository_instance = CassandraCheckpointPassesRepository(cluster, session)
        binder.bind(CheckpointPassesRepository, to=checkpoint_passes_repository_instance)
示例#55
0
    def setup(self, collection):
        """

        :param collection:
        :return:
        """

        connection.setup([CASSANDRA_1], collection)

        management.create_keyspace(
            collection,
            replication_factor=1,
            strategy_class='SimpleStrategy',
        )

        TestModel.__keyspace__ = collection
        management.sync_table(TestModel)
示例#56
0
    def setup(self, force=False, throw=False):
        if self._setup and not force:
            return True

        try:
            connection.setup(self.uri, self.keyspace)
            management.create_keyspace(self.keyspace, replication_factor=1, strategy_class='SimpleStrategy')
            for model in self._models:
                model.__keyspace__ = self.keyspace
                management.sync_table(model)
        except NoHostAvailable:
            logger.error('Could not connect to Cassandra, expect errors.')
            return False

        # Note: return values are for test skipping
        self._setup = True
        return True
示例#57
0
文件: db.py 项目: drount/plays-api
def init_db_blueprint():
    global cluster
    cluster = Cluster(
        db.config['CASSANDRA_CONTACT_POINTS']
    )
    
    global session
    session = cluster.connect(
        db.config['CASSANDRA_KEYSPACE']
    )
    
    connection.setup(
        db.config['CASSANDRA_CONTACT_POINTS'],
        db.config['CASSANDRA_KEYSPACE'], 
        protocol_version=3
    )
    
    initialize_keyspace()
示例#58
0
def cassandra_init(sender=None, signal=None):
    """ Issue reported in ADDNOW-483: Based on suggestions in
        http://stackoverflow.com/questions/24785299/python-cassandra-driver-operationtimeout-on-every-query-in-celery-task.
    """
    try:
        cassandra_host = connections['default'].settings_dict['HOST'].split(',')
        keyspace = connections['default'].settings_dict['NAME']
        user = connections['default'].settings_dict['USER']
        password = connections['default'].settings_dict['PASSWORD']
        auth_provider = PlainTextAuthProvider(username=user, password=password)

        if cql_cluster is not None:
            cql_cluster.shutdown()
        if cql_session is not None:
            cql_session.shutdown()
        connection.setup(cassandra_host, keyspace, auth_provider=auth_provider)
    except NoHostAvailable:
        pass