Criar entradas personalizadas do Data Catalog para suas fontes de dados

Para integrar suas fontes de dados personalizadas, chame APIs do Data Catalog que permitam criar e gerenciar entradas do Data Catalog com tipos de recursos de dados personalizados. Neste documento, uma entrada para um tipo de recurso de dados personalizado é chamada de "entrada personalizada".

Criar grupos de entradas e entradas personalizadas

As entradas personalizadas devem ser inseridas em um grupo de entrada criado pelo usuário. Você cria o grupo de entradas e cria a entrada personalizada no grupo de entrada. Para mais informações, consulte Entradas e grupos de entradas.

Depois de criar uma entrada, você pode definir políticas do IAM no grupo de entrada para definir quem tem acesso ao grupo de entrada e às entradas nele.

Java

Antes de testar esta amostra, siga as instruções de configuração de Java no Guia de início rápido do Data Catalog: como usar bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API Data Catalog Java.

Para autenticar no Data Catalog, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

import com.google.cloud.datacatalog.v1.ColumnSchema;
import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest;
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.Entry;
import com.google.cloud.datacatalog.v1.EntryGroup;
import com.google.cloud.datacatalog.v1.LocationName;
import com.google.cloud.datacatalog.v1.Schema;
import java.io.IOException;

// Sample to create custom entry
public class CreateCustomEntry {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project";
    String entryGroupId = "onprem_entry_group";
    String entryId = "onprem_entry_id";
    createCustomEntry(projectId, entryGroupId, entryId);
  }

  public static void createCustomEntry(String projectId, String entryGroupId, String entryId)
      throws IOException {
    // Currently, Data Catalog stores metadata in the us-central1 region.
    String location = "us-central1";

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
      // Construct the EntryGroup for the EntryGroup request.
      EntryGroup entryGroup =
          EntryGroup.newBuilder()
              .setDisplayName("My awesome Entry Group")
              .setDescription("This Entry Group represents an external system")
              .build();

      // Construct the EntryGroup request to be sent by the client.
      CreateEntryGroupRequest entryGroupRequest =
          CreateEntryGroupRequest.newBuilder()
              .setParent(LocationName.of(projectId, location).toString())
              .setEntryGroupId(entryGroupId)
              .setEntryGroup(entryGroup)
              .build();

      // Use the client to send the API request.
      EntryGroup createdEntryGroup = dataCatalogClient.createEntryGroup(entryGroupRequest);

      // Construct the Entry for the Entry request.
      Entry entry =
          Entry.newBuilder()
              .setUserSpecifiedSystem("onprem_data_system")
              .setUserSpecifiedType("onprem_data_asset")
              .setDisplayName("My awesome data asset")
              .setDescription("This data asset is managed by an external system.")
              .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset")
              .setSchema(
                  Schema.newBuilder()
                      .addColumns(
                          ColumnSchema.newBuilder()
                              .setColumn("first_column")
                              .setDescription("This columns consists of ....")
                              .setMode("NULLABLE")
                              .setType("DOUBLE")
                              .build())
                      .addColumns(
                          ColumnSchema.newBuilder()
                              .setColumn("second_column")
                              .setDescription("This columns consists of ....")
                              .setMode("REQUIRED")
                              .setType("STRING")
                              .build())
                      .build())
              .build();

      // Construct the Entry request to be sent by the client.
      CreateEntryRequest entryRequest =
          CreateEntryRequest.newBuilder()
              .setParent(createdEntryGroup.getName())
              .setEntryId(entryId)
              .setEntry(entry)
              .build();

      // Use the client to send the API request.
      Entry createdEntry = dataCatalogClient.createEntry(entryRequest);
      System.out.printf("Custom entry created with name: %s", createdEntry.getName());
    }
  }
}

Node.js

Antes de testar esta amostra, siga as instruções de configuração de Node.js no Guia de início rápido do Data Catalog: como usar bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API Data Catalog Node.js.

