|
@@ -0,0 +1,49 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.elasticsearch;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.util.Date;
|
|
5
|
+
|
|
6
|
+import org.elasticsearch.ElasticsearchException;
|
|
7
|
+import org.elasticsearch.action.delete.DeleteResponse;
|
|
8
|
+import org.elasticsearch.action.get.GetResponse;
|
|
9
|
+import org.elasticsearch.action.index.IndexResponse;
|
|
10
|
+import org.elasticsearch.client.Client;
|
|
11
|
+import org.elasticsearch.client.transport.TransportClient;
|
|
12
|
+import org.elasticsearch.common.settings.ImmutableSettings;
|
|
13
|
+import org.elasticsearch.common.settings.Settings;
|
|
14
|
+import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
|
15
|
+import org.elasticsearch.node.Node;
|
|
16
|
+import org.elasticsearch.node.NodeBuilder;
|
|
17
|
+
|
|
18
|
+import static org.elasticsearch.node.NodeBuilder.*;
|
|
19
|
+import static org.elasticsearch.common.xcontent.XContentFactory.*;
|
|
20
|
+
|
|
21
|
+public class App {
|
|
22
|
+
|
|
23
|
+ public static void main(String[] args) {
|
|
24
|
+ Settings settings = ImmutableSettings.settingsBuilder()
|
|
25
|
+ .put("cluster.name", "elasticsearch")
|
|
26
|
+ .build();
|
|
27
|
+ TransportClient client = new TransportClient(settings)
|
|
28
|
+ .addTransportAddress(new InetSocketTransportAddress("dev-swh.ga", 9300));
|
|
29
|
+
|
|
30
|
+ IndexResponse index_response = null;
|
|
31
|
+ try {
|
|
32
|
+ index_response = client.prepareIndex("twitter", "tweet", "1")
|
|
33
|
+ .setSource(jsonBuilder()
|
|
34
|
+ .startObject()
|
|
35
|
+ .field("user", "kimchy")
|
|
36
|
+ .field("postDate", new Date())
|
|
37
|
+ .field("message", "trying out Elasticsearch")
|
|
38
|
+ .endObject()
|
|
39
|
+ )
|
|
40
|
+ .execute()
|
|
41
|
+ .actionGet();
|
|
42
|
+ } catch (ElasticsearchException e) {
|
|
43
|
+ e.printStackTrace();
|
|
44
|
+ } catch (IOException e) {
|
|
45
|
+ e.printStackTrace();
|
|
46
|
+ }
|
|
47
|
+ client.close();;
|
|
48
|
+ }
|
|
49
|
+}
|