gpt4 book ai didi

scala - 无法访问类 TransportClient 中的构造函数 TransportClient

转载 作者:行者123 更新时间:2023-12-03 00:13:57 24 4
gpt4 key购买 nike

我为 ElasticSearch 中的文档索引编写了以下类:

import java.net.InetAddress
import com.typesafe.config.ConfigFactory
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import play.api.libs.json.{JsString, JsValue}

/**
* Created by liana on 12/07/16.
*/
class ElasticSearchConnector {

private var transportClient: TransportClient = null
private val host = "localhost"
private val port = 9300
private val cluster = "elasticsearch"
private val indexName = "tests"
private val docType = "test"

def configElasticSearch(): Unit =
{
val settings = Settings.settingsBuilder().put("cluster.name", cluster).build()
transportClient = new TransportClient(settings)
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port.toInt))
}

def putText(json: String, id: Int): String =
{
val response = transportClient.prepareIndex(indexName, docType, id)
.setSource(json)
.get()
val responseId = response.getId
responseId
}
}

然后我按如下方式使用它:
val json = """val jsonString =
{
"title": "Elastic",
"price": 2000,
"author":{
"first": "Zachary",
"last": "Tong";
}
}"""

val ec = new ElasticSearchConnector()
ec.configElasticSearch()
val id = ec.putText(json)
System.out.println(id)

这是我收到的错误消息:

Error:(28, 23) constructor TransportClient in class TransportClient cannot be accessed in class ElasticSearchConnector transportClient = new TransportClient(settings)



这里有什么问题?

最佳答案

在 Elasticsearch 连接器 API 中,TransportClient 类没有公共(public)构造函数,但有一个声明的私有(private)构造函数。因此,您不能直接“新建” TransportClient 的实例。 API 使用 Builder Pattern相当严重,因此为了创建 TransportClient 的实例,您需要执行以下操作:

val settings = Settings.settingsBuilder().put("cluster.name", cluster).build()
val transportClient = TransportClient.builder().settings(settings).build()
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port.toInt))

关于scala - 无法访问类 TransportClient 中的构造函数 TransportClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38327177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com