java - 从 Java 应用程序将数据插入到 elastic-search

标签 java elasticsearch

嗨,我正在尝试将一些数据插入到 Elasticsearch 中。我这样尝试过,

public static void main(String args[])
{
        Client client = null;
        Settings settings = ImmutableSettings
                .settingsBuilder()
                .put("cluster.name","elasticsearch")
                .build();
        try{
            client = new TransportClient(settings)
                    .addTransportAddress(new
                            InetSocketTransportAddress("localhost",9300));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        try{
            IndexResponse response = client.prepareIndex("twitter",
                    "tweet", "1")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("user", "kimchy")
                            .field("postDate", new Date())
                            .field("message", "trying out Elastic Search")
                                            .endObject()
                            )
                            .execute()
                            .actionGet();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

}

我对 put 方法中的“localtestsearch”一无所知。但我不认为它会导致这个错误。 当我尝试运行此代码片段时,它向我显示此错误,

Exception in thread "main" org.elasticsearch.discovery.MasterNotDiscoveredException: waited for [1m]
    at org.elasticsearch.action.support.master.TransportMasterNodeOperationAction$3.onTimeout(TransportMasterNodeOperationAction.java:180)
    at org.elasticsearch.cluster.service.InternalClusterService$NotifyTimeout.run(InternalClusterService.java:451)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我无法弄清楚错误是什么,因为它说只有超时。我哪里做错了?帮助我。

编辑

elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.memory_lock: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

编辑2

这是我注释掉配置文件中的内容时遇到的错误。

    INFO: [Buzz] loaded [], sites []
Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: No node available
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:182)
    at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:94)
    at org.elasticsearch.client.support.AbstractClient.index(AbstractClient.java:79)
    at org.elasticsearch.client.transport.TransportClient.index(TransportClient.java:310)
    at com.kodcu.main.JavaAPIMain.main(JavaAPIMain.java:123)

最佳答案

您应该在elasticsearch.yml中注释掉以下关键字:

cluster.name: your_cluster_name 
node.name: your_node_name
node.master: true
node.data: true
index.number_of_shards: number_of_shards
index.number_of_replicas: number_of_replicas
network.host: 0.0.0.0
path.conf: path_to_conf
path.data: path_to_data

您还可以尝试从http://localhost:9200访问您的elasticsearch服务器。我建议安装插件 head 或插件 HQ 以获得有关您的 elasticsearch 服务器的更好信息。

希望这有帮助

Edit

我认为拼写错误是在运输端口。是 9300 而不是 9200。

关于java - 从 Java 应用程序将数据插入到 elastic-search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46557343/

相关文章:

java - 如何在 RxJava 2 中使用 Flowable?

java - 如何在android中的文本加密/解密中正确地将String转换为byte[]? (错误 - WRONG_FINAL_BLOCK_LENGTH 和 IllegalBlockSizeException)

Java, Linux : how to detect whether two java. io.Files引用同一个物理文件

elasticsearch - 通过API将文档添加到elasticsearch,然后通过kibana发现它们。怎么样?

elasticsearch - Elastic Search多字同义词无法按预期工作

elasticsearch - Elasticsearch 嵌套match_phrase问题

java - 由于以下问题无法启动服务器 : Launch process failed with exit code 1

java - PDF 字节数组到 Android 应用程序中的 pdf 文件显示

elasticsearch - Elasticsearch:浏览排序的术语

elasticsearch - Elasticsearch中子文档的子集合?