1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import java.net.InetAddress; import java.util.ArrayList;
import org.apache.log4j.Logger; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.transport.client.PreBuiltTransportClient;
import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin; import com.floragunn.searchguard.ssl.util.SSLConfigConstants;
public class ESClientHelper {
static Logger LOG = Logger.getLogger(ESClientHelper.class);
static final String INDEX = "index-*"; static final int SIZE = 1000;
private Settings.Builder settingsBuilder; private Settings settings; private TransportClient client;
@SuppressWarnings({ "resource", "unchecked" }) public ESClientHelper() {
try { settingsBuilder = Settings.builder().put("path.home", ".").put("cluster.name", "elasticSearch") .put("client.transport.sniff", true) .put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_ENABLE_OPENSSL_IF_AVAILABLE, true) .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENABLE_OPENSSL_IF_AVAILABLE, true) .put("searchguard.ssl.transport.enabled", true) .put("searchguard.ssl.transport.keystore_filepath", "D:/code/node-1-keystore.jks") .put("searchguard.ssl.transport.truststore_filepath", "D:/code/truststore.jks") .put("searchguard.ssl.transport.keystore_password", "password") .put("searchguard.ssl.transport.truststore_password", "password") .put("searchguard.ssl.transport.enforce_hostname_verification", false);
settings = settingsBuilder.build();
client = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); } catch (Exception e) { LOG.error("create ESClientHelper failed!"); } }
public ArrayList<String> getResponse(int fromN, int sizeN) { try {
FieldSortBuilder sortBuilders = SortBuilders.fieldSort("@timestamp").order(SortOrder.ASC);
SearchResponse response = client.prepareSearch(INDEX).addSort(sortBuilders).setFrom(fromN).setSize(sizeN) .execute().actionGet(); System.out.println(response.getHits().getHits().length);
return getContentList(response);
} catch (Exception e) { LOG.error("getResponse failed, the parameter: " + "," + fromN + "," + sizeN); } return null; }
private ArrayList<String> getContentList(SearchResponse response) {
ArrayList<String> list = new ArrayList<String>();
try { if (response == null) return null; for (int idx = 0, len = response.getHits().getHits().length; idx < len; idx++) {
list.add(response.getHits().getAt(idx).getSource().get("content").toString()); } } catch (Exception e) { LOG.error("response transport to list failed!"); }
return list; }
public void closeClient() { try { client.close(); } catch (Exception e) { LOG.error("close ES java client failed!"); } }
public static void main(String[] args) { System.out.println(new ESClientHelper().getResponse(0, 1000).toString()); } }
|