-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: query runner for Cassandra and ScyllaDB #1236
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eee2e7c
Added new DS for Cassandra and ScyllaDB
87d77d4
Added cassandra-driver to requirements_all_ds.txt file
3787319
Fixed some syntax
bd5039a
Fixed little syntax error
986dc68
Removed unnessecery exception throw
37271c7
Switched to fetch_columns instead of messy code
b308e02
Removed cursor.close() from hive_ds because its not needed
7cce9d5
Added Auth importer for cassandra
d41b84e
Fixed syntax error at _get_tables
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import json | ||
import sys | ||
import logging | ||
|
||
from redash.query_runner import * | ||
from redash.utils import JSONEncoder | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
from cassandra.cluster import Cluster | ||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
class Cassandra(BaseQueryRunner): | ||
@classmethod | ||
def enabled(cls): | ||
return enabled | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
'type': 'object', | ||
'properties': { | ||
'host': { | ||
'type': 'string', | ||
}, | ||
'port': { | ||
'type': 'number', | ||
'default': 9042, | ||
}, | ||
'keyspace': { | ||
'type': 'string', | ||
'title': 'Keyspace name' | ||
}, | ||
'username': { | ||
'type': 'string', | ||
'title': 'Username' | ||
}, | ||
'password': { | ||
'type': 'string', | ||
'title': 'Password' | ||
} | ||
}, | ||
'required': ['keyspace', 'host'] | ||
} | ||
|
||
@classmethod | ||
def type(cls): | ||
return "Cassandra" | ||
|
||
def _get_tables(self, schema): | ||
query = """ | ||
select columnfamily_name from system.schema_columnfamilies where keyspace_name = '{}'; | ||
""".format(self.configuration['keyspace']) | ||
|
||
results = self.run_query(query) | ||
return results, error | ||
|
||
def run_query(self, query): | ||
from cassandra.cluster import Cluster | ||
connection = None | ||
try: | ||
if self.configuration.get('username', '') and self.configuration.get('password', ''): | ||
from cassandra.auth import PlainTextAuthProvider | ||
auth_provider = PlainTextAuthProvider(username='{}'.format(self.configuration.get('username', '')), | ||
password='{}'.format(self.configuration.get('password', ''))) | ||
connection = Cluster([self.configuration.get('host', '')], auth_provider=auth_provider) | ||
else: | ||
connection = Cluster([self.configuration.get('host', '')]) | ||
|
||
session = connection.connect() | ||
logger.debug("Cassandra running query: %s", query) | ||
result = session.execute(query) | ||
|
||
column_names = result.column_names | ||
|
||
columns = self.fetch_columns(map(lambda c: (c, 'string'), column_names)) | ||
|
||
rows = [dict(zip(column_names, row)) for row in result] | ||
|
||
data = {'columns': columns, 'rows': rows} | ||
json_data = json.dumps(data, cls=JSONEncoder) | ||
|
||
error = None | ||
|
||
except cassandra.cluster.Error, e: | ||
error = e.args[1] | ||
except KeyboardInterrupt: | ||
error = "Query cancelled by user." | ||
|
||
return json_data, error | ||
|
||
class ScyllaDB(Cassandra): | ||
|
||
def __init__(self, configuration): | ||
super(ScyllaDB, self).__init__(configuration) | ||
|
||
@classmethod | ||
def type(cls): | ||
return "scylla" | ||
|
||
register(Cassandra) | ||
register(ScyllaDB) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ botocore==1.4.4 | |
sasl>=0.1.3 | ||
thrift>=0.8.0 | ||
thrift_sasl>=0.1.0 | ||
cassandra-driver==3.1.1 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it pure Python or has some C/system package dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcc python-dev
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