I want to create an h5 file using JHDF5 in Spring Boot

140 Views Asked by At

If I create and use the json data coming from the screen as a file, Python reads it as a string, so I am trying to create logic to create an h5 file due to speed issues with converting the data to numbers.

I would like to create a common class called HDF5FileUtil to read or create h5 files.

First, this is a common class I created.

@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;
        }
    }
}

Let's assume you receive json data like this.

{
    "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]
            ]
        }
    ]
}

No matter how much I think about it, I don't know what to do with the logic in the data instanceof List condition.

I'm asking because I want to know how to make it common and usable because later on, a situation will arise where a Map is inside a List inside a Map.

1

There are 1 best solutions below

1
Hamish On

Handling nested data structures such as a Map inside a List inside a Map can be complex. To handle this in a common and reusable way, you can create a recursive function that can handle nested structures. This function would iterate through the data structure and handle each element based on its type (Map, List, Integer, Double, String, etc.).

For the case of a List, you can iterate through the list and recursively call the function to handle each element within the list. This way, you can handle nested structures with a common and reusable approach. a simplified code would be like this:

public class HDF5FileUtil {

// ... (other methods)

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) {
            handleListData(writer, path, (List<Object>) 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);
        }
    } catch (Exception e) {
        throw e;
    }
}

private void handleListData(IHDF5Writer writer, String path, List<Object> listData) throws Exception {
    for (int i = 0; i < listData.size(); i++) {
        Object element = listData.get(i);
        if (element instanceof Map || element instanceof List) {
            writeData(writer, path + i + "/", element); // Handle nested structure recursively
        } else if (element instanceof Integer) {
            writer.writeInt(path + i, (Integer) element);
        } else if (element instanceof Double) {
            writer.writeDouble(path + i, (Double) element);
        } else if (element instanceof String) {
            writer.writeString(path + i, (String) element);
        }
    }
}
}