Go
To learn how to install and use the client library for Sensitive Data Protection, see
Sensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
import (
"context"
"fmt"
"io"
dlp "cloud.google.com/go/dlp/apiv2"
"cloud.google.com/go/dlp/apiv2/dlppb"
)
// deidentifyTableInfotypes de-identifies table data with info types
func deidentifyTableInfotypes(w io.Writer, projectID string) error {
// projectId := "your-project-id"
row1 := &dlppb.Table_Row{
Values: []*dlppb.Value{
{Type: &dlppb.Value_StringValue{StringValue: "22"}},
{Type: &dlppb.Value_StringValue{StringValue: "Jane Austen"}},
{Type: &dlppb.Value_StringValue{StringValue: "21"}},
{Type: &dlppb.Value_StringValue{StringValue: "There are 14 kisses in Jane Austen's novels."}},
},
}
row2 := &dlppb.Table_Row{
Values: []*dlppb.Value{
{Type: &dlppb.Value_StringValue{StringValue: "55"}},
{Type: &dlppb.Value_StringValue{StringValue: "Mark Twain"}},
{Type: &dlppb.Value_StringValue{StringValue: "75"}},
{Type: &dlppb.Value_StringValue{StringValue: "Mark Twain loved cats."}},
},
}
row3 := &dlppb.Table_Row{
Values: []*dlppb.Value{
{Type: &dlppb.Value_StringValue{StringValue: "101"}},
{Type: &dlppb.Value_StringValue{StringValue: "Charles Dickens"}},
{Type: &dlppb.Value_StringValue{StringValue: "95"}},
{Type: &dlppb.Value_StringValue{StringValue: "Charles Dickens name was a curse invented by Shakespeare."}},
},
}
table := &dlppb.Table{
Headers: []*dlppb.FieldId{
{Name: "AGE"},
{Name: "PATIENT"},
{Name: "HAPPINESS SCORE"},
{Name: "FACTOID"},
},
Rows: []*dlppb.Table_Row{
{Values: row1.Values},
{Values: row2.Values},
{Values: row3.Values},
},
}
ctx := context.Background()
// Initialize a client once and reuse it to send multiple requests. Clients
// are safe to use across goroutines. When the client is no longer needed,
// call the Close method to cleanup its resources.
client, err := dlp.NewRESTClient(ctx)
if err != nil {
return err
}
// Closing the client safely cleans up background resources.
defer client.Close()
// Specify what content you want the service to de-identify.
contentItem := &dlppb.ContentItem{
DataItem: &dlppb.ContentItem_Table{
Table: table,
},
}
// Specify how the content should be de-identified.
// Select type of info to be replaced.
infoTypes := []*dlppb.InfoType{
{Name: "PERSON_NAME"},
}
// Specify that findings should be replaced with corresponding info type name.
replaceWithInfoTypeConfig := &dlppb.ReplaceWithInfoTypeConfig{}
primitiveTransformation := &dlppb.PrimitiveTransformation{
Transformation: &dlppb.PrimitiveTransformation_ReplaceWithInfoTypeConfig{
ReplaceWithInfoTypeConfig: replaceWithInfoTypeConfig,
},
}
// Associate info type with the replacement strategy
infoTypeTransformations := &dlppb.InfoTypeTransformations{
Transformations: []*dlppb.InfoTypeTransformations_InfoTypeTransformation{
{
InfoTypes: infoTypes,
PrimitiveTransformation: primitiveTransformation,
},
},
}
// Specify fields to be de-identified.
fields := []*dlppb.FieldId{
{Name: "PATIENT"},
{Name: "FACTOID"},
}
// Associate the de-identification and conditions with the specified field.
fieldTransformation := &dlppb.FieldTransformation{
Fields: fields,
Transformation: &dlppb.FieldTransformation_InfoTypeTransformations{
InfoTypeTransformations: infoTypeTransformations,
},
}
recordTransformations := &dlppb.RecordTransformations{
FieldTransformations: []*dlppb.FieldTransformation{
fieldTransformation,
},
}
// Construct the de-identification request to be sent by the client.
req := &dlppb.DeidentifyContentRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
DeidentifyConfig: &dlppb.DeidentifyConfig{
Transformation: &dlppb.DeidentifyConfig_RecordTransformations{
RecordTransformations: recordTransformations,
},
},
Item: contentItem,
}
// Send the request.
resp, err := client.DeidentifyContent(ctx, req)
if err != nil {
return err
}
// Print the results.
fmt.Fprintf(w, "Table after de-identification : %v", resp.GetItem().GetTable())
return nil
}
Node.js
To learn how to install and use the client library for Sensitive Data Protection, see
Sensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');
// Initialize google DLP Client
const dlp = new DLP.DlpServiceClient();
// The project ID to run the API call under
// const projectId = 'my-project';
// Construct the tabular data
const tablularData = {
headers: [
{name: 'AGE'},
{name: 'PATIENT'},
{name: 'HAPPINESS SCORE'},
{name: 'FACTOID'},
],
rows: [
{
values: [
{integerValue: 101},
{stringValue: 'Charles Dickens'},
{integerValue: 95},
{
stringValue:
'Charles Dickens name was a curse invented by Shakespeare.',
},
],
},
{
values: [
{integerValue: 22},
{stringValue: 'Jane Austen'},
{integerValue: 21},
{stringValue: "There are 14 kisses in Jane Austen's novels."},
],
},
{
values: [
{integerValue: 55},
{stringValue: 'Mark Twain'},
{integerValue: 75},
{stringValue: 'Mark Twain loved cats.'},
],
},
],
};
async function deIdentifyTableWithInfoType() {
// Column that needs to be transformed
const fieldIds = [{name: 'PATIENT'}, {name: 'FACTOID'}];
// Construct InfoTypeTransformations configurations
const infoTypeTransformations = {
transformations: [
{
infoTypes: [{name: 'PERSON_NAME'}],
primitiveTransformation: {
replaceWithInfoTypeConfig: {},
},
},
],
};
// Combine configurations into a request for the service.
const request = {
parent: `projects/${projectId}/locations/global`,
item: {
table: tablularData,
},
deidentifyConfig: {
recordTransformations: {
fieldTransformations: [
{
infoTypeTransformations,
fields: fieldIds,
},
],
},
},
};
// Send the request and receive response from the service
const [response] = await dlp.deidentifyContent(request);
// Print the results
console.log(
`Table after de-identification: ${JSON.stringify(response.item.table)}`
);
}
deIdentifyTableWithInfoType();