1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.net.MalformedURLException; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.HashSet; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Set; |
---|
10 | |
---|
11 | import org.apache.solr.client.solrj.SolrServerException; |
---|
12 | import org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer; |
---|
13 | import org.apache.solr.common.SolrInputDocument; |
---|
14 | import org.slf4j.Logger; |
---|
15 | import org.slf4j.LoggerFactory; |
---|
16 | import org.springframework.beans.factory.BeanFactory; |
---|
17 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.CommonUtils; |
---|
20 | import eu.clarin.cmdi.vlo.Configuration; |
---|
21 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
22 | |
---|
23 | @SuppressWarnings("serial") |
---|
24 | public class MetadataImporter { |
---|
25 | |
---|
26 | private final static Logger LOG = LoggerFactory.getLogger(MetadataImporter.class); |
---|
27 | private static Throwable serverError; |
---|
28 | private StreamingUpdateSolrServer solrServer; |
---|
29 | |
---|
30 | private Set<String> processedIds = new HashSet<String>(); |
---|
31 | protected List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); |
---|
32 | private final ImporterConfig config; |
---|
33 | |
---|
34 | private int nrOFDocumentsUpdated; |
---|
35 | private int nrOfNonExistendResourceFiles = 0; |
---|
36 | private int nrOfFilesAnalyzed = 0; |
---|
37 | private int nrOfFilesWithoutId = 0; |
---|
38 | |
---|
39 | public MetadataImporter(ImporterConfig config) { |
---|
40 | this.config = config; |
---|
41 | } |
---|
42 | |
---|
43 | void startImport() throws MalformedURLException { |
---|
44 | initSolrServer(); |
---|
45 | List<DataRoot> dataRoots = config.getDataRoots(); |
---|
46 | for (DataRoot dataRoot : dataRoots) { |
---|
47 | if (!dataRoot.getRootFile().exists()) { |
---|
48 | LOG.error("Root file " + dataRoot.getRootFile() + " does not exist. Probable configuration error so stopping import."); |
---|
49 | System.exit(1); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | long start = System.currentTimeMillis(); |
---|
54 | try { |
---|
55 | if (config.isDeleteAllFirst()) { |
---|
56 | LOG.info("Deleting original data..."); |
---|
57 | solrServer.deleteByQuery("*:*");//Delete the whole solr db. |
---|
58 | LOG.info("Deleting original data done."); |
---|
59 | } |
---|
60 | for (DataRoot dataRoot : dataRoots) { |
---|
61 | LOG.info("Start of processing: " + dataRoot.getOriginName()); |
---|
62 | if (dataRoot.isDeleteFirst()) { |
---|
63 | LOG.info("Deleting data for origin: " + dataRoot.getOriginName()); |
---|
64 | solrServer.deleteByQuery(FacetConstants.FIELD_ORIGIN + ":" + dataRoot.getOriginName()); |
---|
65 | LOG.info("Deleting data for origin done."); |
---|
66 | } |
---|
67 | CMDIDataProcessor processor = new CMDIParserVTDXML(dataRoot.getFacetMapping()); |
---|
68 | processCmdi(dataRoot.getRootFile(), dataRoot.getOriginName(), processor); |
---|
69 | if (!docs.isEmpty()) { |
---|
70 | sendDocs(); |
---|
71 | } |
---|
72 | LOG.info("End of processing: " + dataRoot.getOriginName()); |
---|
73 | } |
---|
74 | } catch (SolrServerException e) { |
---|
75 | LOG.error("error updating files:\n", e); |
---|
76 | LOG.error("Also see vlo_solr server logs for more information"); |
---|
77 | } catch (IOException e) { |
---|
78 | LOG.error("error updating files:\n", e); |
---|
79 | } finally { |
---|
80 | try { |
---|
81 | if (solrServer != null) |
---|
82 | solrServer.commit(); |
---|
83 | } catch (SolrServerException e) { |
---|
84 | LOG.error("cannot commit:\n", e); |
---|
85 | } catch (IOException e) { |
---|
86 | LOG.error("cannot commit:\n", e); |
---|
87 | } |
---|
88 | } |
---|
89 | long took = (System.currentTimeMillis() - start) / 1000; |
---|
90 | LOG.info("Found " + nrOfNonExistendResourceFiles + " non existing resources files."); |
---|
91 | LOG.info("Found " + nrOfFilesWithoutId + " file(s) without an id."); |
---|
92 | LOG.info("Update of " + nrOFDocumentsUpdated + " took " + took + " secs. Total nr of files analyzed " + nrOfFilesAnalyzed); |
---|
93 | } |
---|
94 | |
---|
95 | protected void initSolrServer() throws MalformedURLException { |
---|
96 | String solrUrl = Configuration.getInstance().getSolrUrl(); |
---|
97 | LOG.info("Initializing Solr Server on " + solrUrl); |
---|
98 | solrServer = new StreamingUpdateSolrServer(solrUrl, 1000, 2) { |
---|
99 | @Override |
---|
100 | public void handleError(Throwable ex) { |
---|
101 | super.handleError(ex); |
---|
102 | serverError = ex; |
---|
103 | } |
---|
104 | }; |
---|
105 | } |
---|
106 | |
---|
107 | private void processCmdi(File file, String origin, CMDIDataProcessor processor) throws SolrServerException, IOException { |
---|
108 | nrOfFilesAnalyzed++; |
---|
109 | CMDIData cmdiData = null; |
---|
110 | try { |
---|
111 | cmdiData = processor.process(file); |
---|
112 | } catch (Exception e) { |
---|
113 | LOG.error("error in file: " + file + " Exception", e); |
---|
114 | } |
---|
115 | if (cmdiData != null && processedIds.add(cmdiData.getId())) { |
---|
116 | SolrInputDocument solrDocument = cmdiData.getSolrDocument(); |
---|
117 | if (solrDocument != null) { |
---|
118 | updateDocument(solrDocument, cmdiData, file, origin); |
---|
119 | } |
---|
120 | List<Resource> resources = cmdiData.getMetadataResources(); |
---|
121 | for (Resource cmdiResource : resources) { |
---|
122 | File resourceFile = new File(file.getParentFile(), cmdiResource.getResourceName()); |
---|
123 | if (resourceFile.exists()) { |
---|
124 | processCmdi(resourceFile, origin, processor); |
---|
125 | } else { |
---|
126 | nrOfNonExistendResourceFiles++; |
---|
127 | LOG.error("Found nonexistent resource file (" + cmdiResource + ") in cmdi: " + file); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | private void updateDocument(SolrInputDocument solrDocument, CMDIData cmdiData, File file, String origin) throws SolrServerException, |
---|
134 | IOException { |
---|
135 | if (cmdiData.getId() == null || cmdiData.getId().isEmpty()) { |
---|
136 | nrOfFilesWithoutId++; |
---|
137 | LOG.info("Ignoring document without id, fileName: " + file); |
---|
138 | } else { |
---|
139 | solrDocument.addField(FacetConstants.FIELD_ORIGIN, origin); |
---|
140 | solrDocument.addField(FacetConstants.FIELD_DATA_ROOT, origin); |
---|
141 | solrDocument.addField(FacetConstants.FIELD_ID, cmdiData.getId()); |
---|
142 | solrDocument.addField(FacetConstants.FIELD_FILENAME, file.getAbsolutePath()); |
---|
143 | List<Resource> resources = cmdiData.getDataResources(); |
---|
144 | for (Resource resource : resources) { |
---|
145 | String mimeType = resource.getMimeType(); |
---|
146 | if (mimeType == null) { |
---|
147 | mimeType = "unknown"; |
---|
148 | } |
---|
149 | solrDocument.addField(FacetConstants.FIELD_RESOURCE_TYPE, CommonUtils.normalizeMimeType(mimeType)); |
---|
150 | solrDocument.addField(FacetConstants.FIELD_RESOURCE, mimeType + "," + resource.getResourceName()); |
---|
151 | } |
---|
152 | docs.add(solrDocument); |
---|
153 | if (docs.size() == 1000) { |
---|
154 | sendDocs(); |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | protected void sendDocs() throws SolrServerException, IOException { |
---|
160 | LOG.info("Sending " + docs.size() + " docs to solr server. Total number of docs updated till now: " + nrOFDocumentsUpdated); |
---|
161 | nrOFDocumentsUpdated += docs.size(); |
---|
162 | solrServer.add(docs); |
---|
163 | if (serverError != null) { |
---|
164 | throw new SolrServerException(serverError); |
---|
165 | } |
---|
166 | docs = new ArrayList<SolrInputDocument>(); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * @param args |
---|
171 | * @throws MalformedURLException |
---|
172 | */ |
---|
173 | public static void main(String[] args) throws MalformedURLException { |
---|
174 | BeanFactory factory = new ClassPathXmlApplicationContext(new String[] { Configuration.CONFIG_FILE, ImporterConfig.CONFIG_FILE }); |
---|
175 | factory.getBean("configuration"); |
---|
176 | ImporterConfig config = (ImporterConfig) factory.getBean("importerConfig", ImporterConfig.class); |
---|
177 | MetadataImporter importer = new MetadataImporter(config); |
---|
178 | importer.startImport(); |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|