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
ElasticSearch第一个测试学习
最新推荐文章于 2022-12-20 11:20:19 发布