Skip to content

Commit

Permalink
fix (samples) Import user events BQ, missing UpdateUserEventsJson.java (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
dfirova and gcf-owl-bot[bot] authored Aug 11, 2022
1 parent 03a61d5 commit e3c5ee4
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package events.setup;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;

public class UpdateUserEventsJson {

public static void main(String[] args) {
try {
String timestamp = LocalDateTime.now().minusDays(1).toString();
String filename = "src/main/resources/user_events.json";
updateFields(timestamp, filename);
filename = "src/main/resources/user_events_some_invalid.json";
updateFields(timestamp, filename);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void updateFields(String timestamp, String filename) throws IOException {
List<String> newLines = new ArrayList<>();
try (BufferedReader file = new BufferedReader(new FileReader(filename))) {
String line = file.readLine();
String field = "eventTime";
while (line != null) {
JSONObject object = new JSONObject(line);
object.put(field, timestamp);
newLines.add(object.toString());
line = file.readLine();
}
}
try (FileWriter file = new FileWriter(filename)) {
for (String event : newLines) {
file.write(event);
file.write("\n");
}
System.out.println("Successfully updated json file!");
}
}
}

0 comments on commit e3c5ee4

Please sign in to comment.