elasticsearch java API
官网:
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/
1. 启动elasticsearch:
sh start-elasticsearch-jiqun.sh
1 #!/bin/bash
2
3 ##
4 # start elasticsearch jiqun
5 ##
6
7 cd /Users/hyy044101331/programfile/elasticsearch/elasticsearch-node1/bin
8 ./elasticsearch -d -Xms512m -Xmx512m
9
10 cd /Users/hyy044101331/programfile/elasticsearch/elasticsearch-node2/bin
11 ./elasticsearch -d -Xms512m -Xmx512m
2. 添加maven依赖:
<!-- *** elasticsearch *** -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>4.10.2</version>
</dependency>
<!-- *************** -->
3. ElasticSearchClient
package com.mengka.elasticsearch;
public class Constant {
/**
* elacticsearch集群名字
*
*/
public static final String CLUSTER_NAME = "mengka-elasticsearch";
/**
* node节点名字
*
*/
public static final String ELASTICSEARCH_NODE_1_NAME = "mengka-mac-node1";
public static final String ELASTICSEARCH_NODE_2_NAME = "mengka-mac-node2";
/**
* elacticsearch集群IP地址
*/
public static final String ELASTICSEARCH_IP = "127.0.0.1";
/**
* node1和node2的端口:
* [9300-9400] is for node-to-node communication
*/
public static final int ELASTICSEARCH_NODE_1_PORT = 9300;
public static final int ELASTICSEARCH_NODE_2_PORT = 9301;
/**
* node1和node2的http端口:
* [9200-9300] is for HTTP traffic
*/
public static final int ELASTICSEARCH_NODE_1_HTTP_PORT = 9200;
public static final int ELASTICSEARCH_NODE_2_HTTP_PORT = 9201;
}
package com.mengka.elasticsearch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
/**
* client类型
* <br/>
* 》》node client:
* <ul>
* <li>客户端节点本身也是elasticsearch节点;</li>
* <li>该节点也会加入集群,和其他节点一样;</li>
* </ul>
* <p/>
* 》》TransportClient:
* <ul>
* <li>更加轻量级;</li>
* <li>客户端socket连接到elasticsearch集群;</li>
* <li>netty线程池;</li>
* </ul>
* <p/>
* Created by mengka
*/
public class ElasticSearchClient {
private static final Log log = LogFactory.getLog(ElasticSearchClient.class);
private static TransportClient client;
private ElasticSearchClient() {
try {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", Constant.CLUSTER_NAME).build();
client = new TransportClient(settings);
client.addTransportAddress(new
InetSocketTransportAddress(Constant.ELASTICSEARCH_IP, Constant.ELASTICSEARCH_NODE_1_PORT));
// client.addTransportAddress(new
// InetSocketTransportAddress(Constant.ELASTICSEARCH_IP, Constant.ELASTICSEARCH_NODE_2_PORT));
} catch (Exception e) {
log.error("ElasticSearchClient client error!", e);
}
}
public void close() {
client.close();
}
public Client client() {
return client;
}
public static ElasticSearchClient getElasticSearchClient() {
return ElasticSearchClientHolder.elasticSearchClient_holder;
}
private static class ElasticSearchClientHolder {
private static final ElasticSearchClient elasticSearchClient_holder = new ElasticSearchClient();
}
}
4. 插入数据
import java.util.List;
import com.mengka.elasticsearch.ElasticSearchClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
/**
* elasticsearch文档(java API):<br>
* http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/
* <hr>
*
* @author mengka.hyy
*/
public class put_01 {
private static final Log log = LogFactory.getLog(put_01.class);
private static Client client = ElasticSearchClient.getElasticSearchClient().client();
public static void main(String[] args) throws Exception {
try {
invoke();
} finally {
client.close();
}
}
public static void invoke() throws Exception {
/**
* 例一:
* 往elasticsearch里面插入数据
*/
XContentBuilder content = jsonBuilder().startObject()
.field("name", "hyy044101331").field("age", "22")
.field("address", "xiamen").endObject();
IndexResponse response = client.prepareIndex("mengka", "age", "22")
.setSource(content).execute().actionGet();
String _index = response.getIndex();
String _type = response.getType();
String _id = response.getId();
long _version = response.getVersion();
log.info("------------, elasticsearch _index = " + _index
+ " , _type = " + _type + " , _id = " + _id + " , _version = "
+ _version);
}
}
5. 查询数据
curl -XGET 'http://localhost:9200/_search?pretty=true' -d '{"query":{"match":{"address":"xiamen"}}}'
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com