我正在尝试按照此处的示例实现 Elasticsearch的传输客户端:
https://github.com/foundit/elasticsearch-transport-module-example
但问题是这个例子已经过时了 - 最明显的是它的版本。
我已经下载了这个示例并运行得很好。但是,当尝试使用相关库的更新版本的依赖项创建我自己的maven项目时,我遇到了一些我无法找到解释的问题。以下是一些可能有用的代码片段:
TransportTest.java
package
import ...
public class TransportTest {
public ESLogger logger = ESLoggerFactory.getLogger(getClass().getCanonicalName());
public static void main(String[] args) {
new TransportTest().run(args);
}
public void run(String[] args) {
TransportCommandLineParameters parameters = new TransportCommandLineParameters().parse(args, logger);
logger.info("Connection to cluster: [{}] using api key: [{}] in region [{}]", parameters.clusterId, parameters.apiKey, parameters.region);
// Build the settings for our client.
Settings settings = Settings.settingsBuilder()
// Setting "transport.type" enables this module:
.put("transport.type", "org.elasticsearch.transport.netty.FoundNettyTransport")
// Create an api key via the console and add it here:
.put("transport.found.api-key", parameters.apiKey)
.put("cluster.name", parameters.clusterId)
.put("client.transport.ignore_cluster_name", false)
.put("client.transport.node_sampler_interval", "30s")
.put("client.transport.ping_timeout", "30s")
.build();
// instantiate TransportClient and add ES to list of addresses to connect to
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
while(true) {
try {
logger.info("Getting cluster health...");
ActionFuture<ClusterHealthResponse> healthFuture = client.admin().cluster().health(Requests.clusterHealthRequest());
ClusterHealthResponse healthResponse = healthFuture.get(5, TimeUnit.SECONDS);
logger.info("Got cluster health response. Health: [{}]", healthResponse.getStatus());
} catch(Throwable t) {
logger.error("Unable to get cluster health response. Error message: [{}]", t.getMessage());
}
try {
Thread.sleep(3000);
} catch(InterruptedException ie) { ie.printStackTrace(); }
}
}
}
TransportCommandLineParameters.java
package
import ...
@Parameters(commandDescription = "Elasticsearch Transport Module.", separators = "=")
public class TransportCommandLineParameters {
@Parameter(names = {"-c", "--cluster-id"}, required = true, description = "Cluster id")
public String clusterId;
@Parameter(names = {"-a", "--api-key"}, required = true, description = "API key from the cluster ACL.")
public String apiKey;
@Parameter(names = {"-r", "--region"}, description = "Region (us-east-1/eu-west-1/...)")
public String region = "us-east-1";
public TransportCommandLineParameters parse(String[] args, ESLogger logger) {
JCommander commander = new JCommander(this);
try {
commander.parse(args);
} catch (ParameterException pe) {
logger.error(pe.getMessage());
commander.usage();
System.exit(-1);
}
return this;
}
}
错误
java:[41,165] incompatible types: java.lang.String cannot be converted to java.net.InetAddress
**RESOLVED** java:[41,21] constructor TransportClient in class org.elasticsearch.client.transport.TransportClient cannot be applied to given types;
任何帮助解决这些错误都将受到赞赏。首先,我不理解它们,我对这些问题的研究对我实现弹性搜索的传输客户端的这种特定情况没有任何帮助。
我想要注意的是,阻止我的项目编译的两个错误都来自这一行:
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
同样,任何解决这些错误的帮助都会受到赞赏,即使它知道已经知道版本控制问题。提前谢谢。