Skip to content

Commit bb916dd

Browse files
dfirovagcf-owl-bot[bot]
authored andcommitted
fix (samples) Import user events BQ, missing UpdateUserEventsJson.java (#494)
* Added UpdateUserEventsJson class. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 6d71849 commit bb916dd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package events.setup;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.time.LocalDateTime;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import org.json.JSONObject;
11+
12+
public class UpdateUserEventsJson {
13+
14+
public static void main(String[] args) {
15+
try {
16+
String timestamp = LocalDateTime.now().minusDays(1).toString();
17+
String filename = "src/main/resources/user_events.json";
18+
updateFields(timestamp, filename);
19+
filename = "src/main/resources/user_events_some_invalid.json";
20+
updateFields(timestamp, filename);
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
private static void updateFields(String timestamp, String filename) throws IOException {
27+
List<String> newLines = new ArrayList<>();
28+
try (BufferedReader file = new BufferedReader(new FileReader(filename))) {
29+
String line = file.readLine();
30+
String field = "eventTime";
31+
while (line != null) {
32+
JSONObject object = new JSONObject(line);
33+
object.put(field, timestamp);
34+
newLines.add(object.toString());
35+
line = file.readLine();
36+
}
37+
}
38+
try (FileWriter file = new FileWriter(filename)) {
39+
for (String event : newLines) {
40+
file.write(event);
41+
file.write("\n");
42+
}
43+
System.out.println("Successfully updated json file!");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)