Skip to content

Commit 9d32734

Browse files
committed
added a source files for CDK
1 parent 795c326 commit 9d32734

File tree

6 files changed

+252
-3
lines changed

6 files changed

+252
-3
lines changed

javav2/example_code/entityresolution/src/main/java/com/example/entity/scenario/EntityResScenario.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public static void main(String[] args) throws InterruptedException {
3232
String workflowName = "MyMatchingWorkflow451";
3333
String schemaName = "schema451";
3434

35-
// Use the AWS CDK to create this AWS resources. See the Readme file located at .
35+
// Use the AWS CDK to create these AWS resources.
36+
// See the Readme file located at resources/cdk/entityresolution_resources.
3637
String roleARN = "arn:aws:iam::814548047983:role/EntityResolutionCdkStack-EntityResolutionRoleB51A51-TSzkkBfrkbfm";
3738
String dataS3bucket = "glue-5ffb912c3d534e8493bac675c2a3196d";
3839
String outputBucket = "s3://entity-resolution-output-entityresolutioncdkstack";

resources/cdk/entityresolution_resources/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
Creates the following AWS resources for Amazon DynamoDB item tracker sample applications:
5+
Creates the following AWS resources for the AWS Entity Resolution scenario:
66

77
* An AWS IAM role that has permissions required to run this Scenario.
88
* An AWS Glue table that provides the input data for the entity resolution matching workflow.
@@ -21,7 +21,7 @@ You can use the AWS Cloud Development Kit (AWS CDK) or the AWS Command Line Inte
2121

2222
### Deploy with the AWS CDK
2323

24-
To deploy with the AWS CDK, you must install [Java JDK](https://www.oracle.com/ca-en/java/technologies/downloads/) and the
24+
To deploy with the AWS CDK, you must install [Java JDK 17](https://www.oracle.com/ca-en/java/technologies/downloads/) and the
2525
[AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html).
2626

2727
This example was built and tested with AWS CDK 2.135.0.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.myorg</groupId>
7+
<artifactId>entity_resolution_cdk</artifactId>
8+
<version>0.1</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<cdk.version>2.135.0</cdk.version>
13+
<constructs.version>[10.0.0,11.0.0)</constructs.version>
14+
<junit.version>5.7.1</junit.version>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>3.11.0</version>
23+
<configuration>
24+
<release>17</release>
25+
</configuration>
26+
</plugin>
27+
28+
<plugin>
29+
<groupId>org.codehaus.mojo</groupId>
30+
<artifactId>exec-maven-plugin</artifactId>
31+
<version>3.1.0</version>
32+
<configuration>
33+
<mainClass>com.myorg.EntityResolutionCdkApp</mainClass>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
<dependencies>
40+
<!-- AWS Cloud Development Kit -->
41+
<dependency>
42+
<groupId>software.amazon.awscdk</groupId>
43+
<artifactId>aws-cdk-lib</artifactId>
44+
<version>${cdk.version}</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>software.constructs</groupId>
49+
<artifactId>constructs</artifactId>
50+
<version>${constructs.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter</artifactId>
56+
<version>${junit.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.myorg;
5+
6+
import software.amazon.awscdk.App;
7+
import software.amazon.awscdk.Environment;
8+
import software.amazon.awscdk.StackProps;
9+
10+
import java.util.Arrays;
11+
12+
public class EntityResolutionCdkApp {
13+
public static void main(final String[] args) {
14+
App app = new App();
15+
16+
new EntityResolutionCdkStack(app, "EntityResolutionCdkStack", StackProps.builder()
17+
// If you don't specify 'env', this stack will be environment-agnostic.
18+
// Account/Region-dependent features and context lookups will not work,
19+
// but a single synthesized template can be deployed anywhere.
20+
21+
// Uncomment the next block to specialize this stack for the AWS Account
22+
// and Region that are implied by the current CLI configuration.
23+
/*
24+
.env(Environment.builder()
25+
.account(System.getenv("CDK_DEFAULT_ACCOUNT"))
26+
.region(System.getenv("CDK_DEFAULT_REGION"))
27+
.build())
28+
*/
29+
30+
// Uncomment the next block if you know exactly what Account and Region you
31+
// want to deploy the stack to.
32+
/*
33+
.env(Environment.builder()
34+
.account("123456789012")
35+
.region("us-east-1")
36+
.build())
37+
*/
38+
39+
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
40+
.build());
41+
42+
app.synth();
43+
}
44+
}
45+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.myorg;
5+
6+
import software.amazon.awscdk.*;
7+
import software.amazon.awscdk.services.iam.*;
8+
import software.amazon.awscdk.services.s3.*;
9+
import software.amazon.awscdk.services.glue.*;
10+
import software.constructs.Construct;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.UUID;
15+
16+
public class EntityResolutionCdkStack extends Stack {
17+
public EntityResolutionCdkStack(final Construct scope, final String id) {
18+
this(scope, id, null);
19+
}
20+
21+
public EntityResolutionCdkStack(final Construct scope, final String id, final StackProps props) {
22+
super(scope, id, props);
23+
24+
// 1. Create an S3 bucket for the Glue Data Table
25+
String uniqueId = UUID.randomUUID().toString().replace("-", ""); // Remove dashes to ensure compatibility
26+
Bucket glueDataBucket = Bucket.Builder.create(this, "GlueDataBucket")
27+
.bucketName("glue-" + uniqueId)
28+
.versioned(true)
29+
.build();
30+
31+
// 2. Create a Glue database
32+
CfnDatabase glueDatabase = CfnDatabase.Builder.create(this, "GlueDatabase")
33+
.catalogId(this.getAccount())
34+
.databaseInput(CfnDatabase.DatabaseInputProperty.builder()
35+
.name("entity_resolution_db")
36+
.build())
37+
.build();
38+
39+
// 3. Create a Glue table referencing the S3 bucket
40+
CfnTable glueTable = CfnTable.Builder.create(this, "GlueTable")
41+
.catalogId(this.getAccount())
42+
.databaseName(glueDatabase.getRef()) // Ensure Glue Table references the database correctly
43+
.tableInput(CfnTable.TableInputProperty.builder()
44+
.name("entity_resolution") // Fixed table name reference
45+
.tableType("EXTERNAL_TABLE")
46+
.storageDescriptor(CfnTable.StorageDescriptorProperty.builder()
47+
.columns(List.of(
48+
CfnTable.ColumnProperty.builder().name("id").type("string").build(), // Fixed: id is a string,
49+
CfnTable.ColumnProperty.builder().name("name").type("string").build(),
50+
CfnTable.ColumnProperty.builder().name("email").type("string").build()
51+
))
52+
.location("s3://" + glueDataBucket.getBucketName() + "/data/") // Append subpath for data
53+
.inputFormat("org.apache.hadoop.mapred.TextInputFormat")
54+
.outputFormat("org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat")
55+
.serdeInfo(CfnTable.SerdeInfoProperty.builder()
56+
.serializationLibrary("org.openx.data.jsonserde.JsonSerDe") // Set JSON SerDe
57+
.parameters(Map.of("serialization.format", "1")) // Optional: Set the format for JSON
58+
.build())
59+
.build())
60+
.build())
61+
.build();
62+
63+
// Ensure Glue Table is created after the Database
64+
glueTable.addDependency(glueDatabase);
65+
66+
// 4. Create an IAM Role for AWS Entity Resolution
67+
Role entityResolutionRole = Role.Builder.create(this, "EntityResolutionRole")
68+
.assumedBy(new ServicePrincipal("entityresolution.amazonaws.com")) // AWS Entity Resolution assumes this role
69+
.managedPolicies(List.of(
70+
ManagedPolicy.fromAwsManagedPolicyName("AmazonS3FullAccess"),
71+
ManagedPolicy.fromAwsManagedPolicyName("AWSEntityResolutionConsoleFullAccess"),
72+
ManagedPolicy.fromAwsManagedPolicyName("AWSGlueConsoleFullAccess"),
73+
ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSGlueServiceRole")
74+
))
75+
.build();
76+
77+
// Add custom permissions for Entity Resolution
78+
entityResolutionRole.addToPolicy(PolicyStatement.Builder.create()
79+
.actions(List.of(
80+
"entityresolution:StartMatchingWorkflow",
81+
"entityresolution:GetMatchingWorkflow"
82+
))
83+
.resources(List.of("*")) // Adjust permissions if needed
84+
.build());
85+
86+
// 5. Create an S3 bucket for output data
87+
Bucket outputBucket = Bucket.Builder.create(this, "OutputBucket")
88+
.bucketName("entity-resolution-output-" + id.toLowerCase())
89+
.versioned(true)
90+
.build();
91+
92+
// 6. Output the Role ARN
93+
new CfnOutput(this, "EntityResolutionArn", CfnOutputProps.builder()
94+
.value(entityResolutionRole.getRoleArn())
95+
.description("The ARN of the Glue Role")
96+
.build());
97+
98+
// 7. Construct and output the Glue Table ARN
99+
String glueTableArn = String.format("arn:aws:glue:%s:%s:table/%s/%s",
100+
this.getRegion(), // Region where the stack is deployed
101+
this.getAccount(), // AWS account ID
102+
glueDatabase.getRef(), // Glue database name (resolved reference)
103+
"entity_resolution" // Corrected table name
104+
);
105+
106+
new CfnOutput(this, "GlueTableArn", CfnOutputProps.builder()
107+
.value(glueTableArn)
108+
.description("The ARN of the Glue Table")
109+
.build());
110+
111+
// 8. Output the name of the Glue Data Bucket
112+
new CfnOutput(this, "GlueDataBucketName", CfnOutputProps.builder()
113+
.value(glueDataBucket.getBucketName()) // Outputs the bucket name
114+
.description("The name of the Glue Data Bucket")
115+
.build());
116+
}
117+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// package com.myorg;
2+
3+
// import software.amazon.awscdk.App;
4+
// import software.amazon.awscdk.assertions.Template;
5+
// import java.io.IOException;
6+
7+
// import java.util.HashMap;
8+
9+
// import org.junit.jupiter.api.Test;
10+
11+
// example test. To run these tests, uncomment this file, along with the
12+
// example resource in java/src/main/java/com/myorg/EntityResolutionCdkStack.java
13+
// public class EntityResolutionCdkTest {
14+
15+
// @Test
16+
// public void testStack() throws IOException {
17+
// App app = new App();
18+
// EntityResolutionCdkStack stack = new EntityResolutionCdkStack(app, "test");
19+
20+
// Template template = Template.fromStack(stack);
21+
22+
// template.hasResourceProperties("AWS::SQS::Queue", new HashMap<String, Number>() {{
23+
// put("VisibilityTimeout", 300);
24+
// }});
25+
// }
26+
// }

0 commit comments

Comments
 (0)