databuild/examples/basic_graph/SumExecute.java
2025-04-17 17:27:55 -07:00

33 lines
1.4 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);
}
// 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 = BASE_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
try (java.io.FileWriter writer = new java.io.FileWriter(BASE_PATH + args[0])) {
writer.write(sum);
} catch (Exception e) {
System.err.println("Error: Failed to write sum to " + args[0] + ": " + e.getMessage());
}
}
}