Skip to content

Commit a03f2bc

Browse files
authored
Merge pull request #41 from dataiku/task/list-tables-in-public-api
add calls to fetch SQL/Hive schemas/tables [ch34607]
2 parents d6f6e4c + a6cebf1 commit a03f2bc

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

dataikuapi/dss/future.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ def __init__(self, client, job_id, state=None):
1010
self.state = state
1111
self.state_is_peek = True
1212

13+
@classmethod
14+
def get_result_wait_if_needed(cls, client, ret):
15+
if 'jobId' in ret:
16+
future = DSSFuture(client, ret["jobId"], ret)
17+
future.wait_for_result()
18+
return future.get_result()
19+
else:
20+
return ret['result']
21+
1322
def abort(self):
1423
"""
1524
Abort the future

dataikuapi/dss/project.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,53 @@ def init_tables_import(self):
860860
"""
861861
return TablesImportDefinition(self.client, self.project_key)
862862

863+
def list_sql_schemas(self, connection_name):
864+
"""
865+
Lists schemas from which tables can be imported in a SQL connection
866+
867+
:returns: an array of schemas names
868+
"""
869+
return self._list_schemas(connection_name)
870+
871+
def list_hive_databases(self):
872+
"""
873+
Lists Hive databases from which tables can be imported
874+
875+
:returns: an array of databases names
876+
"""
877+
return self._list_schemas("@virtual(hive-jdbc):default")
878+
879+
def _list_schemas(self, connection_name):
880+
return self.client._perform_json("GET", "/projects/%s/datasets/tables-import/actions/list-schemas" % (self.project_key),
881+
params = {"connectionName": connection_name} )
882+
883+
def list_sql_tables(self, connection_name, schema_name=None):
884+
"""
885+
Lists tables to import in a SQL connection
886+
887+
:returns: an array of tables
888+
"""
889+
ret = self.client._perform_json("GET", "/projects/%s/datasets/tables-import/actions/list-tables" % (self.project_key),
890+
params = {"connectionName": connection_name, "schemaName": schema_name} )
891+
892+
def to_schema_table_pair(x):
893+
return {"schema":x.get("schema", None), "table":x["table"]}
894+
return [to_schema_table_pair(x) for x in DSSFuture.get_result_wait_if_needed(self.client, ret)['tables']]
895+
896+
def list_hive_tables(self, hive_database):
897+
"""
898+
Lists tables to import in a Hive database
899+
900+
:returns: an array of tables
901+
"""
902+
connection_name = "@virtual(hive-jdbc):" + hive_database
903+
ret = self.client._perform_json("GET", "/projects/%s/datasets/tables-import/actions/list-tables" % (self.project_key),
904+
params = {"connectionName": connection_name} )
905+
906+
def to_schema_table_pair(x):
907+
return {"schema":x.get("databaseName", None), "table":x["table"]}
908+
return [to_schema_table_pair(x) for x in DSSFuture.get_result_wait_if_needed(self.client, ret)['tables']]
909+
863910
class TablesImportDefinition(object):
864911
"""
865912
Temporary structure holding the list of tables to import

0 commit comments

Comments
 (0)