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!"); + } + } +}