Search in sources :

Example 1 with PropertyWriter

use of org.apache.solr.handler.dataimport.config.PropertyWriter in project lucene-solr by apache.

the class DataImporter method createPropertyWriter.

@SuppressWarnings("unchecked")
private DIHProperties createPropertyWriter() {
    DIHProperties propWriter = null;
    PropertyWriter configPw = config.getPropertyWriter();
    try {
        Class<DIHProperties> writerClass = DocBuilder.loadClass(configPw.getType(), this.core);
        propWriter = writerClass.newInstance();
        propWriter.init(this, configPw.getParameters());
    } catch (Exception e) {
        throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Unable to PropertyWriter implementation:" + configPw.getType(), e);
    }
    return propWriter;
}
Also used : SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) PropertyWriter(org.apache.solr.handler.dataimport.config.PropertyWriter)

Example 2 with PropertyWriter

use of org.apache.solr.handler.dataimport.config.PropertyWriter in project lucene-solr by apache.

the class DataImporter method readFromXml.

public DIHConfiguration readFromXml(Document xmlDocument) {
    DIHConfiguration config;
    List<Map<String, String>> functions = new ArrayList<>();
    Script script = null;
    Map<String, Map<String, String>> dataSources = new HashMap<>();
    NodeList dataConfigTags = xmlDocument.getElementsByTagName("dataConfig");
    if (dataConfigTags == null || dataConfigTags.getLength() == 0) {
        throw new DataImportHandlerException(SEVERE, "the root node '<dataConfig>' is missing");
    }
    Element e = (Element) dataConfigTags.item(0);
    List<Element> documentTags = ConfigParseUtil.getChildNodes(e, "document");
    if (documentTags.isEmpty()) {
        throw new DataImportHandlerException(SEVERE, "DataImportHandler " + "configuration file must have one <document> node.");
    }
    List<Element> scriptTags = ConfigParseUtil.getChildNodes(e, ConfigNameConstants.SCRIPT);
    if (!scriptTags.isEmpty()) {
        script = new Script(scriptTags.get(0));
    }
    // Add the provided evaluators
    List<Element> functionTags = ConfigParseUtil.getChildNodes(e, ConfigNameConstants.FUNCTION);
    if (!functionTags.isEmpty()) {
        for (Element element : functionTags) {
            String func = ConfigParseUtil.getStringAttribute(element, NAME, null);
            String clz = ConfigParseUtil.getStringAttribute(element, ConfigNameConstants.CLASS, null);
            if (func == null || clz == null) {
                throw new DataImportHandlerException(SEVERE, "<function> must have a 'name' and 'class' attributes");
            } else {
                functions.add(ConfigParseUtil.getAllAttributes(element));
            }
        }
    }
    List<Element> dataSourceTags = ConfigParseUtil.getChildNodes(e, ConfigNameConstants.DATA_SRC);
    if (!dataSourceTags.isEmpty()) {
        for (Element element : dataSourceTags) {
            Map<String, String> p = new HashMap<>();
            HashMap<String, String> attrs = ConfigParseUtil.getAllAttributes(element);
            for (Map.Entry<String, String> entry : attrs.entrySet()) {
                p.put(entry.getKey(), entry.getValue());
            }
            dataSources.put(p.get("name"), p);
        }
    }
    if (dataSources.get(null) == null) {
        for (Map<String, String> properties : dataSources.values()) {
            dataSources.put(null, properties);
            break;
        }
    }
    PropertyWriter pw = null;
    List<Element> propertyWriterTags = ConfigParseUtil.getChildNodes(e, ConfigNameConstants.PROPERTY_WRITER);
    if (propertyWriterTags.isEmpty()) {
        boolean zookeeper = false;
        if (this.core != null && this.core.getCoreContainer().isZooKeeperAware()) {
            zookeeper = true;
        }
        pw = new PropertyWriter(zookeeper ? "ZKPropertiesWriter" : "SimplePropertiesWriter", Collections.<String, String>emptyMap());
    } else if (propertyWriterTags.size() > 1) {
        throw new DataImportHandlerException(SEVERE, "Only one " + ConfigNameConstants.PROPERTY_WRITER + " can be configured.");
    } else {
        Element pwElement = propertyWriterTags.get(0);
        String type = null;
        Map<String, String> params = new HashMap<>();
        for (Map.Entry<String, String> entry : ConfigParseUtil.getAllAttributes(pwElement).entrySet()) {
            if (TYPE.equals(entry.getKey())) {
                type = entry.getValue();
            } else {
                params.put(entry.getKey(), entry.getValue());
            }
        }
        if (type == null) {
            throw new DataImportHandlerException(SEVERE, "The " + ConfigNameConstants.PROPERTY_WRITER + " element must specify " + TYPE);
        }
        pw = new PropertyWriter(type, params);
    }
    return new DIHConfiguration(documentTags.get(0), this, functions, script, dataSources, pw);
}
Also used : Script(org.apache.solr.handler.dataimport.config.Script) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DIHConfiguration(org.apache.solr.handler.dataimport.config.DIHConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyWriter(org.apache.solr.handler.dataimport.config.PropertyWriter)

Aggregations

PropertyWriter (org.apache.solr.handler.dataimport.config.PropertyWriter)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SolrException (org.apache.solr.common.SolrException)1 DIHConfiguration (org.apache.solr.handler.dataimport.config.DIHConfiguration)1 Script (org.apache.solr.handler.dataimport.config.Script)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1