惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 天纯蓝

go错误总结(27条) GO语言最主的特性 解决“Comparison method violates its general contract!” Centos7 Mysql5.7主从服务器配置 maven buid 导出项目依赖的jar包问题 [数据库]漫谈ElasticSearch关于ES性能调优几件必须知道的事(转) MongoDB 聚合管道(Aggregation Pipeline) mongodb分布式查询 MongoDB JAVA API Filters mongodb.conf配置文件详解 mongodb安装配置 Elasticsearch-2.3.x填坑之路 CentOS VMware 下SSH配置方法详解 15个nosql数据库 MySql 优化 Elasticsearch 相关名词理解 Elasticsearch集群中处理大型日志流的几个常用概念 ES配置文件参考与参数详解 Linux下Tomcat的启动、关闭、杀死进程
Java连接Elasticsearch集群
天纯蓝 · 2016-02-25 · via 博客园 - 天纯蓝
package cn.test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
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 org.elasticsearch.search.SearchHit;
public class ElkTest {
//  private static final String CLUSTER_NAME = "cluster_name";  
    public static final String CLUSTER_NAME = "elasticsearch"; //实例名称
    private static final String IP = "127.0.0.1";  
     //private static final String IP = "192.168.0.29";  
    private static final int PORT = 9300;  //端口
    //1.设置集群名称:默认是elasticsearch,并设置client.transport.sniff为true,使客户端嗅探整个集群状态,把集群中的其他机器IP加入到客户端中  
    /* 
    //对ES1.6有效 
    private static Settings settings = ImmutableSettings 
            .settingsBuilder() 
            .put("cluster.name",CLUSTER_NAME) 
            .put("client.transport.sniff", true) 
            .build(); 
    */  
    //对ES2.0有效  
    private static Settings settings = Settings  
            .settingsBuilder()  
            .put("cluster.name",CLUSTER_NAME)  
            .put("client.transport.sniff", true)  
            .build();  
    //创建私有对象  
    private static TransportClient client;  
  
    //反射机制创建单例的TransportClient对象  ES1.6版本  
//    static {  
//        try {  
//            Class<?> clazz = Class.forName(TransportClient.class.getName());  
//            Constructor<?> constructor = clazz.getDeclaredConstructor(Settings.class);  
//            constructor.setAccessible(true);  
//            client = (TransportClient) constructor.newInstance(settings);  
//            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT));  
//        } catch (Exception e) {  
//            e.printStackTrace();  
//        }  
//    }  
  
    //ES2.0版本  
    static {  
        try {  
            client = TransportClient.builder().settings(settings).build()  
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT));  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        }  
    }  
  
    //取得实例  
    public static synchronized TransportClient getTransportClient(){  
        return client;  
    }  
  
    //为集群添加新的节点  
    public static synchronized void addNode(String name){  
        try {  
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(name),9300));  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        }  
    }  
  
    //删除集群中的某个节点  
    public static synchronized void removeNode(String name){  
        try {  
            client.removeTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(name),9300));  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        }  
    }  
    public static void main(String args[]){
        String index="logstash-2016.02.16";    
        String type="logs"; 
        SearchResponse response=ElkTest.getTransportClient().prepareSearch(index)//设置要查询的索引(index)
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setTypes(type)//设置type, 这个在建立索引的时候同时设置了, 或者可以使用head工具查看
        .setQuery(QueryBuilders.matchQuery("message", "Accept")) //在这里"message"是要查询的field,"Accept"是要查询的内容
        .setFrom(0)
        .setSize(10)
        .setExplain(true)
        .execute()
        .actionGet();
        for(SearchHit hit:response.getHits()){
            System.out.println(hit.getSourceAsString());
        }
    }  
}