[31011] | 1 | package org.hathitrust.extractedfeatures;
|
---|
| 2 |
|
---|
[31028] | 3 | import java.util.ArrayList;
|
---|
| 4 |
|
---|
[31011] | 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;
|
---|
[31028] | 8 | import org.apache.spark.util.LongAccumulator;
|
---|
[31011] | 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 |
|
---|
[31028] | 20 | protected final ArrayList<String> _solr_endpoints;
|
---|
| 21 | protected final int _solr_endpoints_len;
|
---|
[31011] | 22 |
|
---|
[31028] | 23 | protected LongAccumulator _progress_accum;
|
---|
| 24 | protected long _progress_step;
|
---|
[31011] | 25 |
|
---|
[31028] | 26 | public PerPageJSONMap(String input_dir, ArrayList<String> solr_endpoints, String output_dir, int verbosity,
|
---|
| 27 | LongAccumulator progress_accum, long progress_step)
|
---|
[31011] | 28 | {
|
---|
| 29 | _input_dir = input_dir;
|
---|
| 30 | _output_dir = output_dir;
|
---|
| 31 | _verbosity = verbosity;
|
---|
| 32 |
|
---|
[31028] | 33 | _solr_endpoints = solr_endpoints;
|
---|
| 34 | _solr_endpoints_len = _solr_endpoints.size();
|
---|
| 35 |
|
---|
[31011] | 36 | _progress_accum = progress_accum;
|
---|
| 37 | _progress_step = progress_step;
|
---|
[31045] | 38 |
|
---|
[31011] | 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 |
|
---|
[31028] | 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) {
|
---|
[31011] | 61 | if ((_verbosity >=2) && (random_test)) {
|
---|
| 62 | System.out.println("==================");
|
---|
[31028] | 63 | System.out.println("Posting to: " + solr_url);
|
---|
[31011] | 64 | System.out.println("==================");
|
---|
| 65 | }
|
---|
[31028] | 66 | SolrDocJSON.postSolrDoc(solr_url, solr_add_doc_json);
|
---|
[31011] | 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 |
|
---|
[31013] | 78 | _progress_accum.add(_progress_step);
|
---|
| 79 |
|
---|
[31011] | 80 | return output_json_bz2;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|