def test_custom_raw_row_results_all_types(self): """ Test to validate that custom protocol handlers work with varying types of results Connect, create a table with all sorts of data. Query the data, make the sure the custom results handler is used correctly. @since 2.7 @jira_ticket PYTHON-313 @expected_result custom protocol handler is invoked with various result types @test_category data_types:serialization """ # Connect using a custom protocol handler that tracks the various types the result message is used with. session = Cluster(protocol_version=PROTOCOL_VERSION).connect(keyspace="custserdes") session.client_protocol_handler = CustomProtocolHandlerResultMessageTracked session.row_factory = tuple_factory colnames = create_table_with_all_types("alltypes", session, 1) columns_string = ", ".join(colnames) # verify data params = get_all_primitive_params(0) results = session.execute("SELECT {0} FROM alltypes WHERE primkey=0".format(columns_string))[0] for expected, actual in zip(params, results): self.assertEqual(actual, expected) # Ensure we have covered the various primitive types self.assertEqual(len(CustomResultMessageTracked.checked_rev_row_set), len(PRIMITIVE_DATATYPES)-1) session.shutdown()
def initialize_connection(self): session = Cluster(contact_points=[self.host], port=self.port).connect(keyspace=self.keyspace) session.row_factory = panda_factory query = "SELECT epoch_time, post_count, byte_transfer, get_count, requests, visits FROM "+self.source DataFrame = session.execute(query).sort(columns=['epoch_time', 'post_count', 'byte_transfer', 'get_count', 'requests', 'visits'], ascending=[1,0,0,0,0,0]) process = retrieve_insert(DataFrame, session, self.dest) # create instance for the class retrieve_insert process.retrieve_variables() return 1
def test_custom_raw_uuid_row_results(self): """ Test to validate that custom protocol handlers work with raw row results Connect and validate that the normal protocol handler is used. Re-Connect and validate that the custom protocol handler is used. Re-Connect and validate that the normal protocol handler is used. @since 2.7 @jira_ticket PYTHON-313 @expected_result custom protocol handler is invoked appropriately. @test_category data_types:serialization """ # Ensure that we get normal uuid back first session = Cluster(protocol_version=PROTOCOL_VERSION).connect( keyspace="custserdes") session.row_factory = tuple_factory result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() uuid_type = result[0] self.assertEqual(type(uuid_type), uuid.UUID) # use our custom protocol handlder session.client_protocol_handler = CustomTestRawRowType session.row_factory = tuple_factory result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() raw_value = result.pop() self.assertTrue(isinstance(raw_value, binary_type)) self.assertEqual(len(raw_value), 16) # Ensure that we get normal uuid back when we re-connect session.client_protocol_handler = ProtocolHandler result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() uuid_type = result[0] self.assertEqual(type(uuid_type), uuid.UUID) session.shutdown()
def test_custom_raw_uuid_row_results(self): """ Test to validate that custom protocol handlers work with raw row results Connect and validate that the normal protocol handler is used. Re-Connect and validate that the custom protocol handler is used. Re-Connect and validate that the normal protocol handler is used. @since 2.7 @jira_ticket PYTHON-313 @expected_result custom protocol handler is invoked appropriately. @test_category data_types:serialization """ # Ensure that we get normal uuid back first session = Cluster().connect() session.row_factory = tuple_factory result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() uuid_type = result[0] self.assertEqual(type(uuid_type), uuid.UUID) # use our custom protocol handlder session.client_protocol_handler = CustomTestRawRowType session.row_factory = tuple_factory result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() raw_value = result.pop() self.assertTrue(isinstance(raw_value, binary_type)) self.assertEqual(len(raw_value), 16) # Ensure that we get normal uuid back when we re-connect session.client_protocol_handler = ProtocolHandler result_set = session.execute("SELECT schema_version FROM system.local") result = result_set.pop() uuid_type = result[0] self.assertEqual(type(uuid_type), uuid.UUID) session.shutdown()
def create_connection(self): """ Override the create_connection from the DbConnectionWrapper class which get's called in it's initializer """ from cassandra.cluster import Cluster from cassandra.query import dict_factory session = Cluster(self.nodes).connect() # Don't return paged results session.default_fetch_size = self.default_fetch_size # Return in dictionary format for easy parsing to DataFrame session.row_factory = dict_factory return session
from cassandra.query import dict_factory from cassandra.cluster import Cluster import cassandra_client as client KEYSPACE = "user_ratings" TABLE = "ratings" SESSION = Cluster(['127.0.0.1'], port=9042).connect() client.create_keyspace(SESSION, KEYSPACE) client.create_table(SESSION, KEYSPACE, TABLE) SESSION.set_keyspace(KEYSPACE) SESSION.row_factory = dict_factory RATING_QUERY = SESSION.prepare(f"SELECT * FROM {TABLE} WHERE user_id=?") DELETE_RATING_QUERY = SESSION.prepare(f"DELETE FROM {TABLE} WHERE user_id=?") def get(user_id: str) -> dict: user = SESSION.execute(RATING_QUERY, [user_id]) if user: return user[0] return {} def push(rating: dict): client.push_table(SESSION, KEYSPACE, TABLE, rating) def list() -> list: return client.list_table(SESSION, KEYSPACE, TABLE)
""" web app base singleton utils 1. session cassandra driver crud support """ from cassandra.cluster import Cluster from cassandra.decoder import dict_factory from tornado.options import options __author__ = 'wanggen' session = Cluster(contact_points=[options.cassandra_host]).connect(options.cassandra_keyspace) session.row_factory = dict_factory
""" web app base singleton utils 1. session cassandra driver crud support """ from cassandra.cluster import Cluster from cassandra.decoder import dict_factory from tornado.options import options __author__ = 'wanggen' session = Cluster(contact_points=[options.cassandra_host]).connect( options.cassandra_keyspace) session.row_factory = dict_factory
SELECT id, group, cycle, double_sum(metric) AS energy FROM battery_metrics.discharge_energy WHERE group=\'{}\' AND cycle={}; """.format(group_name, cycle_number)) # Calculates deep dive metrics (mean, std dev, and percent deviation) mean = df["energy"].mean() stdev = df["energy"].std() df["percent deviation"] = (df["energy"] - mean) * 100.0 / (2.0 * stdev) df.sort_values(by="percent deviation", ascending=False) for n in ("energy", "percent deviation"): df[n] = df[n].map(lambda x: round(x, 1)) return df.to_dict("records") ## MAIN MODULE if __name__ == "__main__": # Sets formatting for retrieved database query db_session.row_factory = create_dataframe db_session.default_fetch_size = None # Starts Flask/Dash app app.run_server(debug=False, host="0.0.0.0", port=80) ## END OF FILE