My 1billion row challenge solutions in various languages

Usage of try-with-resources pom file cleanup

Changed files
+21 -22
src
main
java
dev
morling
+14 -10
pom.xml
···
<version>1.0.0-SNAPSHOT</version>
<properties>
-
<java.version>21</java.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
-
<maven.compiler.release>${java.version}</maven.compiler.release>
+
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
···
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
-
<version>3.8.1</version>
+
<version>3.12.1</version>
<configuration>
+
<enablePreview>true</enablePreview>
<parameters>true</parameters>
<compilerArgs>
-
<compilerArg>--enable-preview</compilerArg>
<compilerArg>--add-modules</compilerArg>
<compilerArg>java.base,jdk.incubator.vector</compilerArg>
</compilerArgs>
···
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
-
<version>3.0.0-M1</version>
+
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
-
<version>3.0.0-M3</version>
+
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
-
<version>3.0.0-M1</version>
+
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
···
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
-
<version>3.9.1</version>
+
<version>3.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
-
<version>3.0.0-M5</version>
+
<version>3.2.3</version>
+
</plugin>
+
<plugin>
+
<groupId>org.apache.maven.plugins</groupId>
+
<artifactId>maven-wrapper-plugin</artifactId>
+
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
···
<configuration>
<rules>
<requireJavaVersion>
-
<version>[${java.version},)</version>
+
<version>${maven.compiler.release}</version>
</requireJavaVersion>
<requirePluginVersions>
<banLatest>true</banLatest>
+7 -12
src/main/java/dev/morling/onebrc/CreateMeasurements.java
···
package dev.morling.onebrc;
import java.io.BufferedWriter;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.OutputStreamWriter;
-
import java.util.Arrays;
+
import java.nio.file.Files;
+
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class CreateMeasurements {
-
private static final String FILE = "./measurements.txt";
+
private static final Path MEASUREMENT_FILE = Path.of("./measurements.txt");
private record WeatherStation(String id, double meanTemperature) {
double measurement() {
···
// )
// ) TO 'output.csv' (HEADER, DELIMITER ',');
// @formatter:on
-
List<WeatherStation> stations = Arrays.asList(
+
List<WeatherStation> stations = List.of(
new WeatherStation("Abha", 18.0),
new WeatherStation("Abidjan", 26.0),
new WeatherStation("Abéché", 29.4),
···
new WeatherStation("Zanzibar City", 26.0),
new WeatherStation("Zürich", 9.3));
-
File measurements = new File(FILE);
-
try (FileOutputStream fos = new FileOutputStream(measurements); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
+
try (BufferedWriter bw = Files.newBufferedWriter(MEASUREMENT_FILE)) {
for (int i = 0; i < size; i++) {
if (i > 0 && i % 50_000_000 == 0) {
-
System.out.println("Wrote %,d measurements in %s ms".formatted(i, System.currentTimeMillis() - start));
+
System.out.printf("Wrote %,d measurements in %s ms%n", i, System.currentTimeMillis() - start);
}
WeatherStation station = stations.get(ThreadLocalRandom.current().nextInt(stations.size()));
bw.write(station.id());
bw.write(";" + station.measurement());
bw.newLine();
}
-
bw.flush();
-
-
System.out.println("Created file with %,d measurements in %s ms".formatted(size, System.currentTimeMillis() - start));
}
+
System.out.printf("Created file with %,d measurements in %s ms%n", size, System.currentTimeMillis() - start);
}
}