我刚刚开始使用Crate(版本crate-0.55.2)。
我想通过JDBC连接到Crate。
这是我的示例Java代码:
import java.sql.*;
//import io.crate.*;
public class CrateTestDemo {
private Connection connection;
public static void main(String[] args) {
CrateTestDemo t = new CrateTestDemo();
try {
t.setupDatabase();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("e="+e.getMessage());
}
}
public void setupDatabase() throws ClassNotFoundException, SQLException {
Class.forName("io.crate.client.jdbc.CrateDriver");
connection = DriverManager.getConnection("crate://127.0.0.1:4301");
}
}
我已将库“ crate-jdbc-standalone-1.9.3.jar”添加到我的Java构建路径中。我已经将“ crate.yml”文件中的“ transport.tcp.port”更改为4301。 (所有其他配置均保持不变,仅此端口)。
当我运行Java代码时,我在Eclipse中得到以下输出:
log4j:WARN No appenders could be found for logger (io.crate.shade.org.elasticsearch.common.compress.lzf).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
java.sql.SQLException: Connect to '127.0.0.1:4301' failed
at io.crate.client.jdbc.CrateConnection.connect(CrateConnection.java:58)
at io.crate.client.jdbc.CrateDriver.connect(CrateDriver.java:67)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at CrateTestDemo.setupDatabase(CrateTestDemo.java:19)
at CrateTestDemo.main(CrateTestDemo.java:10)
Caused by: io.crate.shade.org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
at io.crate.shade.org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:309)
at io.crate.shade.org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:201)
at io.crate.client.InternalCrateClient.execute(InternalCrateClient.java:87)
at io.crate.client.InternalCrateClient.execute(InternalCrateClient.java:70)
at io.crate.client.InternalCrateClient.sql(InternalCrateClient.java:59)
at io.crate.client.CrateClient.sql(CrateClient.java:152)
at io.crate.client.jdbc.CrateStatement.execute(CrateStatement.java:65)
at io.crate.client.jdbc.CrateStatementBase.executeQuery(CrateStatementBase.java:37)
at io.crate.client.jdbc.CrateDatabaseMetaData.getDatabaseProductVersion(CrateDatabaseMetaData.java:79)
at io.crate.client.jdbc.CrateConnection.connect(CrateConnection.java:56)
... 5 more
e=Connect to '127.0.0.1:4301' failed
在“ crate.log”文件中,我得到了这个:
[2016-07-16 16:26:00,983][WARN ][transport.netty ] [Grasshopper II] exception caught on transport layer [[id: 0x98b5bc2f, /127.0.0.1:64412 => /127.0.0.1:4301]], closing connection
java.lang.IllegalStateException: Message not fully read (request) for requestId [0], action [cluster/nodes/info], readerIndex [39] vs expected [57]; resetting
at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:120)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462)
at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443)
at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:75)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
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)
有人知道为了使我的Java小代码运行而必须做什么吗?谢谢。
最佳答案
嗯..好吧..我应该先检查一下兼容性:)
我已将其更改为更新的库“ crate-jdbc-standalone-1.13.0.jar”,现在它可以工作了。
另请参见:https://github.com/crate/crate/issues/3786
和https://github.com/crate/crate-jdbc/tree/1.13#compatibility
关于java - crate.io jdbc连接失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38412218/