Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Created December 4, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirkapenzes/898bd9f57a5cdb161db2efd031743679 to your computer and use it in GitHub Desktop.
Save jirkapenzes/898bd9f57a5cdb161db2efd031743679 to your computer and use it in GitHub Desktop.
Maven pom file
import java.util.Date;
import java.util.HashMap;
public class Attempt {
private String gamer;
private Date timestamp;
private int score;
private int round;
public Attempt(String gamer, Date timestamp, int round, int score) {
this.gamer = gamer;
this.timestamp = timestamp;
this.score = score;
this.round = round;
}
public HashMap<String, Object> toMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("gamer", gamer);
map.put("timestamp", timestamp);
map.put("score", score);
map.put("round", round);
return map;
}
}
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.util.Arrays;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws Exception {
try (RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")))) {
IndexRequest request = new IndexRequest("posts", "doc", "1")
.source("user", "kimchy",
"postDate", new Date(),
"message", "trying out Elasticsearch");
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.status());
GetRequest getRequest = new GetRequest("posts", "doc", "1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest("posts", "doc", "2")
.source(XContentType.JSON, "field", "foo"));
bulkRequest.add(new IndexRequest("posts", "doc", "3")
.source(XContentType.JSON, "field", "bar"));
bulkRequest.add(new IndexRequest("posts", "doc", "4")
.source(XContentType.JSON, "field", "baz"));
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(!bulkResponse.hasFailures());
SearchRequest searchRequest1 = new SearchRequest("posts");
SearchResponse searchResponse1 = client.search(searchRequest1, RequestOptions.DEFAULT);
System.out.println(Arrays.toString(searchResponse1.getHits().getHits()));
SearchRequest searchRequest2 = new SearchRequest("posts");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("message", "trying"));
searchRequest2.source(sourceBuilder);
SearchResponse searchResponse2 = client.search(searchRequest2, RequestOptions.DEFAULT);
System.out.println(Arrays.toString(searchResponse2.getHits().getHits()));
}
}
}
import org.apache.commons.lang3.RandomUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class Game {
public static void main(String[] args) throws Exception {
List<String> gamers = Arrays.asList("Jirka", "Honza", "Tonda", "Emil", "Martin");
int numberOfRounds = 1000;
try (RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")))) {
BulkRequest bulkRequest = new BulkRequest();
for (int round = 0; round < numberOfRounds; round++) {
Date currentDate = new Date();
for (String gamer : gamers) {
double score = RandomUtils.nextInt(1, 7);
Attempt attempt = new Attempt(gamer, currentDate, round, (int) score);
IndexRequest indexRequest = new IndexRequest("game", "attempt")
.source(attempt.toMap());
bulkRequest.add(indexRequest);
}
if (round % 100 == 0) {
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("Current round " + round + " > " + response.status());
bulkRequest = new BulkRequest();
}
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
}
}
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<elasticsearch.version>6.5.1</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment