我想在Spring Boot中使用JHDF5创建h5文件

问题描述 投票:0回答:1

如果我创建并使用来自屏幕的 json 数据作为文件,Python 会将其读取为字符串,因此由于将数据转换为数字的速度问题,我尝试创建逻辑来创建 h5 文件。

我想创建一个名为 HDF5FileUtil 的通用类来读取或创建 h5 文件。

首先,这是我创建的一个公共类。

@Component
public class HDF5FileUtil {

    public void crateFile(Map<String, Object> data, String filePath) throws Exception {
        IHDF5Writer writer = null;
        try {
            writer = HDF5Factory.open(filePath);
            writeData(writer, "/", data);
        } catch (Exception e) {
            throw e;
        } finally {
            if (writer != null)
                writer.close();
        }
    }
    
    private void writeData(IHDF5Writer writer, String path, Object data) throws Exception {
        try {
            if(data instanceof Map) {
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) data).entrySet()) {
                    writeData(writer, path + entry.getKey() + "/", entry.getValue());
                }
            } else if(data instanceof List) {
                // How should I code this place?
            } else if (data instanceof Integer) {
                writer.writeInt(path, (Integer) data);
            } else if (data instanceof Double) {
                writer.writeDouble(path, (Double) data);
            } else if (data instanceof String) {
                writer.writeString(path, (String) data);
            }
        } catch (Exception e) {
            throw e;
        }
    }
}

假设您收到这样的 json 数据。

{
    "key1": 1,
    "key2": {
        "key2_1": [1,2,3,4,5],
        "ket2_2": "Test"
    },
    "key3": [1.5,3.14,1.23456],
    "key4": [
        {
            "key4_1":0, 
            "key4_2": "00",
             "key4_3":[
                [0,0],
                [0,1.238],
                [0.887,3]
            ]
        },{
            "key4_1":1, 
            "key4_2": "02", 
            "key4_3":[
                [3,1],
                [0,1.238],
                [0.887,3]
            ]
        }
    ]
}

无论我怎么想,我都不知道如何处理

data instanceof List
条件下的逻辑。

我问这个问题是因为我想知道如何使其变得通用和可用,因为稍后会出现一种情况,即

Map
位于
List
内,
Map
内。

json spring spring-boot hdf5
1个回答
0
投票

哇,我想说在 writeData 方法中处理 List 情况可能有点具有挑战性,特别是在处理嵌套结构(例如另一个 Map 内的 List 内的 Map)时,嗯。关键是适当地递归处理每种数据类型。以下是如何修改 writeData 方法来处理 List 实例

import ch.systemsx.cisd.hdf5.IHDF5Writer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class HDF5FileUtil {

    public void createFile(Map<String, Object> data, String filePath) throws Exception {
        IHDF5Writer writer = null;
        try {
            writer = HDF5Factory.open(filePath);
            writeData(writer, "/", data);
        } catch (Exception e) {
            throw e;
        } finally {
            if (writer != null)
                writer.close();
        }
    }
    
    private void writeData(IHDF5Writer writer, String path, Object data) throws Exception {
        try {
            if (data instanceof Map) {
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) data).entrySet()) {
                    writeData(writer, path + entry.getKey() + "/", entry.getValue());
                }
            } else if (data instanceof List) {
                handleList(writer, path, (List<?>) data);
            } else if (data instanceof Integer) {
                writer.writeInt(path, (Integer) data);
            } else if (data instanceof Double) {
                writer.writeDouble(path, (Double) data);
            } else if (data instanceof String) {
                writer.writeString(path, (String) data);
            }
            // handle other data types if necessary
        } catch (Exception e) {
            throw e;
        }
    }

    private void handleList(IHDF5Writer writer, String path, List<?> list) throws Exception {
        int index = 0;
        for (Object item : list) {
            writeData(writer, path + index + "/", item);
            index++;
        }
    }
}


© www.soinside.com 2019 - 2024. All rights reserved.