Para autenticar no Data Catalog, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

// Import the Google Cloud client library.
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1;
const datacatalog = new DataCatalogClient();

async function createCustomEntry() {
  // Create a custom entry within an entry group.

  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const projectId = 'my_project';
  // const entryGroupId = 'my_entry_group';
  // const entryId =  'my_entry';
  // const tagTemplateId = 'my_tag_template';

  // Currently, Data Catalog stores metadata in the us-central1 region.
  const location = 'us-central1';

  // Delete any pre-existing Entry with the same name
  // that will be used to create the new Entry.
  try {
    const entryName = datacatalog.entryPath(
      projectId,
      location,
      entryGroupId,
      entryId
    );
    await datacatalog.deleteEntry({name: entryName});
    console.log(`Deleted Entry: ${entryName}`);
  } catch (err) {
    console.log('Entry does not exist.');
  }

  // Delete any pre-existing Entry Group with the same name
  // that will be used to construct the new EntryGroup.
  try {
    const entryGroupName = datacatalog.entryGroupPath(
      projectId,
      location,
      entryGroupId
    );
    await datacatalog.deleteEntryGroup({name: entryGroupName});
    console.log(`Deleted Entry Group: ${entryGroupName}`);
  } catch (err) {
    console.log('Entry Group does not exist.');
  }

  // Delete any pre-existing Template with the same name
  // that will be used to create a new Template.
  const tagTemplateName = datacatalog.tagTemplatePath(
    projectId,
    location,
    tagTemplateId
  );

  let tagTemplateRequest = {
    name: tagTemplateName,
    force: true,
  };

  try {
    await datacatalog.deleteTagTemplate(tagTemplateRequest);
    console.log(`Deleted template: ${tagTemplateName}`);
  } catch (error) {
    console.log(`Cannot delete template: ${tagTemplateName}`);
  }

  // Construct the EntryGroup for the EntryGroup request.
  const entryGroup = {
    displayName: 'My awesome Entry Group',
    description: 'This Entry Group represents an external system',
  };

  // Construct the EntryGroup request to be sent by the client.
  const entryGroupRequest = {
    parent: datacatalog.locationPath(projectId, location),
    entryGroupId: entryGroupId,
    entryGroup: entryGroup,
  };

  // Use the client to send the API request.
  const [createdEntryGroup] =
    await datacatalog.createEntryGroup(entryGroupRequest);
  console.log(`Created entry group: ${createdEntryGroup.name}`);

  // Construct the Entry for the Entry request.
  const entry = {
    userSpecifiedSystem: 'onprem_data_system',
    userSpecifiedType: 'onprem_data_asset',
    displayName: 'My awesome data asset',
    description: 'This data asset is managed by an external system.',
    linkedResource: '//my-onprem-server.com/dataAssets/my-awesome-data-asset',
    schema: {
      columns: [
        {
          column: 'first_column',
          description: 'This columns consists of ....',
          mode: 'NULLABLE',
          type: 'STRING',
        },
        {
          column: 'second_column',
          description: 'This columns consists of ....',
          mode: 'NULLABLE',
          type: 'DOUBLE',
        },
      ],
    },
  };

  // Construct the Entry request to be sent by the client.
  const entryRequest = {
    parent: datacatalog.entryGroupPath(projectId, location, entryGroupId),
    entryId: entryId,
    entry: entry,
  };

  // Use the client to send the API request.
  const [createdEntry] = await datacatalog.createEntry(entryRequest);
  console.log(`Created entry: ${createdEntry.name}`);

  // Create a Tag Template.
  // For more field types, including ENUM, please refer to
  // https://cloud.google.com/data-catalog/docs/quickstarts/quickstart-search-tag#data-catalog-quickstart-nodejs.
  const fieldSource = {
    displayName: 'Source of data asset',
    type: {
      primitiveType: 'STRING',
    },
  };

  const tagTemplate = {
    displayName: 'Demo Tag Template',
    fields: {
      source: fieldSource,
    },
  };

  tagTemplateRequest = {
    parent: datacatalog.locationPath(projectId, location),
    tagTemplateId: tagTemplateId,
    tagTemplate: tagTemplate,
  };

  // Use the client to send the API request.
  const [createdTagTemplate] =
    await datacatalog.createTagTemplate(tagTemplateRequest);
  console.log(`Created template: ${createdTagTemplate.name}`);

  // Attach a Tag to the custom Entry.
  const tag = {
    template: createdTagTemplate.name,
    fields: {
      source: {
        stringValue: 'On-premises system name',
      },
    },
  };

  const tagRequest = {
    parent: createdEntry.name,
    tag: tag,
  };

  // Use the client to send the API request.
  const [createdTag] = await datacatalog.createTag(tagRequest);
  console.log(`Created tag: ${createdTag.name}`);
}
createCustomEntry();

