Look up an entity (Basic)
Stay organized with collections
Save and categorize content based on your preferences.
Look up an entity by MID in the Knowledge Graph using Basic Edition.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides a Python code sample for looking up entities by MID (Machine ID) in the Google Knowledge Graph using the Basic Edition.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code demonstrates how to use the \u003ccode\u003eEnterpriseKnowledgeGraphServiceClient\u003c/code\u003e to perform a \u003ccode\u003elookup_public_kg\u003c/code\u003e request.\u003c/p\u003e\n"],["\u003cp\u003eUsers must set up Application Default Credentials (ADC) for authentication with the Enterprise Knowledge Graph service.\u003c/p\u003e\n"],["\u003cp\u003eThe response provides information including the name, description, types, detailed description, Cloud MID, and other identifiers for each looked up entity.\u003c/p\u003e\n"],["\u003cp\u003eFor further exploration of additional samples using other Google cloud products, the Google Cloud sample browser is a resource.\u003c/p\u003e\n"]]],[],null,["# Look up an entity (Basic)\n\nLook up an entity by MID in the Knowledge Graph using Basic Edition.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Google Knowledge Graph Search API](/enterprise-knowledge-graph/docs/search-api)\n\nCode sample\n-----------\n\n### Python\n\n\nFor more information, see the\n[Enterprise Knowledge Graph Python API\nreference documentation](/python/docs/reference/enterpriseknowledgegraph/latest).\n\n\nTo authenticate to Enterprise Knowledge Graph, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n from __future__ import annotations\n\n from collections.abc import Sequence\n\n from google.cloud import enterpriseknowledgegraph as ekg\n\n # TODO(developer): Uncomment these variables before running the sample.\n # project_id = 'YOUR_PROJECT_ID'\n # location = 'YOUR_GRAPH_LOCATION' # Values: 'global'\n # ids = ['YOUR_LOOKUP_MID'] # https://cloud.google.com/enterprise-knowledge-graph/docs/mid\n # languages = ['en'] # Optional: List of ISO 639-1 Codes\n\n\n def lookup_public_kg_sample(\n project_id: str,\n location: str,\n ids: Sequence[str],\n languages: Sequence[str] = None,\n ):\n # Create a client\n client = ekg.EnterpriseKnowledgeGraphServiceClient()\n\n # The full resource name of the location\n # e.g. projects/{project_id}/locations/{location}\n parent = client.common_location_path(project=project_id, location=location)\n\n # Initialize request argument(s)\n request = ekg.LookupPublicKgRequest(\n parent=parent,\n ids=ids,\n languages=languages,\n )\n\n # Make the request\n response = client.lookup_public_kg(request=request)\n\n print(f\"Lookup IDs: {ids}\\n\")\n\n print(response)\n\n # Extract and print date from response\n for item in response.item_list_element:\n result = item.get(\"result\")\n\n print(f\"Name: {result.get('name')}\")\n print(f\"- Description: {result.get('description')}\")\n print(f\"- Types: {result.get('@type')}\\n\")\n\n detailed_description = result.get(\"detailedDescription\")\n\n if detailed_description:\n print(\"- Detailed Description:\")\n print(f\"\\t- Article Body: {detailed_description.get('articleBody')}\")\n print(f\"\\t- URL: {detailed_description.get('url')}\")\n print(f\"\\t- License: {detailed_description.get('license')}\\n\")\n\n print(f\"- Cloud MID: {result.get('@id')}\")\n for identifier in result.get(\"identifier\"):\n print(f\"\\t- {identifier.get('name')}: {identifier.get('value')}\")\n\n print(\"\\n\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=enterpriseknowledgegraph)."]]