Search in sources :

Example 1 with HdfException

use of io.jhdf.exceptions.HdfException in project drill by apache.

the class HDF5BatchReader method getAttributes.

/**
 * Gets the attributes of a HDF5 dataset and returns them into a HashMap
 *
 * @param path The path for which you wish to retrieve attributes
 * @return Map The attributes for the given path.  Empty Map if no attributes present
 */
private Map<String, HDF5Attribute> getAttributes(String path) {
    Map<String, Attribute> attributeList;
    // Remove trailing slashes
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    logger.debug("Getting attributes for {}", path);
    Map<String, HDF5Attribute> attributes = new HashMap<>();
    Node theNode;
    try {
        theNode = hdfFile.getByPath(path);
    } catch (Exception e) {
        // Couldn't find node
        logger.debug("Couldn't get attributes for path: {}", path);
        logger.debug("Error: {}", e.getMessage());
        return attributes;
    }
    try {
        attributeList = theNode.getAttributes();
    } catch (HdfException e) {
        logger.warn("Unable to get attributes for {}: Only Huge objects BTrees with 1 record are currently supported.", path);
        return attributes;
    }
    logger.debug("Found {} attribtutes for {}", attributeList.size(), path);
    for (Map.Entry<String, Attribute> attributeEntry : attributeList.entrySet()) {
        HDF5Attribute attribute = HDF5Utils.getAttribute(path, attributeEntry.getKey(), hdfFile);
        // Ignore compound attributes.
        if (attribute != null && attributeEntry.getValue().isScalar()) {
            logger.debug("Adding {} to attribute list for {}", attribute.getKey(), path);
            attributes.put(attribute.getKey(), attribute);
        }
    }
    return attributes;
}
Also used : HdfException(io.jhdf.exceptions.HdfException) Attribute(io.jhdf.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(io.jhdf.api.Node) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UserException(org.apache.drill.common.exceptions.UserException) HdfException(io.jhdf.exceptions.HdfException) IOException(java.io.IOException)

Example 2 with HdfException

use of io.jhdf.exceptions.HdfException in project drill by apache.

the class HDF5BatchReader method getFileMetadata.

/**
 * Gets the file metadata from a given HDF5 file. It will extract the file
 * name the path, and adds any information to the metadata List.
 *
 * @param group A list of paths from which the metadata will be extracted
 * @param metadata The HDF5 metadata object from which the metadata will be extracted
 * @return A list of metadata from the given file paths
 */
private List<HDF5DrillMetadata> getFileMetadata(Group group, List<HDF5DrillMetadata> metadata) {
    Map<String, HDF5Attribute> attribs;
    // that the group can be read.
    try {
        group.getChildren();
    } catch (HdfException e) {
        logger.warn(e.getMessage());
        return metadata;
    }
    for (Node node : group) {
        HDF5DrillMetadata metadataRow = new HDF5DrillMetadata();
        if (node.isLink()) {
            SoftLink link = (SoftLink) node;
            metadataRow.setPath(link.getTargetPath());
            metadataRow.setLink(true);
        } else {
            metadataRow.setPath(node.getPath());
            metadataRow.setDataType(node.getType().name());
            metadataRow.setLink(false);
            switch(node.getType()) {
                case DATASET:
                    attribs = getAttributes(node.getPath());
                    metadataRow.setAttributes(attribs);
                    metadata.add(metadataRow);
                    break;
                case GROUP:
                    attribs = getAttributes(node.getPath());
                    metadataRow.setAttributes(attribs);
                    metadata.add(metadataRow);
                    getFileMetadata((Group) node, metadata);
                    break;
                default:
                    logger.warn("Unknown data type: {}", node.getType());
            }
        }
    }
    return metadata;
}
Also used : HdfException(io.jhdf.exceptions.HdfException) Node(io.jhdf.api.Node) SoftLink(io.jhdf.links.SoftLink)

Aggregations

Node (io.jhdf.api.Node)2 HdfException (io.jhdf.exceptions.HdfException)2 Attribute (io.jhdf.api.Attribute)1 SoftLink (io.jhdf.links.SoftLink)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 UserException (org.apache.drill.common.exceptions.UserException)1