提交 b919ee81 编写于 作者: 武汉红喜's avatar 武汉红喜

Elasticsearch

上级 a6263f89
# whatsmars
*`./mvnw clean package -Dmaven.test.skip`* [`java 8+`](http://www.oracle.com/technetwork/java/javase/downloads) [`mvnrepository`](http://mvn.hongxi.org)
<br>*What's Mars?*
<br>Java生态研究(**Spring Boot**🔥 + **Redis**🔥 + **Dubbo**🔥 + **RocketMQ**🔥)
<br>Java生态研究(**Spring Boot**🔥 + **Redis**🔥 + **Dubbo**🔥 + **RocketMQ**🔥 + **Elasticsearch**🔥)
- [Spring Boot](https://spring.io/projects/spring-boot) Series(Servlet, Tomcat, Spring, SpringMVC, Spring Data, Spring Boot, Spring WebFlux, Spring Cloud)
- [Dubbo](http://dubbo.apache.org/en-us/) / [《企业IT架构转型之道:阿里巴巴中台战略思想与架构实战》](https://book.douban.com/subject/27039508/)
- [《Redis设计与实现》](https://e.jd.com/30189715.html) `e.jd.com`
- [《RocketMQ实战与原理解析》](https://book.douban.com/subject/30246992/) `douban.com`
- [《Elasticsearch技术解析与实战》](https://e.jd.com/30318357.html) `e.jd.com`
👻 [start.spring.io](https://start.spring.io) 👻 [start.dubbo.io](http://start.dubbo.io)
......@@ -40,7 +41,7 @@ whatsmars-tomcat | 模拟tomcat实现,embed tomcat
- [x] [*Redis*](https://github.com/antirez/redis)
- [ ] [*Twemproxy*](https://github.com/twitter/twemproxy)
- [ ] [*Otter*](https://github.com/alibaba/otter)
- [ ] [*Elasticsearch*](https://github.com/elastic/elasticsearch)
- [x] [*Elasticsearch*](https://github.com/elastic/elasticsearch)
### Friendship links
- [*阿里巴巴Java开发手册*](https://github.com/alibaba/Alibaba-Java-Coding-Guidelines) 📚
......
......@@ -24,6 +24,7 @@
<module>whatsmars-sharding-jdbc</module>
<module>whatsmars-earth</module>
<module>whatsmars-spring-boot-samples</module>
<module>whatsmars-elasticsearch</module>
</modules>
<properties>
......
### Elasticsearch
1. Download and unzip Elasticsearch https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-13
1. Run bin/elasticsearch (or bin\elasticsearch.bat on Windows)
1. Run curl http://localhost:9200/ or Invoke-RestMethod http://localhost:9200 with PowerShell
1. Dive into the getting started guide and video.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>whatsmars-parent</artifactId>
<groupId>org.hongxi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-elasticsearch</artifactId>
<properties>
<elasticsearch.version>5.6.13</elasticsearch.version>
</properties>
<dependencies>
<!-- 用于源码阅读 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.elasticsearch;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
/**
* Created by shenhongxi on 2018/11/19.
*/
public class ElasticsearchDemo {
public static void main(String[] args) throws Exception {
// on startup
//此步骤添加IP,至少一个,如果设置了"client.transport.sniff"= true 一个就够了,因为添加了自动嗅探配置
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
Map<String, Object> json = new HashMap<>();
json.put("user","hongxi");
json.put("postDate","2013-01-30");
json.put("message","Elasticsearch Study");
IndexResponse response = client.prepareIndex("whatsmars", "hello", "1")
.setSource(json)
.get();
System.out.println(response.getResult());
// on shutdown
client.close();
}
}
cluster.name: mars
client.transport.sniff: true
\ No newline at end of file
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%m%n
rootLogger.level = ${sys:tests.es.logger.level:-info}
rootLogger.appenderRef.console.ref = console
package org.hongxi.whatsmars.elasticsearch;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
/**
* Created by shenhongxi on 2018/11/19.
*/
public class CreateIndex {
private TransportClient client;
@Before
public void getClient() throws Exception{
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}
/**
* 使用ElasticSearch 帮助类
* @throws IOException
*/
@Test
public void CreateXContentBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("user", "javahongxi")
.field("postDate", new Date())
.field("message", "Elasticsearch Stack Study")
.endObject();
IndexResponse response = client.prepareIndex("whatsmars", "test", "1").setSource(builder).get();
System.out.println(response);
}
}
package org.hongxi.whatsmars.elasticsearch;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
/**
* Created by shenhongxi on 2018/11/19.
*/
public class GetAPI {
private TransportClient client;
@Before
public void getClient() throws Exception{
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}
@Test
public void get() {
GetResponse response = client.prepareGet("whatsmars", "fendodate", "1").get();
System.out.println(response);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册