drop table not working - com.datastax.driver.core

时间:2016-05-04 03:49:06

标签: java datastax cassandra-2.1

使用Cassandra的数据存储驱动程序删除表看起来不起作用。 create table有效,但drop table不会,也不会抛出异常。 1)我正确地做了下降吗? 2)其他人看到过这种行为?

在输出中,您可以看到表已创建并且显然已删除,因为它不在第一次运行的第二个表列表中。但是,当我重新连接(第二次运行)时,表就会导致异常。

import java.util.Collection;
import com.datastax.driver.core.*;

public class Fail {
    SimpleStatement createTableCQL = new SimpleStatement("create table test_table(testfield varchar primary key)");
    SimpleStatement dropTableCQL = new SimpleStatement("drop table test_table");
    Session session = null;
    Cluster cluster = null;

    public Fail()
    {
        System.out.println("First Run");
        this.run();
        System.out.println("Second Run");
        this.run();
    }

    private void run()
    {
        try
        {
            cluster = Cluster.builder().addContactPoints("10.48.8.43 10.48.8.47 10.48.8.53")
                  .withCredentials("394016","394016")
                  .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL))
                  .build();
            session = cluster.connect("gid394016");
        }
        catch(Exception e)
        {
            System.err.println(e.toString());
            System.exit(1);
        }

        //create the table
        System.out.println("createTableCQL");
        this.session.execute(createTableCQL);

        //list tables in the keyspace
        System.out.println("Table list:");
        Collection<TableMetadata> results1 = cluster.getMetadata().getKeyspace("gid394016").getTables();
        for (TableMetadata tm : results1)
        {
            System.out.println(tm.toString());
        }

        //drop the table
        System.out.println("dropTableCQL");
        this.session.execute(dropTableCQL);

        //list tables in the keyspace
        System.out.println("Table list:");
        Collection<TableMetadata> results2 = cluster.getMetadata().getKeyspace("gid394016").getTables();
        for (TableMetadata tm : results2)
        {
            System.out.println(tm.toString());
        }

        session.close();
        cluster.close();    
    }

    public static void main(String[] args) {
        new Fail(); 
    }

}

控制台输出:

First Run
[main] INFO com.datastax.driver.core.NettyUtil - Did not find Netty's native epoll transport in the classpath, defaulting to NIO.
[main] INFO com.datastax.driver.core.policies.DCAwareRoundRobinPolicy - Using data-center name 'Cassandra' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.51:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.47:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.53:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.49:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host 10.48.8.43 10.48.8.47 10.48.8.53/10.48.8.43:9042 added
createTableCQL
Table list:
CREATE TABLE gid394016.test_table (testfield text, PRIMARY KEY (testfield)) WITH read_repair_chance = 0.0 AND dclocal_read_repair_chance = 0.1 AND gc_grace_seconds = 864000 AND bloom_filter_fp_chance = 0.01 AND caching = { 'keys' : 'ALL', 'rows_per_partition' : 'NONE' } AND comment = '' AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' } AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.LZ4Compressor' } AND default_time_to_live = 0 AND speculative_retry = '99.0PERCENTILE' AND min_index_interval = 128 AND max_index_interval = 2048;
dropTableCQL
Table list:
Second Run
[main] INFO com.datastax.driver.core.policies.DCAwareRoundRobinPolicy - Using data-center name 'Cassandra' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.51:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.47:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.53:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host /10.48.8.49:9042 added
[main] INFO com.datastax.driver.core.Cluster - New Cassandra host 10.48.8.43 10.48.8.47 10.48.8.53/10.48.8.43:9042 added
createTableCQL
Exception in thread "main" com.datastax.driver.core.exceptions.AlreadyExistsException: Table gid394016.test_table already exists
    at com.datastax.driver.core.exceptions.AlreadyExistsException.copy(AlreadyExistsException.java:111)
    at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:37)
    at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:217)
    at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:54)
    at com.bdcauto.cassandrachecks.Fail.run(Fail.java:38)
    at com.bdcauto.cassandrachecks.Fail.<init>(Fail.java:17)
    at com.bdcauto.cassandrachecks.Fail.main(Fail.java:65)
Caused by: com.datastax.driver.core.exceptions.AlreadyExistsException: Table gid394016.test_table already exists
    at com.datastax.driver.core.exceptions.AlreadyExistsException.copy(AlreadyExistsException.java:130)
    at com.datastax.driver.core.Responses$Error.asException(Responses.java:118)
    at com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:151)
    at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:175)
    at com.datastax.driver.core.RequestHandler.access$2500(RequestHandler.java:44)
    at com.datastax.driver.core.RequestHandler$SpeculativeExecution.setFinalResult(RequestHandler.java:801)
    at com.datastax.driver.core.RequestHandler$SpeculativeExecution.onSet(RequestHandler.java:617)
    at com.datastax.driver.core.Connection$Dispatcher.channelRead0(Connection.java:1014)
    at com.datastax.driver.core.Connection$Dispatcher.channelRead0(Connection.java:937)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:276)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:263)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.datastax.driver.core.exceptions.AlreadyExistsException: Table gid394016.test_table already exists
    at com.datastax.driver.core.Responses$Error$1.decode(Responses.java:69)
    at com.datastax.driver.core.Responses$Error$1.decode(Responses.java:37)
    at com.datastax.driver.core.Message$ProtocolDecoder.decode(Message.java:230)
    at com.datastax.driver.core.Message$ProtocolDecoder.decode(Message.java:221)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89)
    ... 14 more

1 个答案:

答案 0 :(得分:0)

您正在使用数据库中的表格运行此代码,这就是为什么您已经存在&#34;已经存在&#34;错误。请使用cqlsh连接到数据库并自行检查。

创建,更改和删除表语句将异步传播到整个群集中。即使您收到协调员的回复,您仍需要等待schema agreement