Created
August 15, 2016 08:15
-
-
Save yershalom/2bb47b8a2fad03fed0e605c2482b1c2b to your computer and use it in GitHub Desktop.
Cassandra DS for redash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import logging | |
from redash.query_runner import * | |
logger = logging.getLogger(__name__) | |
class Cassandra(BaseQueryRunner): | |
@classmethod | |
def configuration_schema(cls): | |
return { | |
'type': 'object', | |
'properties': { | |
'host': { | |
'type': 'string', | |
}, | |
'port': { | |
'type': 'number', | |
'default': 9042, | |
}, | |
'Keyspace': { | |
'type': 'string', | |
'title': 'Keyspace name' | |
} | |
}, | |
'required': ['Keyspace'] | |
} | |
@classmethod | |
def name(cls): | |
return "cassandra" | |
@classmethod | |
def enabled(cls): | |
try: | |
from cassandra.cluster import Cluster | |
except ImportError: | |
return False | |
return True | |
def _get_tables(self, schema): | |
query = """ | |
select columnfamily_name from system.schema_columnfamilies where keyspace_name = '{}'; | |
""".format(self.configuration['Keyspace']) | |
results, error = self.run_query(query) | |
return results | |
def run_query(self, query): | |
from cassandra.cluster import Cluster | |
connection = None | |
try: | |
connection = Cluster([self.configuration.get('host', '')]) | |
session = connection.connect() | |
logger.debug("Cassandra running query: %s", query) | |
row = session.execute(query) | |
CF = [] | |
count = 0 | |
for _ in row: | |
CF.append(row[count[0]]) | |
couny += 1 | |
error = "No data was returned." | |
session.close() | |
except cassandra.cluster.Error, e: | |
error = e.args[1] | |
except KeyboardInterrupt: | |
error = "Query cancelled by user." | |
except Exception as e: | |
raise sys.exc_info()[1], None, sys.exc_info()[2] | |
finally: | |
if connection: | |
connection.close() | |
return CF | |
register(Cassandra) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment