本文整理汇总了Python中cassandra.cluster方法的典型用法代码示例。如果您正苦于以下问题:Python cassandra.cluster方法的具体用法?Python cassandra.cluster怎么用?Python cassandra.cluster使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cassandra
的用法示例。
在下文中一共展示了cassandra.cluster方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_ks
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def create_ks(session, name, rf):
query = 'CREATE KEYSPACE %s WITH replication={%s}'
if isinstance(rf, int):
# we assume simpleStrategy
query = query % (name, "'class':'SimpleStrategy', 'replication_factor':%d" % rf)
else:
assert len(rf) >= 0, "At least one datacenter/rf pair is needed"
# we assume networkTopologyStrategy
options = (', ').join(['\'%s\':%d' % (d, r) for d, r in rf.items()])
query = query % (name, "'class':'NetworkTopologyStrategy', %s" % options)
try:
retry_till_success(session.execute, query=query, timeout=120, bypassed_exception=cassandra.OperationTimedOut)
except cassandra.AlreadyExists:
logger.warn('AlreadyExists executing create ks query \'%s\'' % query)
session.cluster.control_connection.wait_for_schema_agreement(wait_time=120)
#Also validates it was indeed created even though we ignored OperationTimedOut
#Might happen some of the time because CircleCI disk IO is unreliable and hangs randomly
session.execute('USE {}'.format(name))
示例2: __init__
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def __init__(self, cluster=Cluster(['127.0.0.1'], connect_timeout=30), keyspace='cstar_perf', email_notifications=False):
"""Instantiate DB model object for interacting with the C* backend.
cluster - Python driver object for accessing Cassandra
keyspace - the keyspace to use
email_notifications - if True, perform email notifications for some actions. Defaults to False.
"""
log.info("Initializing Model...")
self.cluster = cluster if type(cluster) == Cluster else Cluster(cluster)
self.keyspace = keyspace
self.email_notifications = email_notifications
self.__maybe_create_schema()
self.__shared_session = self.get_session()
## Prepare statements:
self.__prepared_statements = {}
for name, stmt in Model.statements.items():
# log.debug("Preparing statement: {stmt}".format(stmt=stmt))
self.__prepared_statements[name] = self.get_session().prepare(stmt)
### ZeroMQ publisher for announcing jobs as they come in:
zmq_context = zmq.Context()
self.zmq_socket = zmq_context.socket(zmq.PUSH)
self.zmq_socket.connect("tcp://127.0.0.1:5556")
log.info("Model initialized")
示例3: stop
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def stop(self):
if self.__stopped:
return
self.__stopped = True
# pytests may appear to hang forever waiting for cluster tear down. are all driver session objects shutdown?
# to debug hang you can add the following at the top of the test
# import faulthandler
# faulthandler.enable()
#
# and then when the hang occurs send a SIGABRT to the pytest process (e.g. kill -SIGABRT <pytest_pid>)
# this will print a python thread dump of all currently alive threads
self.join(timeout=30)
if self.__error is not None:
raise self.__error
示例4: create_cf_simple
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def create_cf_simple(session, name, query):
try:
retry_till_success(session.execute, query=query, timeout=120, bypassed_exception=cassandra.OperationTimedOut)
except cassandra.AlreadyExists:
logger.warn('AlreadyExists executing create cf query \'%s\'' % query)
session.cluster.control_connection.wait_for_schema_agreement(wait_time=120)
#Going to ignore OperationTimedOut from create CF, so need to validate it was indeed created
session.execute('SELECT * FROM %s LIMIT 1' % name)
示例5: execute
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def execute(values):
#randomly set consistency level to quorum
#which will fail on a single node cluster
CL1 = ConsistencyLevel.QUORUM if random.random() < 0.2 else ConsistencyLevel.ONE
CL2 = ConsistencyLevel.QUORUM if random.random() < 0.2 else ConsistencyLevel.ONE
insert_statement_prepared[0].consistency_level = CL1
insert_statement_prepared[1].consistency_level = CL2
# put both writes (cluster 1 and cluster 2) into a list
writes = []
#insert 1st statement into db1 session, table 1
writes.append(db1.execute_async(insert_statement_prepared[0], values))
#insert 2nd statement into db2 session, table 2
writes.append(db2.execute_async(insert_statement_prepared[1], values))
# loop over futures and output success/fail
results = []
for i in range(0,len(writes)):
try:
row = writes[i].result()
results.append(1)
except Exception:
results.append(0)
#log exception if you like
#logging.exception('Failed write: %s', ('Cluster 1' if (i==0) else 'Cluster 2'))
results.append(values)
log(results)
#did we have failures?
if (results[0]==0):
#do something, like re-write to cluster 1
log('Write to cluster 1 failed')
if (results[1]==0):
#do something, like re-write to cluster 2
log('Write to cluster 2 failed')
示例6: connect_cassandra
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def connect_cassandra(is_client_encryption_enable):
connected = False
attempt = 0
session = None
_ssl_context = None
if is_client_encryption_enable:
ssl_context = SSLContext(PROTOCOL_TLSv1)
ssl_context.load_verify_locations(certfile)
ssl_context.verify_mode = CERT_REQUIRED
ssl_context.load_cert_chain(
certfile=usercert,
keyfile=userkey)
_ssl_context = ssl_context
while not connected and attempt < 10:
try:
cluster = Cluster(contact_points=["127.0.0.1"], ssl_context=_ssl_context)
session = cluster.connect()
connected = True
except cassandra.cluster.NoHostAvailable:
attempt += 1
time.sleep(10)
return session
示例7: get_session
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_session(self, shared=True):
try:
if shared:
try:
session = self.__shared_session
except AttributeError:
session = self.cluster.connect(self.keyspace)
else:
session = self.cluster.connect(self.keyspace)
except cassandra.InvalidRequest, e:
# Only attempt to create the schema if we get an error that it
# doesn't exist:
if "Keyspace '{ks}' does not exist".format(ks=self.keyspace) in e.message:
self.__maybe_create_schema()
session = self.cluster.connect(self.keyspace)
示例8: schedule_test
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def schedule_test(self, test_id, test_series, user, cluster, test_definition):
session = self.get_session()
test_definition['test_id'] = str(test_id)
test_json = json.dumps(test_definition)
session.execute(self.__prepared_statements['insert_test'], (test_id, user, cluster, 'scheduled', test_json))
session.execute(self.__prepared_statements['insert_series'], (test_series, test_id))
session.execute(self.__prepared_statements['insert_test_status'], ('scheduled', cluster, test_id, user, test_definition['title']))
self.zmq_socket.send_string("scheduled {cluster} {test_id}".format(**locals()))
return test_id
示例9: update_test_progress_msg
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def update_test_progress_msg(self, test_id, progress_msg):
if not isinstance(test_id, uuid.UUID):
test_id = uuid.UUID(test_id)
session = self.get_session()
test = self.get_test(test_id)
session.execute(self.__prepared_statements['update_test_set_progress_msg'], (progress_msg, test_id))
session.execute(self.__prepared_statements['update_test_status_set_progress_msg'],
(progress_msg, 'in_progress', test['cluster'], test_id))
示例10: update_test_status
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def update_test_status(self, test_id, status):
assert status in TEST_STATES, "{status} is not a valid test state".format(status=status)
session = self.get_session()
if not isinstance(test_id, uuid.UUID):
test_id = uuid.UUID(test_id)
test = self.get_test(test_id)
original_status = self.get_test_status(test_id)
# Update tests table:
if status == "completed":
completed_date = uuid.uuid1()
session.execute(self.__prepared_statements['update_test_set_status_completed'], (status, completed_date, test_id ))
# Add denormalized copy in tests_
session.execute(self.__prepared_statements['insert_test_completed'], ("completed", completed_date, test_id, test['cluster'], test['test_definition']['title'], test['user']))
else:
session.execute(self.__prepared_statements['update_test_set_status'], (status, test_id))
# Remove the old status from test_status:
session.execute(self.__prepared_statements['delete_test_status'], (test['status'], test['cluster'], test_id))
# Add the new status to test_status:
session.execute(self.__prepared_statements['insert_test_status'], (status, test['cluster'], test_id, test['user'], test['test_definition']['title']))
self.zmq_socket.send_string("{status} {cluster} {test_id}".format(status=status, cluster=test['cluster'], test_id=test_id))
log.info("test status is: {status}".format(status=status))
# Send the user an email when the status updates:
if self.email_notifications and status not in ('scheduled','in_progress') and status != original_status:
TestStatusUpdateEmail([test['user']], status=status, name=test['test_definition']['title'],
test_id=test_id).send()
return namedtuple('TestStatus', 'test_id status')(test_id, status)
示例11: get_scheduled_tests
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_scheduled_tests(self, cluster, limit=999999999):
return self.get_test_status_by_cluster('scheduled', cluster, 'ASC', limit)
示例12: get_failed_tests
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_failed_tests(self, cluster, limit=999999999):
return self.get_test_status_by_cluster('failed', cluster, 'DESC', limit)
示例13: get_cancelled_tests
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_cancelled_tests(self, cluster, limit=999999999):
return self.get_test_status_by_cluster('cancelled', cluster, 'DESC', limit)
示例14: get_next_scheduled_test
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_next_scheduled_test(self, cluster):
session = self.get_session()
rows = session.execute(self.__prepared_statements['select_next_scheduled'], (cluster,))
tests = [self.__test_row_to_dict(r) for r in rows]
try:
return tests[0]
except IndexError:
raise NoTestsScheduledError('No tests scheduled for {cluster}'.format(cluster=cluster))
示例15: get_in_progress_tests
# 需要导入模块: import cassandra [as 别名]
# 或者: from cassandra import cluster [as 别名]
def get_in_progress_tests(self, cluster, limit=999999999):
return self.get_test_status_by_cluster('in_progress', cluster, 'ASC', limit) + \
self.get_test_status_by_cluster('cancel_pending', cluster, 'ASC', limit)