70 lines
No EOL
2.6 KiB
Java
70 lines
No EOL
2.6 KiB
Java
package com.databuild.examples.basic_graph;
|
|
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* Execute class for generating a random number.
|
|
* This class generates a random number based on the partition ref.
|
|
*/
|
|
public class GenerateExecute {
|
|
public static String BASE_PATH = "/tmp/databuild/examples/basic_graph/";
|
|
|
|
public static void main(String[] args) {
|
|
if (args.length < 1) {
|
|
System.err.println("Error: Partition ref (output path) is required");
|
|
System.exit(1);
|
|
}
|
|
|
|
String partitionRef = args[0];
|
|
|
|
try {
|
|
// Create a hash of the partition ref to use as a seed
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
byte[] hashBytes = md.digest(partitionRef.getBytes(StandardCharsets.UTF_8));
|
|
|
|
// Convert the first 8 bytes of the hash to a long to use as a seed
|
|
long seed = 0;
|
|
for (int i = 0; i < Math.min(8, hashBytes.length); i++) {
|
|
seed = (seed << 8) | (hashBytes[i] & 0xff);
|
|
}
|
|
|
|
// Create a random number generator with the seed
|
|
Random random = new Random(seed);
|
|
|
|
// Generate a random number
|
|
int randomNumber = random.nextInt(100);
|
|
|
|
// Write the random number to the output file
|
|
// Ensure dir exists
|
|
java.io.File parent = new java.io.File(partitionRef).getParentFile();
|
|
if (parent != null) {
|
|
parent.mkdirs();
|
|
}
|
|
try (FileWriter writer = new FileWriter(partitionRef)) {
|
|
writer.write("Random number for partition " + partitionRef + ": " + randomNumber);
|
|
}
|
|
|
|
System.out.println("Generated random number " + randomNumber + " for partition " + partitionRef);
|
|
|
|
// Write the random number to the output file
|
|
String outputPath = partitionRef;
|
|
System.out.println("Writing random number " + randomNumber + " to " + outputPath);
|
|
// Ensure dir exists
|
|
new java.io.File(outputPath).getParentFile().mkdirs();
|
|
// Write number (overwrite)
|
|
try (FileWriter writer = new FileWriter(outputPath)) {
|
|
writer.write(String.valueOf(randomNumber));
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException | IOException e) {
|
|
System.err.println("Error: " + e.getMessage());
|
|
e.printStackTrace();
|
|
System.exit(1);
|
|
}
|
|
}
|
|
} |