44 lines
1.7 KiB
Java
44 lines
1.7 KiB
Java
package com.databuild.examples.basic_graph;
|
|
|
|
import static com.databuild.examples.basic_graph.GenerateExecute.BASE_PATH;
|
|
|
|
public class SumExecute {
|
|
public static void main(String[] args) {
|
|
if (args.length < 1) {
|
|
System.err.println("Error: Partition ref (output path) is required");
|
|
System.exit(1);
|
|
}
|
|
|
|
// Get output ref from env var OUTPUT_REF
|
|
String outputRef = System.getenv("OUTPUT_REF");
|
|
|
|
// For each arg, load it from the file system and add it to the sum
|
|
int sum = 0;
|
|
for (String partitionRef : args) {
|
|
try {
|
|
String path = partitionRef;
|
|
int partitionValue = Integer.parseInt(new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(path))));
|
|
System.out.println("Summing partition " + partitionRef + " with value " + partitionValue);
|
|
sum += partitionValue;
|
|
} catch (Exception e) {
|
|
System.err.println("Error: Failed to read partition " + partitionRef + ": " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
System.out.println("Sum of " + args.length + " partitions: " + sum);
|
|
// Write the sum to the output file
|
|
String outPath = outputRef;
|
|
System.out.println("Writing sum " + sum + " to " + outPath);
|
|
|
|
java.io.File parent = new java.io.File(outPath).getParentFile();
|
|
if (parent != null) {
|
|
parent.mkdirs();
|
|
}
|
|
|
|
try (java.io.FileWriter writer = new java.io.FileWriter(outPath)) {
|
|
writer.write(String.valueOf(sum));
|
|
} catch (Exception e) {
|
|
System.err.println("Error: Failed to write sum to " + outputRef + ": " + e.getMessage());
|
|
}
|
|
}
|
|
}
|