-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
03a61d5
commit e3c5ee4
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
retail/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
} |