0

我刚刚开始玩 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 代码运行必须做些什么吗?谢谢。

4

1 回答 1

1

嗯..好的..我之前应该检查一下兼容性:)

我已将其更改为较新的库“crate-jdbc-standalone-1.13.0.jar”,现在它可以工作了。

另请参阅:https ://github.com/crate/crate/issues/3786 和https://github.com/crate/crate-jdbc/tree/1.13#compatibility

于 2016-07-16T15:25:42.163 回答