-
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
[Cassandra] Add: schema browser support & explicit protocol version #1482
Changes from 1 commit
420973c
045c921
f54e7b3
ca4db61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import json | ||
import logging | ||
|
||
from redash.query_runner import BaseQueryRunner, register | ||
from redash.query_runner import BaseSQLQueryRunner, register | ||
from redash.utils import JSONEncoder | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -14,7 +14,7 @@ | |
enabled = False | ||
|
||
|
||
class Cassandra(BaseQueryRunner): | ||
class Cassandra(BaseSQLQueryRunner): | ||
noop_query = "SELECT * FROM system.compactions_in_progress" | ||
|
||
@classmethod | ||
|
@@ -54,22 +54,36 @@ def type(cls): | |
return "Cassandra" | ||
|
||
def _get_tables(self, schema): | ||
query = """ | ||
CF_query = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
select columnfamily_name from system.schema_columnfamilies where keyspace_name = '{}'; | ||
""".format(self.configuration['keyspace']) | ||
|
||
results, error = self.run_query(query, None) | ||
return results, error | ||
results, error = self.run_query(CF_query, None) | ||
results = json.loads(results) | ||
for row in results['rows']: | ||
table_name = row['columnfamily_name'] | ||
if table_name not in schema: | ||
schema[table_name] = {'name': table_name, 'columns': []} | ||
CN_query = """ | ||
SELECT column_name FROM system.schema_columns where keyspace_name ='{}' and columnfamily_name ='{}'; | ||
""".format(self.configuration['keyspace'], table_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not run: |
||
columns, error = self.run_query(CN_query, None) | ||
columns = json.loads(columns) | ||
for col in columns['rows']: | ||
column_name = col['column_name'] | ||
schema[table_name]['columns'].append(col['column_name']) | ||
|
||
return schema.values() | ||
|
||
def run_query(self, query, user): | ||
connection = None | ||
try: | ||
if self.configuration.get('username', '') and self.configuration.get('password', ''): | ||
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) | ||
connection = Cluster([self.configuration.get('host', '')], auth_provider=auth_provider, protocol_version=3) | ||
else: | ||
connection = Cluster([self.configuration.get('host', '')]) | ||
connection = Cluster([self.configuration.get('host', '')], protocol_version=3) | ||
|
||
session = connection.connect() | ||
session.set_keyspace(self.configuration['keyspace']) | ||
|
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.
Why?
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.
Because when I use BaseQueryRunner i'm getting empty list
[]
for get_tablesThere 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.
Correct, because you need to implement
get_schema
instead (get_tables
is a method BaseSQLQueryRunner uses forget_schema
).