ElasticSearch第一个测试学习

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

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.index.query.QueryBuilders;

import com.alibaba.fastjson.JSONObject;

public class QWE {
	
	
	
	/**
	 * ElasticSearch  
	 * @param args
	 */
	/**集群名称,值为{@value}*/
	public final static String CLUSTER_NAME = "cluster.name";
	/**集群配置属性名称,值为{@value}*/
	public final static String CLIENT_TRANSPORT_SNIFF = "client.transport.sniff";
	
	    private static TransportClient client;
	    
	    public static void  client(){
	    	//设置特定的集群名
	    	 Settings settings = Settings  
	 	            .settingsBuilder()  
	 	            .put(CLUSTER_NAME,"testcluster")  //集群名称
	 	            .put(CLIENT_TRANSPORT_SNIFF, true)  
	 	            .build(); 
	    	 try {
	    		 //连接特定的节点,并产生为对象client
				client = TransportClient.builder().settings(settings).build()  
				            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));
				System.out.println(client.toString()+"-------------------->");
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}  
	    }
    //取得实例  
    public static  TransportClient getTransportClient(){  
    	System.out.println(client+".......................");
        return client;  
    }  
  
   // "org.elasticsearch.action.count.CountResponse@9ee0de4"
	public static void main(String[] args) {
		client();
		System.out.println(QWE.getTransportClient().toString()+"------------------?");
		
		//查询信息获取信息类似SearchResponse ir = QWE.getTransportClient().prepareSearch().get();
		/*SearchResponse ir = QWE.getTransportClient().prepareSearch().execute().actionGet();
		System.out.println(ir.toString());*/
		
		//获取索引、类型和id位特定值的数据信息
/*		GetResponse irb = QWE.getTransportClient().prepareGet("index", "type", "id").get();*/	
	//获取1388条索引为“idx)xyr_v1”的信息
		SearchResponse irb = QWE.getTransportClient().prepareSearch("index").setQuery(QueryBuilders.matchAllQuery()
				).setSize(1388).get();
		
		
		
		System.out.println();
		try {
			//将信息转换为JSON格式,存储到文本中
			FileOutputStream st = new FileOutputStream("d:\\fuck.txt");
			st.write(JSONObject.toJSONString(irb.getHits()).getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//获取ip为特定值的信息
		/*NodesInfoResponse nir = QWE.getTransportClient().admin().cluster().prepareNodesInfo("host2").get();*/
		
		//获取并打印特定信息,将信息存储到MAP中,通过Key、Value循环打印出来。
/*		Iterator<Entry<String, Object>> map = irb.getSource().entrySet().iterator();
		System.out.println(map);
		System.out.println(JSONObject.parseObject(nir.toString()));
		System.out.println("ID = "+irb.getId()+"\n");
		System.out.println("索引是 = "+irb.getIndex()+"\n");
		System.out.println("版本是 = "+irb.getVersion()+"\n");
		System.out.println("buzhidao是 = "+irb.getSourceAsMap()+"\n");
			System.out.println("金龟子幼儿园开始上课了");
		while (map.hasNext()) {
			Entry<String, Object> entry = map.next();
			System.out.println(entry.getKey()+"\t"+entry.getValue());
		}*/
	}

}
 参考api  : https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/java-api/client.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值