1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import org.apache.spark.api.java.function.Function;
|
---|
6 | import org.apache.spark.api.java.function.MapFunction;
|
---|
7 | import org.apache.spark.util.DoubleAccumulator;
|
---|
8 | import org.apache.spark.util.LongAccumulator;
|
---|
9 | import org.json.JSONObject;
|
---|
10 |
|
---|
11 |
|
---|
12 | class PerPageJSONMap implements Function<JSONObject, String>
|
---|
13 | {
|
---|
14 | private static final long serialVersionUID = 1L;
|
---|
15 |
|
---|
16 | protected String _input_dir;
|
---|
17 | protected String _output_dir;
|
---|
18 | protected int _verbosity;
|
---|
19 |
|
---|
20 | protected final ArrayList<String> _solr_endpoints;
|
---|
21 | protected final int _solr_endpoints_len;
|
---|
22 |
|
---|
23 | protected LongAccumulator _progress_accum;
|
---|
24 | protected long _progress_step;
|
---|
25 |
|
---|
26 | public PerPageJSONMap(String input_dir, ArrayList<String> solr_endpoints, String output_dir, int verbosity,
|
---|
27 | LongAccumulator progress_accum, long progress_step)
|
---|
28 | {
|
---|
29 | _input_dir = input_dir;
|
---|
30 | _output_dir = output_dir;
|
---|
31 | _verbosity = verbosity;
|
---|
32 |
|
---|
33 | _solr_endpoints = solr_endpoints;
|
---|
34 | _solr_endpoints_len = _solr_endpoints.size();
|
---|
35 |
|
---|
36 | _progress_accum = progress_accum;
|
---|
37 | _progress_step = progress_step;
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 | public String call(JSONObject solr_add_doc_json)
|
---|
42 | {
|
---|
43 | String output_json_bz2 = solr_add_doc_json.getString("filename_json_bz2");
|
---|
44 | solr_add_doc_json.remove("filename_json_bz2");
|
---|
45 |
|
---|
46 | boolean random_test = (Math.random()>0.999); // every 1000
|
---|
47 |
|
---|
48 | if ((_verbosity >=2) && (random_test)) {
|
---|
49 | System.out.println("==================");
|
---|
50 | System.out.println("Sample output Solr add JSON [random test 1/1000]: " + solr_add_doc_json.toString());
|
---|
51 | System.out.println("==================");
|
---|
52 | }
|
---|
53 |
|
---|
54 | String solr_url = null;
|
---|
55 | if (_solr_endpoints_len > 0) {
|
---|
56 | int random_choice = (int)(_solr_endpoints_len * Math.random());
|
---|
57 | solr_url = _solr_endpoints.get(random_choice);
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (solr_url != null) {
|
---|
61 | if ((_verbosity >=2) && (random_test)) {
|
---|
62 | System.out.println("==================");
|
---|
63 | System.out.println("Posting to: " + solr_url);
|
---|
64 | System.out.println("==================");
|
---|
65 | }
|
---|
66 | SolrDocJSON.postSolrDoc(solr_url, solr_add_doc_json);
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (_output_dir != null) {
|
---|
70 | if ((_verbosity >=2) && (random_test)) {
|
---|
71 | System.out.println("==================");
|
---|
72 | System.out.println("Saving to: " + _output_dir);
|
---|
73 | System.out.println("==================");
|
---|
74 | }
|
---|
75 | SolrDocJSON.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
|
---|
76 | }
|
---|
77 |
|
---|
78 | _progress_accum.add(_progress_step);
|
---|
79 |
|
---|
80 | return output_json_bz2;
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|