def done_qubole(query_id): """Sends query_id to Qubole and retrieves the data as pandas DataFrame. :param int query_id: query_id ready in Qubole :return: pandas DataFrame with response data. :rtype: pandas.DataFrame """ with execute_with_handling_errors(config.get_value, 'qubole', 'api_token') as api_token: if api_token is None: return pd.DataFrame([]) Qubole.configure(api_token=api_token) with execute_with_handling_errors(Command().find, id=query_id) as res: if res is None: return pd.DataFrame([]) print("Id: %s, Status: %s" % (str(res.id), res.status)) try: response_buffer = io.BytesIO() res.get_results(response_buffer) return qubole_output_to_df(response_buffer.getvalue()) except Exception as e: print(e) print("Oops! There was a problem. Try again...") return pd.DataFrame([])
def get(query, delete_file=True, filepath='', delimiter=';', query_type='presto', cluster_label=None): with execute_with_handling_errors(config.get_value, 'qubole', 'api_token') as api_token: if api_token is None: return try: Qubole.configure(api_token=api_token) except UnauthorizedAccess: print("Invalid credentials were provided") return if isinstance(query, int): with execute_with_handling_errors(Command().find, id=query) as command: if command is None: return elif query_type == 'presto': with execute_with_handling_errors(PrestoCommand.run, query=query, label=cluster_label) as command: if command is None: return elif query_type == 'hive': with execute_with_handling_errors(HiveCommand.run, query=query, label=cluster_label) as command: if command is None: return else: print('Please verify your input.') return if filepath != '': file = open(filepath, 'w+') else: file = tempfile.NamedTemporaryFile(mode='w+', delete=delete_file) if command.status == 'done': _get_results(command, file, delimiter) file.seek(0) return file else: raise Exception( 'Could not retrieve query results (id: %s, status: %s)' % (command.id, command.status))