我已经成功创建了 Cassandra 连接,并且我创建了一个键空间名称单工,其中我有一个表 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();
}
}
这是我的代码,我不明白列和值在哪里不匹配。请帮忙....