def fetch_single_entity(self, dsl_query): ''' REQUEST: hue:8889/metadata/api/navigator/find_entity?type=database&name=default SAMPLE response for Navigator find_entity response {"status": 0, "entity": { "customProperties": null, "deleteTime": null, "fileSystemPath": "hdfs://nightly6x-1.vpc.cloudera.com:8020/user/hive/warehouse", "description": null, "params": null, "type": "DATABASE", "internalType": "hv_database", "sourceType": "HIVE", "tags": [], "deleted": false, "technicalProperties": null, "userEntity": false, "originalDescription": "Default Hive database", "metaClassName": "hv_database", "properties": {"__cloudera_internal__hueLink": "https://nightly6x-1.vpc.cloudera.com:8889/hue/metastore/tables/default"}, "identity": "23", "firstClassParentId": null, "name": null, "extractorRunId": "7##1", "sourceId": "7", "packageName": "nav", "parentPath": null, "originalName": "default"}} ''' response = {"status": 0, "entity": []} try: if CATALOG.ENABLE_BASIC_SEARCH.get(): atlas_response = self._root.get('/v2/search/basic?query=%s' % dsl_query, headers=self.__headers, params=self.__params) else: atlas_response = self._root.get('/v2/search/dsl?query=%s' % dsl_query, headers=self.__headers, params=self.__params) if not 'entities' in atlas_response or len( atlas_response['entities']) < 1: raise CatalogEntityDoesNotExistException( 'Could not find entity with query: %s' % dsl_query) for atlas_entity in atlas_response['entities']: response['entity'].append( self.adapt_atlas_entity_to_navigator(atlas_entity)) return response['entity'][0] except RestException as e: if e.code == 401: raise raise_popup_exception( 'Hue could not authenticate to Atlas', detail=e) else: raise raise_popup_exception('Hue could not query Atlas', detail=e)
def find_entity(self, source_type, type, name, **filters): """ GET /api/v3/entities?query=((sourceType:<source_type>)AND(type:<type>)AND(originalName:<name>)) http://cloudera.github.io/navigator/apidocs/v3/path__v3_entities.html """ try: params = self.__params query_filters = { 'sourceType': source_type, 'originalName': name, 'deleted': 'false' } for key, value in list(filters.items()): query_filters[key] = value filter_query = 'AND'.join( '(%s:%s)' % (key, value) for key, value in list(query_filters.items())) filter_query = '%(type)s AND %(filter_query)s' % { 'type': '(type:%s)' % 'TABLE OR type:VIEW' if type == 'TABLE' else type, # Impala does not always say that a table is actually a view 'filter_query': filter_query } source_ids = get_cluster_source_ids(self) if source_ids: filter_query = source_ids + '(' + filter_query + ')' params += ( ('query', filter_query), ('offset', 0), ( 'limit', 2 ), # We are looking for single entity, so limit to 2 to check for multiple results ) response = self._root.get('entities', headers=self.__headers, params=params) if not response: raise CatalogEntityDoesNotExistException( 'Could not find entity with query filters: %s' % str(query_filters)) elif len(response) > 1: raise CatalogApiException( 'Found more than 1 entity with query filters: %s' % str(query_filters)) return response[0] except RestException as e: msg = 'Failed to find entity: %s' % str(e) LOG.error(msg) raise CatalogApiException(e.message)