Skip to content

Commit fe2c19d

Browse files
authored
Add upsert search attributes example (#29)
1 parent 99aaae0 commit fe2c19d

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ To run the hello world samples:
9696
./gradlew -q execute -PmainClass=com.uber.cadence.samples.hello.HelloPeriodic
9797
./gradlew -q execute -PmainClass=com.uber.cadence.samples.hello.HelloQuery
9898
./gradlew -q execute -PmainClass=com.uber.cadence.samples.hello.HelloSignal
99+
./gradlew -q execute -PmainClass=com.uber.cadence.samples.hello.HelloSearchAttributes
99100

100101
### File Processing
101102

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ repositories {
3535
dependencies {
3636
errorproneJavac("com.google.errorprone:javac:9+181-r4173-1")
3737
errorprone("com.google.errorprone:error_prone_core:2.3.1")
38-
compile group: 'com.uber.cadence', name: 'cadence-client', version: '2.6.0'
38+
compile group: 'com.uber.cadence', name: 'cadence-client', version: '2.6.3'
3939
compile group: 'commons-configuration', name: 'commons-configuration', version: '1.9'
4040
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
4141
testCompile group: 'junit', name: 'junit', version: '4.12'

src/main/java/com/uber/cadence/samples/hello/HelloSearchAttributes.java

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
import com.uber.cadence.activity.ActivityMethod;
2727
import com.uber.cadence.client.WorkflowClient;
2828
import com.uber.cadence.client.WorkflowOptions;
29-
import com.uber.cadence.converter.DataConverter;
30-
import com.uber.cadence.converter.JsonDataConverter;
3129
import com.uber.cadence.serviceclient.IWorkflowService;
3230
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
3331
import com.uber.cadence.worker.Worker;
3432
import com.uber.cadence.workflow.Workflow;
3533
import com.uber.cadence.workflow.WorkflowMethod;
36-
import java.nio.ByteBuffer;
34+
import com.uber.cadence.workflow.WorkflowUtils;
3735
import java.time.LocalDateTime;
3836
import java.time.format.DateTimeFormatter;
3937
import java.util.HashMap;
@@ -70,9 +68,54 @@ public static class GreetingWorkflowImpl implements HelloActivity.GreetingWorkfl
7068

7169
@Override
7270
public String getGreeting(String name) {
71+
SearchAttributes currentSearchAttributes = Workflow.getWorkflowInfo().getSearchAttributes();
72+
// Use System.out just for demo, please use Workflow.getLogger in production.
73+
System.out.println("Search Attributes on start: ");
74+
printSearchAttributes(currentSearchAttributes);
75+
76+
// update some of the search attributes
77+
Map<String, Object> upsertedMap = new HashMap<>();
78+
upsertedMap.put("CustomKeywordField", name);
79+
Workflow.upsertSearchAttributes(upsertedMap);
80+
81+
currentSearchAttributes = Workflow.getWorkflowInfo().getSearchAttributes();
82+
System.out.println("Search Attributes after upsert: ");
83+
printSearchAttributes(currentSearchAttributes);
84+
7385
// This is a blocking call that returns only after the activity has completed.
7486
return activities.composeGreeting("Hello", name);
7587
}
88+
89+
private void printSearchAttributes(SearchAttributes searchAttributes) {
90+
if (searchAttributes == null) {
91+
return;
92+
}
93+
searchAttributes
94+
.getIndexedFields()
95+
.forEach(
96+
(k, v) -> {
97+
System.out.printf("%s: %s\n", k, getValueForKey(k, searchAttributes));
98+
});
99+
}
100+
101+
private String getValueForKey(String key, SearchAttributes searchAttributes) {
102+
switch (key) {
103+
case "CustomKeywordField":
104+
case "CustomDatetimeField":
105+
case "CustomStringField":
106+
return WorkflowUtils.getValueFromSearchAttributes(searchAttributes, key, String.class);
107+
case "CustomIntField":
108+
return WorkflowUtils.getValueFromSearchAttributes(searchAttributes, key, Integer.class)
109+
.toString();
110+
case "CustomDoubleField":
111+
return WorkflowUtils.getValueFromSearchAttributes(searchAttributes, key, Double.class)
112+
.toString();
113+
case "CustomBoolField":
114+
return WorkflowUtils.getValueFromSearchAttributes(searchAttributes, key, Boolean.class)
115+
.toString();
116+
}
117+
return "Unknown key";
118+
}
76119
}
77120

78121
static class GreetingActivitiesImpl implements HelloActivity.GreetingActivities {
@@ -109,8 +152,10 @@ public static void main(String[] args) {
109152
workflowClient.newWorkflowStub(
110153
HelloSearchAttributes.GreetingWorkflow.class, workflowOptions);
111154
// Execute a workflow waiting for it to complete.
112-
String greeting = workflow.getGreeting("SearchAttributes");
155+
String greeting = workflow.getGreeting("World");
113156

157+
// Bellow shows how to read search attributes using DescribeWorkflowExecution API
158+
// You can do similar things using ListWorkflowExecutions
114159
IWorkflowService cadenceService = new WorkflowServiceTChannel();
115160
WorkflowExecution execution = new WorkflowExecution();
116161
execution.setWorkflowId(workflowID);
@@ -121,7 +166,9 @@ public static void main(String[] args) {
121166
try {
122167
DescribeWorkflowExecutionResponse resp = cadenceService.DescribeWorkflowExecution(request);
123168
SearchAttributes searchAttributes = resp.workflowExecutionInfo.getSearchAttributes();
124-
String keyword = getKeywordFromSearchAttribute(searchAttributes);
169+
String keyword =
170+
WorkflowUtils.getValueFromSearchAttributes(
171+
searchAttributes, "CustomKeywordField", String.class);
125172
System.out.printf("In workflow we get CustomKeywordField is: %s\n", keyword);
126173
} catch (Exception e) {
127174
System.out.println(e);
@@ -135,7 +182,7 @@ private static Map<String, Object> generateSearchAttributes() {
135182
Map<String, Object> searchAttributes = new HashMap<>();
136183
searchAttributes.put(
137184
"CustomKeywordField",
138-
"keys"); // each field can also be array such as: String[] keys = {"k1", "k2"};
185+
"old world"); // each field can also be array such as: String[] keys = {"k1", "k2"};
139186
searchAttributes.put("CustomIntField", 1);
140187
searchAttributes.put("CustomDoubleField", 0.1);
141188
searchAttributes.put("CustomBoolField", true);
@@ -153,14 +200,4 @@ private static String generateDateTimeFieldValue() {
153200
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
154201
return currentDateTime.format(formatter);
155202
}
156-
157-
// example for extract value from search attributes
158-
private static String getKeywordFromSearchAttribute(SearchAttributes searchAttributes) {
159-
Map<String, ByteBuffer> map = searchAttributes.getIndexedFields();
160-
ByteBuffer byteBuffer = map.get("CustomKeywordField");
161-
DataConverter dataConverter = JsonDataConverter.getInstance();
162-
final byte[] valueBytes = new byte[byteBuffer.limit() - byteBuffer.position()];
163-
byteBuffer.get(valueBytes, 0, valueBytes.length);
164-
return dataConverter.fromData(valueBytes, String.class, String.class);
165-
}
166203
}

0 commit comments

Comments
 (0)