edu.ucsd.ccdb.cil.xml2json.ElasticsearchClient.java Source code

Java tutorial

Introduction

Here is the source code for edu.ucsd.ccdb.cil.xml2json.ElasticsearchClient.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ucsd.ccdb.cil.xml2json;

import java.io.File;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONObject;
import org.omg.CORBA.portable.ResponseHandler;

/**
 *
 * @author ncmir
 */
public class ElasticsearchClient {

    public void xputElastic(String index, String type, String ID, File f) throws Exception {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "http://localhost:9200/" + index + "/" + type + "/" + ID;
        System.out.println(url);
        HttpPut put = new HttpPut(url); //-X PUT
        put.setEntity(new FileEntity(f, "application/json")); //@ - absolute path
        BasicResponseHandler responseHandler = new BasicResponseHandler();

        String o = httpClient.execute(put, responseHandler);
        /* System.err.println("----------"+o+"----------");
         if(o == null || o.trim().length() == 0)
         {
             System.err.println(o);
             System.exit(1);
         } */

    }

    public static void main(String[] args) throws Exception {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            //HttpPut put = new HttpPut("http://localhost:9200/customer/user/john@smith.com");  //-X PUT
            //put.setEntity(new FileEntity(new File("/Users/ncmir/NetBeansProjects/MyTest/web/customer.json"), "application/json"));  //@ - absolute path
            HttpPut put = new HttpPut("http://localhost:9200/ccdb/data/1"); //-X PUT
            put.setEntity(
                    new FileEntity(new File("/Users/ncmir/Documents/CCDB/ccdbJson/1.json"), "application/json")); //@ - absolute path

            BasicResponseHandler responseHandler = new BasicResponseHandler();

            String o = httpClient.execute(put, responseHandler);
            System.out.println(o);
        } catch (Exception e) {
            //-f, fail silently
            e.printStackTrace();
        }
    }
}