2016-07-12 4 views
1
zugegriffen werden kann

schrieb ich die folgende Klasse für die Indizierung von Dokumenten in Elasticsearch:Constructor TransportClient in Klasse TransportClient nicht

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 
    } 
} 

Dann habe ich es wie folgt verwenden:

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) 

Dies ist die Fehlermeldung, die ich bekam :

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

Was ist hier falsch?

Antwort

0

In der Elasticsearch Connector-API hat die TransportClient-Klasse keinen öffentlichen Konstruktor, sondern einen deklarierten privaten Konstruktor. Sie können also eine Instanz des TransportClients nicht direkt "neu" erstellen. Die API verwendet die Builder Pattern ziemlich stark. Um eine Instanz des TransportClients zu erstellen, müssen Sie beispielsweise Folgendes tun:

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)) 
Verwandte Themen