Skip to content

Commit 1f4c1e2

Browse files
authored
finisihed datagen (#881)
Signed-off-by: Mark Nelson <mark.x.nelson@oracle.com>
1 parent 97419bf commit 1f4c1e2

File tree

3 files changed

+4609
-1
lines changed

3 files changed

+4609
-1
lines changed
Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,195 @@
11
package com.example.datagenerator;
22

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;
314
import org.springframework.boot.SpringApplication;
415
import org.springframework.boot.autoconfigure.SpringBootApplication;
516

617
@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<>();
847

948
public static void main(String[] args) {
1049
SpringApplication.run(DataGeneratorApplication.class, args);
1150
}
1251

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+
13195
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.datagenerator;
2+
3+
import java.util.Random;
4+
5+
public enum State {
6+
AL("Alabama"),
7+
MT("Montana"),
8+
AK("Alaska"),
9+
NE("Nebraska"),
10+
AZ("Arizona"),
11+
NV("Nevada"),
12+
AR("Arkansas"),
13+
NH("NewHampshire"),
14+
CA("California"),
15+
NJ("NewJersey"),
16+
CO("Colorado"),
17+
NM("NewMexico"),
18+
CT("Connecticut"),
19+
NY("NewYork"),
20+
DE("Delaware"),
21+
NC("NorthCarolina"),
22+
FL("Florida"),
23+
ND("NorthDakota"),
24+
GA("Georgia"),
25+
OH("Ohio"),
26+
HI("Hawaii"),
27+
OK("Oklahoma"),
28+
ID("Idaho"),
29+
OR("Oregon"),
30+
IL("Illinois"),
31+
PA("Pennsylvania"),
32+
IN("Indiana"),
33+
RI("RhodeIsland"),
34+
IA("Iowa"),
35+
SC("SouthCarolina"),
36+
KS("Kansas"),
37+
SD("SouthDakota"),
38+
KY("Kentucky"),
39+
TN("Tennessee"),
40+
LA("Louisiana"),
41+
TX("Texas"),
42+
ME("Maine"),
43+
UT("Utah"),
44+
MD("Maryland"),
45+
VT("Vermont"),
46+
MA("Massachusetts"),
47+
VA("Virginia"),
48+
MI("Michigan"),
49+
WA("Washington"),
50+
MN("Minnesota"),
51+
WV("WestVirginia"),
52+
MS("Mississippi"),
53+
WI("Wisconsin"),
54+
MO("Missouri"),
55+
WY("Wyoming");
56+
57+
private final String state;
58+
59+
private State(String state) {
60+
this.state = state;
61+
}
62+
63+
public String getStateCode() {
64+
return this.state;
65+
}
66+
67+
public static String randomStateCode() {
68+
return values()[(new Random()).nextInt(values().length)].toString();
69+
}
70+
}

0 commit comments

Comments
 (0)