You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 lines
6.6 KiB
266 lines
6.6 KiB
package de.memtext.db; |
|
import java.io.BufferedWriter; |
|
import java.io.FileWriter; |
|
import java.sql.Connection; |
|
import java.sql.DriverManager; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
import java.util.Enumeration; |
|
import java.util.Random; |
|
import java.util.Vector; |
|
/** |
|
* Randomizes values in a database. |
|
* Reads all values of a given table column (select distinct x from..) and |
|
* multiplies all occurences of a single value (e.g. 150) with a factor between |
|
* minFactor and maxFactor (e.g. 150*1.1 -> all entries with 150 become 165) |
|
* |
|
* if boolean printOutOnly is true the update sql statements are only logged |
|
* to console, not performed |
|
* */ |
|
public class ValueRandomizer { |
|
//private static String driver = "org.hsqldb.jdbcDriver"; |
|
private static String driver = "org.postgresql.Driver"; |
|
//private static String dbUrl = "jdbc:hsqldb:hsql://localhost:9999"; |
|
private static String dbUrl = "jdbc:postgresql://localhost:5432/superx"; |
|
private static String username = "superx"; //"joolap"; |
|
private static String password = ""; //"loopja3000"; |
|
private static String tablename = "cob_busa"; |
|
private static String colname = "betrag"; //"value"; |
|
private static String datenart = "DOUBLE"; //or "INTEGER"; |
|
|
|
private static double minFactor = 0.6; |
|
private static double maxFactor = 1.8; |
|
|
|
private static boolean printOutOnly = false; |
|
private static Statement stat; |
|
private static Random r = new Random(); |
|
public static void main(String args[]) { |
|
|
|
try { |
|
Class.forName(driver); |
|
Connection conn; |
|
System.out.println("establishing connection to " + dbUrl); |
|
conn = DriverManager.getConnection(dbUrl, username, password); |
|
stat = conn.createStatement(); |
|
System.out.println("done."); |
|
out(); |
|
stat.close(); |
|
conn.close(); |
|
System.exit(-1); |
|
|
|
|
|
//alle werte einlesen |
|
Vector werte = readDistinctValues(); |
|
if (datenart.toUpperCase().equalsIgnoreCase("DOUBLE")) { |
|
//massiveRandomizeDouble(werte); |
|
randomize(werte); |
|
} else //integer |
|
{ |
|
massiveRandomizeInteger(werte); |
|
} |
|
|
|
System.out.println("Randomizierung erfolgreich beendet"); |
|
} catch (Exception e) { |
|
System.out.println(e.toString()); |
|
System.exit(0); |
|
} |
|
|
|
} |
|
/** |
|
* randomizes in categories of values |
|
* only for double |
|
* @param werte |
|
*/ |
|
private static void randomize(Vector werte) { |
|
int categories = 15; |
|
int valuesPerCategory = (int) werte.size() / categories; |
|
StringBuffer updates = new StringBuffer(); |
|
float limit = 0, limit2; |
|
for (int i = 1; i < categories; i++) { |
|
limit = |
|
(float) ((Double) werte.get(i * valuesPerCategory)) |
|
.doubleValue(); |
|
updates.append( |
|
"update " |
|
+ tablename |
|
+ " set " |
|
+ colname |
|
+ "=" |
|
+ colname |
|
+ "*" |
|
+ getRndFactor() |
|
+ " where " |
|
+ colname |
|
+ ">" |
|
+ limit); |
|
if (i > 1) { |
|
limit2 = |
|
(float) ((Double) werte.get((i - 1) * valuesPerCategory)) |
|
.doubleValue(); |
|
updates.append(" and " + colname + "<" + limit2); |
|
} |
|
updates.append(";\n"); |
|
} |
|
limit = |
|
(float) ((Double) werte.get((categories - 1) * valuesPerCategory)) |
|
.doubleValue(); |
|
updates.append( |
|
"update " |
|
+ tablename |
|
+ " set " |
|
+ colname |
|
+ "=" |
|
+ colname |
|
+ "*" |
|
+ getRndFactor() |
|
+ " where " |
|
+ colname |
|
+ "<" |
|
+ limit |
|
+ ";"); |
|
|
|
System.out.println(updates); |
|
} |
|
/** |
|
* * multiplies all occurences of a single value (e.g. 150) with a factor between |
|
* minFactor and maxFactor (e.g. 150*1.1 -> all entries with 150 become 165) |
|
|
|
* @param werte |
|
* @throws SQLException |
|
*/ |
|
private static void massiveRandomizeInteger(Vector werte) |
|
throws SQLException { |
|
Integer einWert; |
|
String upd = null; |
|
for (Enumeration en = werte.elements(); en.hasMoreElements();) { |
|
einWert = (Integer) en.nextElement(); |
|
int neuerwert = (int) (einWert.intValue() * getRndFactor()); |
|
upd = |
|
"update " |
|
+ tablename |
|
+ " set " |
|
+ colname |
|
+ "=" |
|
+ neuerwert |
|
+ " where " |
|
+ colname |
|
+ "=" |
|
+ einWert.toString() |
|
+ ";"; |
|
|
|
if (printOutOnly) |
|
System.out.println(upd); |
|
else |
|
stat.executeUpdate(upd); |
|
} |
|
|
|
} |
|
/** |
|
* * multiplies all occurences of a single value (e.g. 150) with a factor between |
|
* minFactor and maxFactor (e.g. 150*1.1 -> all entries with 150 become 165) |
|
|
|
* @param werte |
|
* @throws SQLException |
|
*/ |
|
private static void massiveRandomizeDouble(Vector werte) |
|
throws SQLException { |
|
Double einWert; |
|
double zufallswert; |
|
String upd; |
|
int i = 0; |
|
int count = werte.size(); |
|
for (Enumeration en = werte.elements(); en.hasMoreElements();) { |
|
einWert = (Double) en.nextElement(); |
|
i++; |
|
zufallswert = einWert.doubleValue() * getRndFactor(); |
|
upd = |
|
"update " |
|
+ tablename |
|
+ " set " |
|
+ colname |
|
+ "=" |
|
+ zufallswert |
|
+ " where " |
|
+ colname |
|
+ "=" |
|
+ einWert |
|
+ ";"; |
|
if (printOutOnly) |
|
System.out.println(upd); |
|
else { |
|
stat.executeUpdate(upd); |
|
if (i % 20 == 0) |
|
System.out.println("Done " + i + " updates of " + count); |
|
} |
|
|
|
} |
|
} |
|
private static float getRndFactor() { |
|
double rnd = Math.abs(r.nextDouble()); |
|
while (rnd < minFactor) |
|
rnd += minFactor; |
|
while (rnd > maxFactor) |
|
rnd -= 0.05; |
|
return (float) rnd; |
|
} |
|
private static Vector readDistinctValues() throws SQLException { |
|
Vector werte = new Vector(); |
|
ResultSet rs = null; |
|
Object item; |
|
System.out.println( |
|
"Alle werte einlesen:" |
|
+ "select distinct " |
|
+ colname |
|
+ " from " |
|
+ tablename |
|
+ " order by 1 DESC"); |
|
|
|
rs = |
|
stat.executeQuery( |
|
"select distinct " |
|
+ colname |
|
+ " from " |
|
+ tablename |
|
+ " order by " |
|
+ colname |
|
+ " DESC"); |
|
while (rs.next()) { |
|
item = rs.getObject(1); |
|
werte.add(item); |
|
} |
|
rs.close(); |
|
return werte; |
|
} |
|
|
|
private static void out() { |
|
try { |
|
FileWriter fw = |
|
new FileWriter("c:\\cygwin\\home\\superx\\cob_busa.unl"); |
|
BufferedWriter bw = new BufferedWriter(fw); |
|
StringBuffer buf = new StringBuffer(); |
|
|
|
ResultSet rs = stat.executeQuery("select * from cob_busa"); |
|
|
|
int cols = 20;String s;Object o; |
|
while (rs.next()) { |
|
for (int i = 1; i <= cols; i++) |
|
{o=rs.getObject(i); |
|
if (o==null) s=""; |
|
else s=o.toString(); |
|
|
|
buf.append(s.trim() + "^");} |
|
buf.append("^\n"); |
|
|
|
} |
|
rs.close(); |
|
|
|
bw.write(buf.toString()); |
|
bw.close(); |
|
fw.close(); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
System.out.println("done"); |
|
} |
|
}
|
|
|