Hdf5JavaLib is a pure Java library for reading HDF5 files, released as version 0.1.1. This guide helps users read datasets in the root group using examples from the org.hdf5javalib.examples.read
package. The library reads HDF5 files generated by the C++ HDF5 library, supporting compound datasets, multi-dimensional data, and various datatypes.
Add Hdf5JavaLib to your project via Maven Central:
<dependency>
<groupId>org.hdf5javalib</groupId>
<artifactId>hdf5javalib</artifactId>
<version>0.1.1</version>
</dependency>
git clone https://github.com/karlnicholas/Hdf5JavaLib.git
cd Hdf5JavaLib
mvn install
compound_example.h5
, twenty_datasets.h5
, ascii_dataset.h5
) are used in the examples below.src/test/resources
.ResourceLoader
or a SeekableByteChannel
.Hdf5JavaLib supports reading datasets in the root group (/
), including:
CompoundData
with recordId
, fixedStr
).BigInteger
, BigDecimal
), floating-point (Float
, Double
), strings (ASCII, UTF-8), time, bitfields, compounds, opaque, references, enums, arrays.The library supports sequential and parallel streaming for efficient data processing.
Use the HdfFileReader
class (in org.hdf5javalib.file
) to read HDF5 files. The TypedDataSource
class provides typed data access (scalar, vector, matrix) and streaming, while HdfDisplayUtils
simplifies data display. The org.hdf5javalib.examples.read
package shows how to read different datasets.
This example reads a compound dataset (CompoundData
) from an HDF5 file, with fields like recordId
(Long) and fixedStr
(String), and maps it to a custom Java class.
package org.hdf5javalib.examples.read;
import org.hdf5javalib.HdfFileReader;
import org.hdf5javalib.dataclass.HdfCompound;
import org.hdf5javalib.datasource.TypedDataSource;
import org.hdf5javalib.examples.ResourceLoader;
import org.hdf5javalib.file.HdfDataSet;
import java.nio.channels.SeekableByteChannel;
public class HdfCompoundRead {
public static void main(String[] args) throws Exception {
try (SeekableByteChannel channel = ResourceLoader.loadResourceAsChannel("compound_example.h5")) {
HdfFileReader reader = new HdfFileReader(channel).readFile();
try (HdfDataSet dataSet = reader.getRootGroup().findDataset("CompoundData")) {
new TypedDataSource<>(channel, reader, dataSet, HdfCompound.class)
.streamVector()
.limit(2)
.forEach(c -> System.out.println("Row: " + c.getMembers()));
}
}
}
}
Output (example):
Row: [recordId=1000, fixedStr="Fixed0", varStr="Variable0", ...]
Row: [recordId=1001, fixedStr="Fixed1", varStr="Variable1", ...]
Note: If using your own HDF5 file, replace "compound_example.h5"
with the path to your file relative to the classpath or use a SeekableByteChannel
from a file path.
This example reads scalar datasets from an HDF5 file, displaying values as Long
.
package org.hdf5javalib.examples.read;
import org.hdf5javalib.HdfFileReader;
import org.hdf5javalib.examples.ResourceLoader;
import org.hdf5javalib.file.HdfDataSet;
import org.hdf5javalib.utils.HdfDisplayUtils;
import java.nio.channels.SeekableByteChannel;
public class HdfTwentyScalarRead {
public static void main(String[] args) throws Exception {
try (SeekableByteChannel channel = ResourceLoader.loadResourceAsChannel("twenty_datasets.h5")) {
HdfFileReader reader = new HdfFileReader(channel).readFile();
for (HdfDataSet dataSet : reader.getRootGroup().getDataSets()) {
try (HdfDataSet ds = dataSet) {
HdfDisplayUtils.displayScalarData(channel, ds, Long.class, reader);
}
}
}
}
}
Output (example):
Dataset0: 123
Dataset1: 456
...
This example reads a vector of ASCII strings from an HDF5 file.
package org.hdf5javalib.examples.read;
import org.hdf5javalib.HdfFileReader;
import org.hdf5javalib.examples.ResourceLoader;
import org.hdf5javalib.file.HdfDataSet;
import org.hdf5javalib.utils.HdfDisplayUtils;
import java.nio.channels.SeekableByteChannel;
public class HdfStringRead {
public static void main(String[] args) throws Exception {
try (SeekableByteChannel channel = ResourceLoader.loadResourceAsChannel("ascii_dataset.h5")) {
HdfFileReader reader = new HdfFileReader(channel).readFile();
try (HdfDataSet dataSet = reader.getRootGroup().findDataset("strings")) {
HdfDisplayUtils.displayVectorData(channel, dataSet, String.class, reader);
}
}
}
}
Output (example):
["Hello", "World", "HDF5"]
pom.xml
.src/main/resources
(or another classpath-accessible location).mvn compile
java -cp target/classes org.hdf5javalib.examples.read.HdfCompoundRead
src/test/resources
.mvn test
mvn compile
java -cp target/classes org.hdf5javalib.examples.read.HdfCompoundRead
ResourceLoader
or create a SeekableByteChannel
from a file path./
).Help improve Hdf5JavaLib by reporting issues at GitHub Issues. Please include:
Visit https://www.hdf5javalib.org for updates and resources.