elasticsearch使用_ java API
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"}}}'
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29