-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensorDataDynamoDBConnection.java
More file actions
184 lines (164 loc) · 7.36 KB
/
SensorDataDynamoDBConnection.java
File metadata and controls
184 lines (164 loc) · 7.36 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.Page;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
public class SensorDataDynamoDBConnection {
static AmazonDynamoDBClient dynamoDBClient ;
static DynamoDB dynamoDB;
static String tableName = "serverRoomSensor";
public static void main(String[] args) throws Exception {
AWSCredentials credentials = new ProfileCredentialsProvider("default").getCredentials();
dynamoDBClient = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
dynamoDBClient.setRegion(usWest2);
dynamoDB = new DynamoDB(dynamoDBClient);
String forumName = "Amazon DynamoDB";
String threadSubject = "DynamoDB Thread 1";
findRepliesForAThread(forumName, threadSubject);
// findRepliesForAThreadSpecifyOptionalLimit(forumName, threadSubject);
// findRepliesInLast15DaysWithConfig(forumName, threadSubject);
// findRepliesPostedWithinTimePeriod(forumName, threadSubject);
// findRepliesUsingAFilterExpression(forumName, threadSubject);
}
private static void findRepliesForAThread(String forumName, String threadSubject) {
Table table = dynamoDB.getTable(tableName);
HashMap<String, String> nameMap = new HashMap<String, String>();
nameMap.put("#key", "key");
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":key", "sensordata");
QuerySpec spec=new QuerySpec();
spec.withKeyConditionExpression("#key = :key");
spec.withNameMap(nameMap);
spec.withValueMap(valueMap);
ItemCollection<QueryOutcome> items = table.query(spec);
System.out.println("\nfindRepliesForAThread results:");
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
// private static void findRepliesForAThreadSpecifyOptionalLimit(String forumName, String threadSubject) {
//
// Table table = dynamoDB.getTable(tableName);
//
// String replyId = forumName + "#" + threadSubject;
//
// QuerySpec spec = new QuerySpec()
// .withKeyConditionExpression("Id = :v_id")
// .withValueMap(new ValueMap()
// .withString(":v_id", replyId))
// .withMaxPageSize(1);
//
// ItemCollection<QueryOutcome> items = table.query(spec);
//
// System.out.println("\nfindRepliesForAThreadSpecifyOptionalLimit results:");
//
// // Process each page of results
// int pageNum = 0;
// for (Page<Item, QueryOutcome> page : items.pages()) {
//
// System.out.println("\nPage: " + ++pageNum);
//
// // Process each item on the current page
// Iterator<Item> item = page.iterator();
// while (item.hasNext()) {
// System.out.println(item.next().toJSONPretty());
// }
// }
// }
//
// private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) {
//
// Table table = dynamoDB.getTable(tableName);
//
// long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
// Date twoWeeksAgo = new Date();
// twoWeeksAgo.setTime(twoWeeksAgoMilli);
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// String twoWeeksAgoStr = df.format(twoWeeksAgo);
//
// String replyId = forumName + "#" + threadSubject;
//
// QuerySpec spec = new QuerySpec()
// .withProjectionExpression("Message, ReplyDateTime, PostedBy")
// .withKeyConditionExpression("Id = :v_id and ReplyDateTime <= :v_reply_dt_tm")
// .withValueMap(new ValueMap()
// .withString(":v_id", replyId)
// .withString(":v_reply_dt_tm", twoWeeksAgoStr));
//
// ItemCollection<QueryOutcome> items = table.query(spec);
//
// System.out.println("\nfindRepliesInLast15DaysWithConfig results:");
// Iterator<Item> iterator = items.iterator();
// while (iterator.hasNext()) {
// System.out.println(iterator.next().toJSONPretty());
// }
//
// }
//
// private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) {
//
// Table table = dynamoDB.getTable(tableName);
//
// long startDateMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
// long endDateMilli = (new Date()).getTime() - (5L*24L*60L*60L*1000L);
// java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// String startDate = df.format(startDateMilli);
// String endDate = df.format(endDateMilli);
//
// String replyId = forumName + "#" + threadSubject;
//
// QuerySpec spec = new QuerySpec()
// .withProjectionExpression("Message, ReplyDateTime, PostedBy")
// .withKeyConditionExpression("Id = :v_id and ReplyDateTime between :v_start_dt and :v_end_dt")
// .withValueMap(new ValueMap()
// .withString(":v_id", replyId)
// .withString(":v_start_dt", startDate)
// .withString(":v_end_dt", endDate));
//
// ItemCollection<QueryOutcome> items = table.query(spec);
//
// System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
// Iterator<Item> iterator = items.iterator();
// while (iterator.hasNext()) {
// System.out.println(iterator.next().toJSONPretty());
// }
// }
//
// private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) {
//
// Table table = dynamoDB.getTable(tableName);
//
// String replyId = forumName + "#" + threadSubject;
//
// QuerySpec spec = new QuerySpec()
// .withProjectionExpression("Message, ReplyDateTime, PostedBy")
// .withKeyConditionExpression("Id = :v_id")
// .withFilterExpression("PostedBy = :v_postedby")
// .withValueMap(new ValueMap()
// .withString(":v_id", replyId)
// .withString(":v_postedby", "User B"));
//
// ItemCollection<QueryOutcome> items = table.query(spec);
//
// System.out.println("\nfindRepliesUsingAFilterExpression results:");
// Iterator<Item> iterator = items.iterator();
// while (iterator.hasNext()) {
// System.out.println(iterator.next().toJSONPretty());
// }
// }
}