Python

Antes de testar esta amostra, siga as instruções de configuração de Python no Guia de início rápido do Data Catalog: como usar bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API Data Catalog Python.

Para autenticar no Data Catalog, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

# Import required modules.
from google.cloud import datacatalog_v1

# Google Cloud Platform project.
project_id = "my-project"
# Entry Group to be created.
entry_group_id = "my_new_entry_group_id"
# Entry to be created.
entry_id = "my_new_entry_id"
# Currently, Data Catalog stores metadata in the us-central1 region.
location = "us-central1"

datacatalog = datacatalog_v1.DataCatalogClient()

# Create an Entry Group.
entry_group_obj = datacatalog_v1.types.EntryGroup()
entry_group_obj.display_name = "My awesome Entry Group"
entry_group_obj.description = "This Entry Group represents an external system"

entry_group = datacatalog.create_entry_group(
    parent=datacatalog_v1.DataCatalogClient.common_location_path(
        project_id, location
    ),
    entry_group_id=entry_group_id,
    entry_group=entry_group_obj,
)
entry_group_name = entry_group.name
print("Created entry group: {}".format(entry_group_name))

# Create an Entry.
entry = datacatalog_v1.types.Entry()
entry.user_specified_system = "onprem_data_system"
entry.user_specified_type = "onprem_data_asset"
entry.display_name = "My awesome data asset"
entry.description = "This data asset is managed by an external system."
entry.linked_resource = "//my-onprem-server.com/dataAssets/my-awesome-data-asset"

# Create the Schema, this is optional.
entry.schema.columns.append(
    datacatalog_v1.types.ColumnSchema(
        column="first_column",
        type_="STRING",
        description="This columns consists of ....",
        mode=None,
    )
)

entry.schema.columns.append(
    datacatalog_v1.types.ColumnSchema(
        column="second_column",
        type_="DOUBLE",
        description="This columns consists of ....",
        mode=None,
    )
)

entry = datacatalog.create_entry(
    parent=entry_group_name, entry_id=entry_id, entry=entry
)
print("Created entry: {}".format(entry.name))

REST e LINHA DE CMD

REST

Veja os exemplos a seguir e consulte a documentação da API REST Data Exchange entryGroups.create e entryGroups.entries.create.

1. Criar um grupo de entrada

Antes de usar os dados da solicitação, faça as substituições a seguir:

  • project-id: é o ID do projeto do Google Cloud.
  • entryGroupId: o ID precisa começar com uma letra ou um sublinhado, conter apenas letras, números e sublinhados do inglês e ter no máximo 64 caracteres.
  • displayName: o nome textual do grupo de entradas.

Método HTTP e URL:

POST https://datacatalog.googleapis.com/v1/projects/project-id/locations/region/entryGroups?entryGroupId=entryGroupId

Corpo JSON da solicitação:

{
  "displayName": "Entry Group display name"
}

Para enviar a solicitação, expanda uma destas opções:

Você receberá uma resposta JSON semelhante a esta:

