-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDeidentifyTextExample.java
More file actions
142 lines (118 loc) · 7.13 KB
/
DeidentifyTextExample.java
File metadata and controls
142 lines (118 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.example.detect;
import java.util.ArrayList;
import java.util.List;
import com.skyflow.Skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.DetectEntities;
import com.skyflow.enums.Env;
import com.skyflow.enums.LogLevel;
import com.skyflow.errors.SkyflowException;
import com.skyflow.vault.detect.DeidentifyTextRequest;
import com.skyflow.vault.detect.DeidentifyTextResponse;
import com.skyflow.vault.detect.TokenFormat;
/**
* Skyflow Deidentify Text Example
* <p>
* This example demonstrates how to use the Skyflow SDK to deidentify text data
* across multiple vaults. It includes:
* 1. Setting up credentials and vault configurations.
* 2. Creating a Skyflow client with multiple vaults.
* 3. Performing deidentify of text with various options.
* 4. Handling responses and errors.
*/
public class DeidentifyTextExample {
public static void main(String[] args) throws SkyflowException {
// Step 1: Set up credentials for the first vault configuration
Credentials credentials = new Credentials();
credentials.setPath("<YOUR_CREDENTIALS_FILE_PATH_1>"); // Replace with the path to the credentials file
// Step 2: Configure the first vault (Blitz)
VaultConfig blitzConfig = new VaultConfig();
blitzConfig.setVaultId("<YOUR_VAULT_ID_1>"); // Replace with the ID of the first vault
blitzConfig.setClusterId("<YOUR_CLUSTER_ID_1>"); // Replace with the cluster ID of the first vault
blitzConfig.setEnv(Env.DEV); // Set the environment (e.g., DEV, STAGE, PROD)
blitzConfig.setCredentials(credentials); // Associate the credentials with the vault
// Step 3: Configure the second vault (Stage)
VaultConfig stageConfig = new VaultConfig();
stageConfig.setVaultId("<YOUR_VAULT_ID_2>"); // Replace with the ID of the second vault
stageConfig.setClusterId("<YOUR_CLUSTER_ID_2>"); // Replace with the cluster ID of the second vault
stageConfig.setEnv(Env.STAGE); // Set the environment for the second vault
// Step 4: Set up credentials for the Skyflow client
Credentials skyflowCredentials = new Credentials();
skyflowCredentials.setPath("<YOUR_CREDENTIALS_FILE_PATH_2>"); // Replace with the path to another credentials file
// Step 5: Create a Skyflow client and add vault configurations
Skyflow skyflowClient = Skyflow.builder()
.setLogLevel(LogLevel.DEBUG) // Enable debugging for detailed logs
.addVaultConfig(blitzConfig) // Add the first vault configuration
.addVaultConfig(stageConfig) // Add the second vault configuration
.addSkyflowCredentials(skyflowCredentials) // Add general Skyflow credentials
.build();
// Step 6: Configuring the different options for deidentify
// Replace with the entity you want to detect
List<DetectEntities> detectEntitiesList = new ArrayList<>();
detectEntitiesList.add(DetectEntities.SSN);
detectEntitiesList.add(DetectEntities.CREDIT_CARD);
// Replace with the entity you want to detect with vault token
List<DetectEntities> vaultTokenList = new ArrayList<>();
vaultTokenList.add(DetectEntities.SSN);
vaultTokenList.add(DetectEntities.CREDIT_CARD);
// Replace with the entity you want to detect with entity only
// List<DetectEntities> entityOnlyList = new ArrayList<>();
// entityOnlyList.add(DetectEntities.SSN);
// Replace with the entity you want to detect with entity unique counter
// List<DetectEntities> entityUniqueCounterList = new ArrayList<>();
// entityUniqueCounterList.add(DetectEntities.SSN);
// Replace with the regex patterns you want to allow during deidentification
// List<String> allowRegexList = new ArrayList<>();
// allowRegexList.add("<YOUR_ALLOW_REGEX_LIST>");
// Replace with the regex patterns you want to restrict during deidentification
// List<String> restrictRegexList = new ArrayList<>();
// restrictRegexList.add("YOUR_RESTRICT_REGEX_LIST");
// Configure Token Format
TokenFormat tokenFormat = TokenFormat.builder()
.vaultToken(vaultTokenList)
// .entityOnly(entityOnlyList)
// .entityUniqueCounter(entityUniqueCounterList)
.build();
// Configure Transformation for deidentified entities
// List<DetectEntities> detectEntitiesTransformationList = new ArrayList<>();
// detectEntitiesTransformationList.add(DetectEntities.DOB); // Replace with the entity you want to transform
// detectEntitiesTransformationList.add(DetectEntities.DATE);
// DateTransformation dateTransformation = new DateTransformation(20, 5, detectEntitiesTransformationList);
// Transformations transformations = new Transformations(dateTransformation);
// Example 1: Deidentify text on the first vault
try {
// Create a deidentify text request for the first vault
DeidentifyTextRequest deidentifyTextRequest = DeidentifyTextRequest.builder()
.text("My SSN is 123-45-6789 and my card is 4111 1111 1111 1111.") // Replace with the text you want to deidentify
.entities(detectEntitiesList)
// .allowRegexList(allowRegexList)
// .restrictRegexList(restrictRegexList)
.tokenFormat(tokenFormat)
// .transformations(transformations)
.build();
DeidentifyTextResponse deidentifyTextResponse = skyflowClient.detect(blitzConfig.getVaultId()).deidentifyText(deidentifyTextRequest);
System.out.println("Deidentify text Response (Vault1): " + deidentifyTextResponse);
} catch (SkyflowException e) {
System.err.println("Error occurred during deidentify (Vault1): ");
e.printStackTrace();
}
// Example 2: Deidentify text on the second vault
try {
// Create a deidentify text request for the second vault
DeidentifyTextRequest deidentifyTextRequest2 = DeidentifyTextRequest.builder()
.text("My SSN is 123-45-6789 and my card is 4111 1111 1111 1111.") // Replace with the text you want to deidentify
.entities(detectEntitiesList)
// .allowRegexList(allowRegexList)
// .restrictRegexList(restrictRegexList)
.tokenFormat(tokenFormat)
// .transformations(transformations)
.build();
DeidentifyTextResponse deidentifyTextResponse2 = skyflowClient.detect(stageConfig.getVaultId()).deidentifyText(deidentifyTextRequest2);
System.out.println("Deidentify text Response (Vault2): " + deidentifyTextResponse2);
} catch (SkyflowException e) {
System.err.println("Error occurred during deidentify (Vault2): ");
e.printStackTrace();
}
}
}