本文整理汇总了Python中cassandra.cluster.Cluster方法的典型用法代码示例。如果您正苦于以下问题:Python cluster.Cluster方法的具体用法?Python cluster.Cluster怎么用?Python cluster.Cluster使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cassandra.cluster
的用法示例。
在下文中一共展示了cluster.Cluster方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cloud_generator
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def cloud_generator(test_split=0.2):
"""
Generator for cloud use.
test_split: Percentage of data to reserve for testing.
Return X, y (input image and label)
"""
# retrieve raw data
cluster = Cluster(['10.44.27.2'])
session = cluster.connect('healthcare_keyspace_v2')
f_names = _get_image_filenames(test_split=test_split)
while True:
for x in f_names.iloc:
query = "SELECT image_blob, is_label FROM processed_data"
query = query + " WHERE image_filename='" + x[0] + "'" + "ALLOW FILTERING"
rows = session.execute(query)
X, y = _return_input_and_label(rows)
yield X, y
示例2: context
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def context():
global session
if not session:
cluster = Cluster()
session = cluster.connect()
# create the keyspace
ks = 'test_cdm'
if 'test_cdm' not in cluster.metadata.keyspaces:
q = "create KEYSPACE {} WITH \
replication = {{'class': \
'SimpleStrategy', \
'replication_factor': 1}}".format(ks)
session.execute(q)
if not os.path.exists("./cache"):
os.mkdir("./cache")
session.set_keyspace(ks)
return Context(root=os.getcwd(),
dataset="test",
session=session,
cache_dir="./cache",
)
示例3: __init__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster 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")
示例4: test_cassandra_connect
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def test_cassandra_connect(instrument, elasticapm_client, cassandra_cluster):
elasticapm_client.begin_transaction("transaction.test")
sess = cassandra_cluster.connect()
elasticapm_client.end_transaction("test")
transactions = elasticapm_client.events[TRANSACTION]
span = elasticapm_client.spans_for_transaction(transactions[0])[0]
assert span["type"] == "db"
assert span["subtype"] == "cassandra"
assert span["action"] == "connect"
assert span["duration"] > 0
assert span["name"] == "Cluster.connect"
assert span["context"]["destination"] == {
"address": socket.gethostbyname(os.environ.get("CASSANDRA_HOST", "localhost")),
"port": 9042,
"service": {"name": "cassandra", "resource": "cassandra", "type": "db"},
}
示例5: __enter__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def __enter__(self):
domsconfig = ConfigParser.RawConfigParser()
domsconfig.readfp(pkg_resources.resource_stream(__name__, "domsconfig.ini"), filename='domsconfig.ini')
cassHost = domsconfig.get("cassandra", "host")
cassKeyspace = domsconfig.get("cassandra", "keyspace")
cassDatacenter = domsconfig.get("cassandra", "local_datacenter")
cassVersion = int(domsconfig.get("cassandra", "protocol_version"))
dc_policy = DCAwareRoundRobinPolicy(cassDatacenter)
token_policy = TokenAwarePolicy(dc_policy)
self._cluster = Cluster([host for host in cassHost.split(',')], load_balancing_policy=token_policy,
protocol_version=cassVersion)
self._session = self._cluster.connect(cassKeyspace)
return self
示例6: init
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def init(args):
global solr_connection
solr_connection = SolrConnection(args.solr)
global solr_collection
solr_collection = solr_connection[args.collection]
global SOLR_UNIQUE_KEY
SOLR_UNIQUE_KEY = args.solrIdField
dc_policy = RoundRobinPolicy()
token_policy = TokenAwarePolicy(dc_policy)
global cassandra_cluster
cassandra_cluster = Cluster(contact_points=args.cassandra, port=args.cassandraPort,
protocol_version=int(args.cassandraProtocolVersion),
load_balancing_policy=token_policy)
global cassandra_session
cassandra_session = cassandra_cluster.connect(keyspace=args.cassandraKeyspace)
global cassandra_table
cassandra_table = args.cassandraTable
示例7: get_db
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def get_db(args):
log = logging.getLogger("cassandra")
patch_tables(args)
try:
cas_host, cas_port = args.cassandra.split(":")
except ValueError:
cas_host = args.cassandra
cas_port = "9042"
def get_cluster():
return Cluster((cas_host,), port=int(cas_port),
load_balancing_policy=RoundRobinPolicy())
cluster = get_cluster()
log.info("Connecting to %s", args.cassandra)
try:
session = cluster.connect(args.keyspace)
except NoHostAvailable:
log.warning("Keyspace %s does not exist", args.keyspace)
cluster = get_cluster()
session = cluster.connect()
return session
示例8: __init__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def __init__(self):
self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')
self.prep_stmt = {}
template_for_prediction = 'INSERT INTO ga_chp_bq_predictions (client_id,prediction) VALUES (?,?)'
template_for_predictions_by_date = 'INSERT INTO ga_chp_bq_predictions_by_prediction_date (prediction_date, client_id, prediction) VALUES (?,?,?)'
template_for_predictions_statistics = 'UPDATE ga_chp_bq_predictions_statistics SET loyal=loyal+?, neutral=neutral+?, churning=churning+?, lost=lost+? WHERE prediction_date=?'
self.CASS_REQ_TIMEOUT = 3600.0
self.auth_provider = PlainTextAuthProvider(
username=self.MORPHL_CASSANDRA_USERNAME,
password=self.MORPHL_CASSANDRA_PASSWORD)
self.cluster = Cluster(
[self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)
self.prep_stmt['prediction'] = self.session.prepare(template_for_prediction)
self.prep_stmt['predictions_by_date'] = self.session.prepare(template_for_predictions_by_date)
self.prep_stmt['predictions_statistics'] = self.session.prepare(template_for_predictions_statistics)
示例9: __init__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def __init__(self):
self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')
self.QUERY = 'SELECT * FROM ga_chp_bq_predictions WHERE client_id = ? LIMIT 1'
self.CASS_REQ_TIMEOUT = 3600.0
self.auth_provider = PlainTextAuthProvider(
username=self.MORPHL_CASSANDRA_USERNAME,
password=self.MORPHL_CASSANDRA_PASSWORD)
self.cluster = Cluster(
[self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)
self.session.row_factory = dict_factory
self.session.default_fetch_size = 100
self.prepare_statements()
示例10: __init__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def __init__(self):
self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')
self.CASS_REQ_TIMEOUT = 3600.0
self.auth_provider = PlainTextAuthProvider(
username=self.MORPHL_CASSANDRA_USERNAME,
password=self.MORPHL_CASSANDRA_PASSWORD)
self.cluster = Cluster(
[self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)
self.session.row_factory = dict_factory
self.session.default_fetch_size = 100
self.prepare_statements()
示例11: get_session
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def get_session(cls, seeds, **kwargs):
_ = kwargs
if cls.__session is None:
# Allow dependency injection
session = kwargs.get("session")
if session is None:
cluster = c_cluster.Cluster(seeds)
session = cluster.connect()
keyspace = kwargs["keyspace"]
replication = kwargs["replication"]
if kwargs.get("drop_keyspace", False):
session.execute(cls.QUERY_DROP_KEYSPACE.format(keyspace))
session.execute(cls.QUERY_CREATE_KEYSPACE.format(
keyspace=keyspace,
replication=str(replication),
))
session.set_keyspace(keyspace)
cls.__session = session
return cls.__session
示例12: init
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def init(self):
from cassandra.cluster import Cluster
from dino.storage.cassandra_driver import Driver
cluster = Cluster(self.hosts)
self.driver = Driver(cluster.connect(), self.key_space, self.strategy, self.replications)
self.driver.init()
示例13: __init__
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def __init__(self, config, profile='dev', hosts=['127.0.0.1'], port=9042,
user=None, password=None, host_cert_path=None,
client_key_path=None, client_cert_path=None):
self.config = config
try:
self.current_profile = self.config.profiles[profile]
except KeyError:
raise ValueError("Invalid profile name '{}'".format(profile))
if user:
auth_provider = PlainTextAuthProvider(user, password)
else:
auth_provider = None
if host_cert_path:
ssl_options = self._build_ssl_options(
host_cert_path,
client_key_path,
client_cert_path)
else:
ssl_options = None
self.cluster = Cluster(
contact_points=hosts,
port=port,
auth_provider=auth_provider,
max_schema_agreement_wait=300,
control_connection_timeout=10,
connect_timeout=30,
ssl_options=ssl_options)
self._session = None
示例14: _check_cluster
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def _check_cluster(self):
"""Check if the cluster is still alive, raise otherwise"""
if not self.cluster:
raise RuntimeError("Cluster has shut down")
示例15: _create_session
# 需要导入模块: from cassandra import cluster [as 别名]
# 或者: from cassandra.cluster import Cluster [as 别名]
def _create_session(self, node, keyspace, user, password, compression, protocol_version,
port=None, ssl_opts=None, execution_profiles=None, **kwargs):
node_ip = get_ip_from_node(node)
if not port:
port = get_port_from_node(node)
if protocol_version is None:
protocol_version = get_eager_protocol_version(node.cluster.version())
if user is not None:
auth_provider = get_auth_provider(user=user, password=password)
else:
auth_provider = None
profiles = {EXEC_PROFILE_DEFAULT: make_execution_profile(**kwargs)
} if not execution_profiles else execution_profiles
cluster = PyCluster([node_ip],
auth_provider=auth_provider,
compression=compression,
protocol_version=protocol_version,
port=port,
ssl_options=ssl_opts,
connect_timeout=15,
allow_beta_protocol_version=True,
execution_profiles=profiles)
session = cluster.connect(wait_for_all_pools=True)
if keyspace is not None:
session.set_keyspace(keyspace)
self.connections.append(session)
return session