HDF5相关网址:
hdf5-java java语言API:
https://portal.hdfgroup.org/display/support/HDF-Java
hdf5 官网:
https://portal.hdfgroup.org/display/HDF5
java-hdf5相关包下载(环境所需资源):
链接:https://pan.baidu.com/s/1EwIvnTjyZY6JlhmimNU54Q
提取码:6z27
什么是HDF5:
HDF5是一种独特的技术套件,可以管理极其庞大和复杂的数据集。
HDF5技术套件包括:
- 一种多功能数据类型,可以表示非常复杂的数据对象和各种元数据
- 一种完全可移植的文件格式,对集合中数据对象的数量和大小没有限制
- 一个软件库,运行在一系列计算平台上,从笔记本电脑到大规模并行系统,并使用C, C++, Fortran 90和Java接口实现高级API
- 丰富的集成性功能,可实现访问空间和存储空间优化
- 用于管理,操作,查看和分析集合中数据的工具和应用程序
(HDF5数据模型,文件格式,API,库和工具是免费开放和分发的)
HDF5-Java读写数据集:
import hdf.hdf5lib.H5;
import hdf.hdf5lib.HDF5Constants;
public class H5_Read_Write {
private static String FILENAME= "G:/hdf5_test/H5_ReadWrite.h5"; //写出.h5文件名
private static String DATASETNAME= "dset";//定义数据集名称
private static final int DIM_X= 4;//横轴大小,数据集的行数
private static final int DIM_Y= 8;//纵轴大小,数据集的列数
private static final int RANK= 2;//数据集的维度
public static void main(String[] args) {
H5_Read_Write.WriteDataset();
H5_Read_Write.ReadDataset();
}
private static void WriteDataset() {
int file_id = -1;
int filespace_id = -1;
int dataset_id = -1;
long[] dims = { DIM_X, DIM_Y};
int[][] dset_data = new int[DIM_X][DIM_Y];
// Initialize data. 初始化数据
for (int indx = 0; indx < DIM_X; indx++)
for (int jndx = 0; jndx < DIM_Y; jndx++)
dset_data[indx][jndx] = indx*jndx-jndx;
try {//创建文件,指定文件名,文件存取模式,文件创建特性列表,文件存取特性列表 返回文件id(文件的唯一性标识)
file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);//文件创建成功之后会返回一个整数,该数为文件的id值
} catch(Exception e) {
e.printStackTrace();
}
try {//H5Screate_simple 新建一个数据空间并打开 等待接收数据
filespace_id = H5.H5Screate_simple(RANK, dims, null);//返回数据空间的id值
} catch(Exception e) {
e.printStackTrace();
}
try {
if((file_id >= 0) && (filespace_id >= 0)) //如果文件和数据空间存在,则创建数据集
//根据存在文件的id和数据空间的id (H5Dcreate)新建一个数据集 并返回数据集id
dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, filespace_id,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
} catch(Exception e) {
e.printStackTrace();
}
try {
if(dataset_id >= 0) //根据数据集id判断数据集是否存在,若存在调用 H5.H5Dwrite将dset_data中的数据对象写入文件中
H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT,HDF5Constants.H5S_ALL,
HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, dset_data);
} catch(Exception e) {
e.printStackTrace();
}
try {
if(dataset_id >= 0)H5.H5Dclose(dataset_id);
//关闭数据集
} catch(Exception e) {
e.printStackTrace();
}
try {
if(filespace_id >= 0)
H5.H5Sclose(filespace_id); //释放数据空间
} catch(Exception e) {
e.printStackTrace();
}
// Close the file.
try {
if(file_id >= 0)
H5.H5Fclose(file_id);//关闭文件,写操作结束
} catch(Exception e) {
e.printStackTrace();
}
}
private static void ReadDataset() {
int file_id = -1;
int dataset_id = -1;
int[][] dset_data = new int[DIM_X][DIM_Y];//定义一个数据接受数组
// Open file using the default properties.
try {//H5.H5Fopen打开一个已存在的文件 根据文件名称
file_id =H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT); //成功打开之后返回文件id
System.out.println("===================="+file_id);
} catch(Exception e) {
e.printStackTrace();
}
try {if(file_id >= 0) //H5.H5Dopen打开数据集(根据文件id和数据集名称)
dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
} catch(Exception e) {
e.printStackTrace();
}
try {if(dataset_id >= 0) //H5Dread读数据到dset_data中
H5.H5Dread(dataset_id,HDF5Constants.H5T_NATIVE_INT,HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
HDF5Constants.H5P_DEFAULT, dset_data);
} catch(Exception e) {
e.printStackTrace();
}
//实现数据的打印输出
System.out.println(DATASETNAME+ ":");
for (int indx = 0; indx < DIM_X; indx++) {
System.out.print(" [ ");
for (int jndx = 0; jndx < DIM_Y; jndx++)
System.out.print(dset_data[indx][jndx] + " ");
System.out.println("]");
}
System.out.println();
// Close the dataset.
try {
if(dataset_id >= 0)
H5.H5Dclose(dataset_id);
} catch(Exception e) {
e.printStackTrace();
}
// Close the file
try {
if(file_id >= 0)
H5.H5Fclose(file_id);
} catch(Exception e) {
e.printStackTrace();
}
}
}
HDF5-java 创建并写入数据到文件-组-数据集:
import hdf.hdf5lib.H5;
import hdf.hdf5lib.HDF5Constants;
public class H5_CreateGroupDataset {
private static String FILENAME = "G:/hdf5_test/H5_CreateGroupDataset.h5";
private static String GROUPNAME = "MyGroup";
private static String GROUPNAME_A = "GroupA";
private static String DATASETNAME1 = "dset1";
private static String DATASETNAME2 = "dset2";
private static final int DIM1_X = 3;
private static final int DIM1_Y = 3;
private static final int DIM2_X = 2;
private static final int DIM2_Y = 10;
private static void h5_crtgrpd() {
int file_id = -1;
int dataspace_id = -1;
int dataset_id = -1;
int group_id = -1;
int group1_id = -1;
int group2_id = -1;
int[][] dset1_data = new int[DIM1_X][DIM1_Y];
int[][] dset2_data = new int[DIM2_X][DIM2_Y];
long[] dims1 = { DIM1_X, DIM1_Y };
long[] dims2 = { DIM2_X, DIM2_Y };
// Initialize the first dataset.
for (int indx = 0; indx < DIM1_X; indx++)
for (int jndx = 0; jndx < DIM1_Y; jndx++)
dset1_data[indx][jndx] = jndx + 1;
// Initialize the second dataset.
for (int indx = 0; indx < DIM2_X; indx++)
for (int jndx = 0; jndx < DIM2_Y; jndx++)
dset2_data[indx][jndx] = jndx + 1;
// Create a file.
try {
file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT);
// Create a group named "/MyGroup" in the file.
if (file_id >= 0) {//创建组的时候需要添加“/”
group1_id = H5.H5Gcreate(file_id, "/" + GROUPNAME, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
// Create group "Group_A" in group "MyGroup" using absolute name.
if (group1_id >= 0) {
group2_id = H5.H5Gcreate(file_id, "/" + GROUPNAME + "/" + GROUPNAME_A, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
if (group2_id >= 0)
H5.H5Gclose(group2_id);
}
if (group1_id >= 0)
H5.H5Gclose(group1_id);
}
}
catch (Exception e) {
e.printStackTrace();
}
// Create the data space for the first dataset.
try {
dataspace_id = H5.H5Screate_simple(2, dims1, null);
}
catch (Exception e) {
e.printStackTrace();
}
// Create the dataset in group "MyGroup".
try {
if ((file_id >= 0) && (dataspace_id >= 0))//根据绝对路径创建数据集
dataset_id = H5.H5Dcreate(file_id, "/" + GROUPNAME + "/" + DATASETNAME1, HDF5Constants.H5T_STD_I32BE,
dataspace_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
// Write the first dataset.
try {
if (dataset_id >= 0)
H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
HDF5Constants.H5P_DEFAULT, dset1_data);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the data space for the first dataset.
try {
if (dataspace_id >= 0)
H5.H5Sclose(dataspace_id);
dataspace_id = -1;
}
catch (Exception e) {
e.printStackTrace();
}
// Close the first dataset.
try {
if (dataset_id >= 0)
H5.H5Dclose(dataset_id);
dataset_id = -1;
}
catch (Exception e) {
e.printStackTrace();
}
// Open an existing group of the specified file.
try {
if (file_id >= 0)
group_id = H5.H5Gopen(file_id, "/" + GROUPNAME + "/" + GROUPNAME_A, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
// Create the data space for the second dataset.
try {
dataspace_id = H5.H5Screate_simple(2, dims2, null);
}
catch (Exception e) {
e.printStackTrace();
}
// Create the second dataset in group "Group_A".
try {
if ((group_id >= 0) && (dataspace_id >= 0))
dataset_id = H5.H5Dcreate(group_id, DATASETNAME2, HDF5Constants.H5T_STD_I32BE, dataspace_id,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
// Write the second dataset.
try {
if (dataset_id >= 0)
H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
HDF5Constants.H5P_DEFAULT, dset2_data);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the data space for the second dataset.
try {
if (dataspace_id >= 0)
H5.H5Sclose(dataspace_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the second dataset.
try {
if (dataset_id >= 0)
H5.H5Dclose(dataset_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the group.
try {
if (group_id >= 0)
H5.H5Gclose(group_id);
}
catch (Exception e) {
e.printStackTrace();
}
// Close the file.
try {
if (file_id >= 0)
H5.H5Fclose(file_id);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
H5_CreateGroupDataset.h5_crtgrpd();
}
}
HDF5-Java得到具体数据集的条数:
//读取.h5文件中的行数
public static long readH5LineNum(String FILENAME,String DATASETNAME) {
int file_id = -1;
int dataspace_id = -1;
int dataset_id = -1;
long[] dims = { DIM0 };//DIM0为设置的初始复合数据接收大小
// Open an existing file.
try {
file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
try {
if (file_id >= 0)
dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
}
catch (Exception e) {
e.printStackTrace();
}
// Get dataspace and allocate memory for read buffer.
try {
if (dataset_id >= 0)
dataspace_id = H5.H5Dget_space(dataset_id);
}
catch (Exception e) {
e.printStackTrace();
}
try {
if (dataspace_id >= 0)
H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
}
catch (Exception e) {
e.printStackTrace();
}
try {
if (dataset_id >= 0)
H5.H5Dclose(dataset_id);
} catch (Exception e) {
e.printStackTrace();
}
// Terminate access to the data space.
try {
if (dataspace_id >= 0)
H5.H5Sclose(dataspace_id);
} catch (Exception e){
e.printStackTrace();
}
// Close the file.
try {
if (file_id >= 0)
H5.H5Fclose(file_id);
} catch (Exception e) {
e.printStackTrace();
}
return dims[0];
}