Ignore:
Timestamp:
2016-10-30T23:51:07+13:00 (7 years ago)
Author:
davidb
Message:

Code to work per-volume and per-page

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/hathitrust/solr-extracted-features/trunk/src/main/java/org/hathitrust/extractedfeatures/JSONSolrTransform.java

    r30996 r31001  
    11package org.hathitrust.extractedfeatures;
    22
     3import java.io.BufferedReader;
     4import java.io.BufferedWriter;
     5import java.io.IOException;
     6import java.io.InputStreamReader;
     7import java.io.OutputStream;
     8import java.net.HttpURLConnection;
     9import java.net.URL;
    310import java.util.Iterator;
    411
     12import org.apache.commons.compress.compressors.CompressorException;
    513import org.json.JSONObject;
    614
     
    115123    }
    116124
     125    public static void saveSolrDoc(JSONObject solr_add_doc_json, String output_file_json_bz2)
     126    {
     127        try {
     128            BufferedWriter bw = ClusterFileIO.getBufferedWriterForCompressedFile(output_file_json_bz2);
     129            bw.write(solr_add_doc_json.toString());
     130            bw.close();
     131        } catch (IOException e) {
     132            e.printStackTrace();
     133        } catch (CompressorException e) {
     134            e.printStackTrace();
     135        }
     136    }
     137   
     138    public static void postSolrDoc(String post_url, JSONObject solr_add_doc_json)
     139    {
     140       
     141        //String curl_popen = "curl -X POST -H 'Content-Type: application/json'";
     142        //curl_popen += " 'http://10.11.0.53:8983/solr/htrc-pd-ef/update'";
     143        //curl_popen += " --data-binary '";
     144        //curl_popen += "'"
     145
     146
     147        try {
     148            HttpURLConnection httpcon = (HttpURLConnection) ((new URL(post_url).openConnection()));
     149            httpcon.setDoOutput(true);
     150            httpcon.setRequestProperty("Content-Type", "application/json");
     151            httpcon.setRequestProperty("Accept", "application/json");
     152            httpcon.setRequestMethod("POST");
     153            httpcon.connect();
     154
     155            byte[] outputBytes = solr_add_doc_json.toString().getBytes("UTF-8");
     156            OutputStream os = httpcon.getOutputStream();
     157            os.write(outputBytes);
     158            os.close();
     159           
     160           
     161            // Read response
     162            StringBuilder sb = new StringBuilder();
     163            BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
     164            String decodedString;
     165            while ((decodedString = in.readLine()) != null) {
     166                sb.append(decodedString);
     167            }
     168            in.close();
     169
     170            JSONObject solr_status_json = new JSONObject(sb.toString());
     171            JSONObject response_header_json = solr_status_json.getJSONObject("responseHeader");
     172            if (response_header_json != null) {
     173                int status = response_header_json.getInt("status");
     174                if (status != 0) {
     175                    System.err.println("Warning: POST request to " + post_url + " returned status " + status);
     176                    System.err.println("Full response was: " + sb);
     177                }
     178            }
     179            else {
     180                System.err.println("Failed response to Solr POST: " + sb);
     181            }
     182           
     183           
     184           
     185        }
     186        catch (Exception e) {
     187            e.printStackTrace();
     188        }
     189       
     190    }
    117191}
Note: See TracChangeset for help on using the changeset viewer.