|
1 | 1 | package com.example.datagenerator;
|
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Random; |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +import org.springframework.boot.CommandLineRunner; |
3 | 14 | import org.springframework.boot.SpringApplication;
|
4 | 15 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
5 | 16 |
|
6 | 17 | @SpringBootApplication
|
7 |
| -public class DataGeneratorApplication { |
| 18 | +public class DataGeneratorApplication implements CommandLineRunner { |
| 19 | + |
| 20 | + private static final String outputFile = "target/load-data.sql"; |
| 21 | + private static final String mensNamesFile = "src/main/resources/mens-names.txt"; |
| 22 | + private static final String womensNamesFile = "src/main/resources/womens-names.txt"; |
| 23 | + private static final String surnamesFile = "src/main/resources/surnames.txt"; |
| 24 | + private static final String streetNamesFile = "src/main/resources/street-names.txt"; |
| 25 | + private static final String zipcodesFile = "src/main/resources/nj-zipcodes-placenames.txt"; |
| 26 | + private static final String imagesFile = "src/main/resources/input-files.txt"; |
| 27 | + |
| 28 | + private static final Random rand = new Random(); |
| 29 | + private static final int NUM_VEHICLES = 1_000_000; |
| 30 | + |
| 31 | + private static final String personSql = """ |
| 32 | + insert into customer (customer_id, account_number, first_name, last_name, address, city, zipcode) |
| 33 | + values ('X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7'); |
| 34 | + """; |
| 35 | + |
| 36 | + private static final String vehicleSql = """ |
| 37 | + insert into vehicle (vehicle_id, customer_id, tag_id, state, license_plate, vehicle_type, image) |
| 38 | + values ('X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7'); |
| 39 | + """; |
| 40 | + |
| 41 | + private static ArrayList<String> mensNames = new ArrayList<>(); |
| 42 | + private static ArrayList<String> womensNames = new ArrayList<>(); |
| 43 | + private static ArrayList<String> surnames = new ArrayList<>(); |
| 44 | + private static ArrayList<String> streetNames = new ArrayList<>(); |
| 45 | + private static ArrayList<Location> locations = new ArrayList<>(); |
| 46 | + private static ArrayList<Image> images = new ArrayList<>(); |
8 | 47 |
|
9 | 48 | public static void main(String[] args) {
|
10 | 49 | SpringApplication.run(DataGeneratorApplication.class, args);
|
11 | 50 | }
|
12 | 51 |
|
| 52 | + @Override |
| 53 | + public void run(String... args) { |
| 54 | + |
| 55 | + try ( |
| 56 | + BufferedReader mensNamesReader = new BufferedReader(new FileReader(mensNamesFile)); |
| 57 | + BufferedReader womensNamesReader = new BufferedReader(new FileReader(womensNamesFile)); |
| 58 | + BufferedReader surnamesReader = new BufferedReader(new FileReader(surnamesFile)); |
| 59 | + BufferedReader streetNamesReader = new BufferedReader(new FileReader(streetNamesFile)); |
| 60 | + BufferedReader zipcodeReader = new BufferedReader(new FileReader(zipcodesFile)); |
| 61 | + BufferedReader imagesReader = new BufferedReader(new FileReader(imagesFile)); |
| 62 | + ) { |
| 63 | + // read through the input file and set up the source data |
| 64 | + String line; |
| 65 | + while ((line = mensNamesReader.readLine()) != null) { |
| 66 | + mensNames.add(line); |
| 67 | + } |
| 68 | + |
| 69 | + while ((line = womensNamesReader.readLine()) != null) { |
| 70 | + womensNames.add(line); |
| 71 | + } |
| 72 | + |
| 73 | + while ((line = surnamesReader.readLine()) != null) { |
| 74 | + surnames.add(line); |
| 75 | + } |
| 76 | + |
| 77 | + while ((line = streetNamesReader.readLine()) != null) { |
| 78 | + streetNames.add(line); |
| 79 | + } |
| 80 | + |
| 81 | + while ((line = zipcodeReader.readLine()) != null) { |
| 82 | + String[] location = line.split("\t"); |
| 83 | + String zipcode = location[0]; |
| 84 | + String city = location[1]; |
| 85 | + locations.add(new Location(zipcode, city)); |
| 86 | + } |
| 87 | + |
| 88 | + while ((line = imagesReader.readLine()) != null) { |
| 89 | + String[] image = line.split("/"); |
| 90 | + images.add(new Image(image[1], line)); |
| 91 | + } |
| 92 | + |
| 93 | + System.out.println("Finished loading source data:"); |
| 94 | + System.out.println("men's names: " + mensNames.size()); |
| 95 | + System.out.println("women's names: " + womensNames.size()); |
| 96 | + System.out.println("surnames: " + surnames.size()); |
| 97 | + System.out.println("street names: " + streetNames.size()); |
| 98 | + System.out.println("locations: " + locations.size()); |
| 99 | + System.out.println("images: " + images.size()); |
| 100 | + |
| 101 | + } catch (IOException ignore) {} |
| 102 | + |
| 103 | + try ( |
| 104 | + BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); |
| 105 | + ) { |
| 106 | + // do the things |
| 107 | + for (int vehicleCount = 0; vehicleCount < NUM_VEHICLES; vehicleCount++) { |
| 108 | + |
| 109 | + // we want 1 to 3 vehicles per person |
| 110 | + int vehiclesForThisPerson = rand.nextInt(3) + 1; |
| 111 | + |
| 112 | + // create a person |
| 113 | + boolean man = rand.nextBoolean(); |
| 114 | + String firstName = (man ? mensNames.get(rand.nextInt(mensNames.size())) |
| 115 | + : womensNames.get(rand.nextInt(womensNames.size()))); |
| 116 | + String lastName = surnames.get(rand.nextInt(surnames.size())); |
| 117 | + String address = ""; |
| 118 | + int k = rand.nextInt(4) + 1; |
| 119 | + for (int j = 0; j < k; j++) { |
| 120 | + address = address.concat(randomDigit()); |
| 121 | + } |
| 122 | + address = address.concat(" ").concat(streetNames.get(rand.nextInt(streetNames.size()))); |
| 123 | + Location l = locations.get(rand.nextInt(locations.size())); |
| 124 | + String city = l.city(); |
| 125 | + String zipcode = l.zipcode(); |
| 126 | + String accountNumber = randomDigit() |
| 127 | + .concat(randomDigit()) |
| 128 | + .concat(randomDigit()) |
| 129 | + .concat(randomLetter()) |
| 130 | + .concat(randomDigit()) |
| 131 | + .concat(randomDigit()) |
| 132 | + .concat(randomDigit()) |
| 133 | + .concat(randomDigit()); |
| 134 | + String customerId = UUID.randomUUID().toString(); |
| 135 | + |
| 136 | + writer.write(personSql.replace("X1", customerId) |
| 137 | + .replace("X2", accountNumber) |
| 138 | + .replace("X3", firstName) |
| 139 | + .replace("X4", lastName) |
| 140 | + .replace("X5", address) |
| 141 | + .replace("X6", city) |
| 142 | + .replace("X7", zipcode)); |
| 143 | + |
| 144 | + // now create the vehicles for this person |
| 145 | + for (int j = 0; j < vehiclesForThisPerson; j++) { |
| 146 | + |
| 147 | + Vehicle v = generateVehicle(); |
| 148 | + |
| 149 | + writer.write(vehicleSql.replace("X1", v.vehicleId()) |
| 150 | + .replace("X2", customerId) |
| 151 | + .replace("X3", v.tagId()) |
| 152 | + .replace("X4", v.state()) |
| 153 | + .replace("X5", v.licensePlate) |
| 154 | + .replace("X6", v.vehicleType.toUpperCase()) |
| 155 | + .replace("X7", v.photoFilename())); |
| 156 | + vehicleCount++; |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + } catch (IOException ignore) {} |
| 163 | + } |
| 164 | + |
| 165 | + private Vehicle generateVehicle() { |
| 166 | + Image image = images.get(rand.nextInt(images.size())); |
| 167 | + String vehicleId = UUID.randomUUID().toString(); |
| 168 | + String tagId = UUID.randomUUID().toString(); |
| 169 | + String state = State.randomStateCode(); |
| 170 | + String licensePlate = randomLetter() |
| 171 | + .concat(randomLetter()) |
| 172 | + .concat(randomLetter()) |
| 173 | + .concat(randomDigit()) |
| 174 | + .concat(randomDigit()) |
| 175 | + .concat(randomDigit()); |
| 176 | + return new Vehicle(vehicleId, tagId, state, licensePlate, image.vehicleType, image.filename); |
| 177 | + } |
| 178 | + |
| 179 | + private String randomLetter() { |
| 180 | + char[] c =new char[1]; |
| 181 | + c[0] = (char)(rand.nextInt(26) + 'A'); |
| 182 | + return new String(c); |
| 183 | + } |
| 184 | + |
| 185 | + private String randomDigit() { |
| 186 | + char[] c = new char[1]; |
| 187 | + c[0] = (char)(rand.nextInt(10) + '0'); |
| 188 | + return new String(c); |
| 189 | + } |
| 190 | + |
| 191 | + private record Vehicle (String vehicleId, String tagId, String state, String licensePlate, String vehicleType, String photoFilename) {}; |
| 192 | + private record Location (String zipcode, String city) {}; |
| 193 | + private record Image (String vehicleType, String filename) {}; |
| 194 | + |
13 | 195 | }
|
0 commit comments