From ab55c05fa712fd0ac6313a7f2848ff1cd12e0ce0 Mon Sep 17 00:00:00 2001 From: dfirova <93149631+dfirova@users.noreply.github.com> Date: Thu, 11 Aug 2022 18:51:25 +0300 Subject: [PATCH] fix (samples) Import user events BQ, missing UpdateUserEventsJson.java (#494) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- java-retail/README.md | 1 + .../events/setup/UpdateUserEventsJson.java | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 java-retail/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java diff --git a/java-retail/README.md b/java-retail/README.md index f96f3ef2d0fb..071691212a65 100644 --- a/java-retail/README.md +++ b/java-retail/README.md @@ -85,6 +85,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m | Write User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | | Events Create Big Query Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | | Events Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | +| Update User Events Json | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java) | | Create Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | | Remove Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | | Add Fulfillment Places | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | diff --git a/java-retail/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java b/java-retail/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java new file mode 100644 index 000000000000..b23da9afcd6c --- /dev/null +++ b/java-retail/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java @@ -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 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!"); + } + } +}