{
  "name": "projects/my_projectid/locations/us-central1/entryGroups/my_entry_group",
  "displayName": "Entry Group display name",
  "dataCatalogTimestamps": {
    "createTime": "2019-10-19T16:35:50.135Z",
    "updateTime": "2019-10-19T16:35:50.135Z"
  }
}

2. Criar uma entrada personalizada no grupo de entrada

Antes de usar os dados da solicitação, faça as substituições a seguir:

  • project_id pelo ID do projeto do Google Cloud.
  • entryGroupId: ID do entryGroup existente. A entrada será criada neste EntryGroup.
  • entryId: ID da nova entrada. O ID deve começar com uma letra ou um sublinhado, conter somente letras, números e sublinhados ingleses e ter no máximo 64 caracteres.
  • description: descrição da entrada opcional
  • displayName: nome textual opcional para a entrada.
  • userSpecifiedType: nome do tipo personalizado. O nome do tipo deve começar com uma letra ou um sublinhado, conter apenas letras, números e sublinhados e deve ter no máximo 64 caracteres.
  • userSpecifiedSystem: o sistema de origem da entrada que não é do Google Cloud, que não é integrado ao Data Catalog. O nome do sistema de origem deve começar com uma letra ou um sublinhado, conter apenas letras, números e sublinhados e deve ter no máximo 64 caracteres.
  • linkedResource: nome completo opcional do recurso ao qual a entrada se refere.
  • schema: esquema de dados opcional.

    Exemplo de esquema JSON:
    { ...
      "schema": {
        "columns": [
          {
            "column": "first_name",
            "description": "First name",
            "mode": "REQUIRED",
            "type": "STRING"
          },
          {
            "column": "last_name",
            "description": "Last name",
            "mode": "REQUIRED",
            "type": "STRING"
          },
          {
            "column": "address",
            "description": "Address",
            "mode": "REPEATED",
            "subcolumns": [
              {
                "column": "city",
                "description": "City",
                "mode": "NULLABLE",
                "type": "STRING"
              },
              {
                "column": "state",
                "description": "State",
                "mode": "NULLABLE",
                "type": "STRING"
              }
            ],
            "type": "RECORD"
          }
        ]
      }
    ...
    }
    

Método HTTP e URL:

POST https://datacatalog.googleapis.com/v1/projects/project_id/locations/region/entryGroups/entryGroupId/entries?entryId=entryId

Corpo JSON da solicitação:

{
  "description": "Description",
  "displayName": "Display name",
  "userSpecifiedType": "my_type",
  "userSpecifiedSystem": "my_system",
  "linkedResource": "example.com/def",
  "schema": { schema }
}

Para enviar a solicitação, expanda uma destas opções:

Você receberá uma resposta JSON semelhante a esta:

{
  "name": "projects/my_project_id/locations/us-central1/entryGroups/my_entryGroup_id/entries/my_entry_id",
  "userSpecifiedType": "my-type",
  "userSpecifiedSystem": "my_system",
  "displayName": "On-prem entry",
  "description": "My entry description.",
  "schema": {
    "columns": [
      {
        "type": "STRING",
        "description": "First name",
        "mode": "REQUIRED",
        "column": "first_name"
      },
      {
        "type": "STRING",
        "description": "Last name",
        "mode": "REQUIRED",
        "column": "last_name"
      },
      {
        "type": "RECORD",
        "description": "Address",
        "mode": "REPEATED",
        "column": "address",
        "subcolumns": [
          {
            "type": "STRING",
            "description": "City",
            "mode": "NULLABLE",
            "column": "city"
          },
          {
            "type": "STRING",
            "description": "State",
            "mode": "NULLABLE",
            "column": "state"
          }
        ]
      }
    ]
  },
  "sourceSystemTimestamps": {
    "createTime": "2019-10-23T23:11:26.326Z",
    "updateTime": "2019-10-23T23:11:26.326Z"
  },
"linkedResource": "example.com/def"
}