在Cassandra中插入值

时间:2013-06-13 10:51:49

标签: java cassandra

我已经成功创建了Cassandra连接,并且我创建了一个键空间名称simplex 我有一个表emp。现在我想随机地向emp表插入值。但它显示

不匹配的列名称/值错误。我无法理解错误。这是我的代码

import java.util.Random;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class RandomTableInsertion {


    private Session session;
    private Cluster cluster;

    Random r=new Random();


 public void connect(String node ){

 cluster=Cluster.builder().addContactPoint(node).build();
 Metadata metadata=cluster.getMetadata();
 System.out.println("Cassandra Connection Established");
 System.out.printf("Connected to cluster:%s\n",metadata.getClusterName());

 for(Host host:metadata.getAllHosts()){

     System.out.printf("dataCenter :%s; Host: %s; Rack:%s\n",host.getDatacenter(),host.getAddress(),host.getRack());
     session = cluster.connect("simplex");
 }

 }


 public void close() {
 cluster.shutdown();
 }



 public void duplicateSchema() {
 session.execute("CREATE KEYSPACE duplex WITH replication " +
         "= {'class':'SimpleStrategy', 'replication_factor':1};");

    session.execute("CREATE TABLE simplex.emp (" + "id int PRIMARY KEY,"
            + "roll int," + "marks1 int," + "marks2 int,"
            +"marks3 int" + ");");

    System.out.println("Schema is created");

}



 public void loadData() {


    session.execute("INSERT INTO emp (id, roll, marks1, marks2, marks3) "
            + "VALUES ("+r.nextInt(2)+ r.nextInt(2)+ r.nextInt(2)+r.nextInt(3) +r.nextInt(2)+")");

    System.out.println("Table Creation Successful");

}




public static void main(String[] args) {


    RandomTableInsertion random=new RandomTableInsertion();
    random.connect("127.0.0.1");
    //random.createSchema();
    //random.duplicateSchema();
    random.loadData();
    random.close();
}

}

这是我的代码,我无法理解列和值不匹配的位置。有人请帮帮....

0 个答案:

没有答案