Search in sources :

Example 1 with JsonMetadataParser

use of org.apache.hop.metadata.serializer.json.JsonMetadataParser in project hop by apache.

the class SerializableMetadataProvider method toJson.

public String toJson() throws HopException {
    JSONObject jStore = new JSONObject();
    // 
    for (Class<IHopMetadata> metadataClass : getMetadataClasses()) {
        IHopMetadataSerializer<IHopMetadata> serializer = getSerializer(metadataClass);
        HopMetadata hopMetadata = metadataClass.getAnnotation(HopMetadata.class);
        if (hopMetadata == null) {
            throw new HopException("Error: class " + metadataClass + " is not annotated with " + HopMetadata.class.getName());
        }
        String classKey = hopMetadata.key();
        JSONArray jClass = new JSONArray();
        JsonMetadataParser parser = new JsonMetadataParser(metadataClass, this);
        // 
        for (String name : serializer.listObjectNames()) {
            Object object = serializer.load(name);
            JSONObject jObject = parser.getJsonObject((IHopMetadata) object);
            jClass.add(jObject);
        }
        jStore.put(classKey, jClass);
    }
    return jStore.toJSONString();
}
Also used : JSONObject(org.json.simple.JSONObject) JsonMetadataParser(org.apache.hop.metadata.serializer.json.JsonMetadataParser) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) HopException(org.apache.hop.core.exception.HopException) JSONArray(org.json.simple.JSONArray) HopMetadata(org.apache.hop.metadata.api.HopMetadata) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) JSONObject(org.json.simple.JSONObject)

Example 2 with JsonMetadataParser

use of org.apache.hop.metadata.serializer.json.JsonMetadataParser in project hop by apache.

the class MetadataInput method processRow.

@Override
public boolean processRow() throws HopException {
    IRowMeta outputRowMeta = new RowMeta();
    meta.getFields(outputRowMeta, getTransformName(), null, null, this, metadataProvider);
    // Get the list of type key filters...
    // 
    List<String> typeKeyFilters = new ArrayList<>();
    for (String typeKeyFilter : meta.getTypeKeyFilters()) {
        typeKeyFilters.add(StringUtils.trim(resolve(typeKeyFilter)));
    }
    // Loop over the metadata classes (types)
    List<Class<IHopMetadata>> metadataClasses = metadataProvider.getMetadataClasses();
    for (Class<IHopMetadata> metadataClass : metadataClasses) {
        IHopMetadataSerializer<IHopMetadata> serializer = metadataProvider.getSerializer(metadataClass);
        HopMetadata annotation = HopMetadataUtil.getHopMetadataAnnotation(metadataClass);
        // See if we need to include this class in the output...
        // 
        boolean include = typeKeyFilters.isEmpty();
        for (String typeKeyFilter : typeKeyFilters) {
            if (typeKeyFilter.equals(annotation.key())) {
                include = true;
            }
        }
        if (!include) {
            // Skip this one
            continue;
        }
        JsonMetadataParser<IHopMetadata> parser = new JsonMetadataParser<>(metadataClass, metadataProvider);
        // 
        for (String name : serializer.listObjectNames()) {
            IHopMetadata metadata = serializer.load(name);
            // Output the metadata...
            // 
            Object[] outputRow = RowDataUtil.allocateRowData(outputRowMeta.size());
            int index = 0;
            // 
            if (StringUtils.isNotEmpty(meta.getProviderFieldName())) {
                outputRow[index++] = metadata.getMetadataProviderName();
            }
            // 
            if (StringUtils.isNotEmpty(meta.getTypeKeyFieldName())) {
                outputRow[index++] = annotation.key();
            }
            if (StringUtils.isNotEmpty(meta.getTypeDescriptionFieldName())) {
                outputRow[index++] = annotation.name();
            }
            if (StringUtils.isNotEmpty(meta.getTypeDescriptionFieldName())) {
                outputRow[index++] = annotation.description();
            }
            if (StringUtils.isNotEmpty(meta.getTypeClassFieldName())) {
                outputRow[index++] = metadataClass.getName();
            }
            // Some information about the metadata object...
            if (StringUtils.isNotEmpty(meta.getNameFieldName())) {
                outputRow[index++] = metadata.getName();
            }
            // 
            if (StringUtils.isNotEmpty(meta.getJsonFieldName())) {
                JSONObject jsonObject = parser.getJsonObject(metadata);
                outputRow[index] = jsonObject.toJSONString();
            }
            putRow(outputRowMeta, outputRow);
        }
    }
    setOutputDone();
    return false;
}
Also used : IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) IRowMeta(org.apache.hop.core.row.IRowMeta) ArrayList(java.util.ArrayList) JsonMetadataParser(org.apache.hop.metadata.serializer.json.JsonMetadataParser) JSONObject(org.json.simple.JSONObject) HopMetadata(org.apache.hop.metadata.api.HopMetadata) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) JSONObject(org.json.simple.JSONObject)

Aggregations

HopMetadata (org.apache.hop.metadata.api.HopMetadata)2 IHopMetadata (org.apache.hop.metadata.api.IHopMetadata)2 JsonMetadataParser (org.apache.hop.metadata.serializer.json.JsonMetadataParser)2 JSONObject (org.json.simple.JSONObject)2 ArrayList (java.util.ArrayList)1 HopException (org.apache.hop.core.exception.HopException)1 IRowMeta (org.apache.hop.core.row.IRowMeta)1 RowMeta (org.apache.hop.core.row.RowMeta)1 JSONArray (org.json.simple.JSONArray)1