import java.io.*; import java.util.*; /** Generates a synthetic database for use in CSE 444 mini-project 3, outputting the generated tables into CSV files that can be inserted into a database using the DBMS's bulk import command. @author Michael Ratanapintha */ public class DatabaseGenerator { /* See importDatabase.sql for the database schema */ /** Scales the number of rows in each table (see sizeR, sizeS, etc. to see how the scaling is done). */ private static int tableSizeScale = 100; private static int sizeR() { return 1000 * tableSizeScale; }; private static int sizeS() { return 1000 * tableSizeScale; }; private static int sizeT() { return 1000 * tableSizeScale; }; private static int sizeU() { return 1000 * tableSizeScale; }; public static void main (String[] args) { Random r = new Random(); if (args.length != 0 && args.length != 1) { System.out.println ("Usage: java DatabaseGenerator tableSizeScale"); System.exit (1); } if (args.length == 1) { tableSizeScale = Integer.parseInt(args[0]); } MakeR ("tableR.txt", r); MakeS ("tableS.txt", r); MakeT ("tableT.txt", r); MakeU ("tableU.txt", r); } private static void MakeR (String filename, Random prng) { PrintStream Rstream = null; try { Rstream = new PrintStream (filename); for (int key = 1; key <= sizeR(); key++) { Rstream.print (key + "\t"); // write fields A, B, C Rstream.printf("%d\t%d\t%d\t", prng.nextInt(), prng.nextInt(), prng.nextInt()); // Generate the varchar field by string-izing a byte array // (there is slightly less randomness here because while the individual bytes // are random, the array is always printed in the format [1, 2, 3,...], // with max 6 chars per byte (4 chars for value plus ", ", plus "[]" and less ", " for last byte value)) byte arr_bytes[] = new byte[100]; prng.nextBytes (arr_bytes); Rstream.println(Arrays.toString(arr_bytes)); } } catch (Throwable ex) { System.err.println ("Cannot open or write to table R output file: \"" + filename + "\""); System.err.println ("Error info:"); System.err.println (ex); } finally { if (null != Rstream) Rstream.close(); } } private static void MakeS (String filename, Random prng) { PrintStream Sstream = null; try { Sstream = new PrintStream (filename); for (int key = 1; key <= sizeS(); key++) { Sstream.print (key + "\t"); // write fields A, B, C int a = prng.nextInt(); int b = prng.nextInt(); if (b < 0) b = -b; b = (b % sizeT())+1; /* foreign key into T */ int c = prng.nextInt(); Sstream.printf("%d\t%d\t%d", a,b,c); Sstream.println(); } } catch (Throwable ex) { System.err.println ("Cannot open or write to table S output file: \"" + filename + "\""); System.err.println ("Error info:"); System.err.println (ex); } finally { if (null != Sstream) Sstream.close(); } } private static void MakeT (String filename, Random prng) { PrintStream Tstream = null; try { Tstream = new PrintStream (filename); for (int key = 1; key <= sizeT(); key++) { Tstream.print (key + "\t"); // write fields D, E Tstream.printf("%d\t%d", prng.nextInt(), prng.nextInt()); Tstream.println(); } } catch (Throwable ex) { System.err.println ("Cannot open or write to table T output file: \"" + filename + "\""); System.err.println ("Error info:"); System.err.println (ex); } finally { if (null != Tstream) Tstream.close(); } } private static void MakeU (String filename, Random prng) { PrintStream Ustream = null; try { Ustream = new PrintStream (filename); for (int key = 1; key <= sizeU(); key++) { Ustream.print (key + "\t"); // write field A int a = prng.nextInt(); if (a % 1000 == 0) a = 0; Ustream.print (a + "\t"); // write field B Ustream.println(prng.nextInt()); } } catch (Throwable ex) { System.err.println ("Cannot open or write to table T output file: \"" + filename + "\""); System.err.println ("Error info:"); System.err.println (ex); } finally { if (null != Ustream) Ustream.close(); } } }