[英]Cannot insert into Cassandra table, getting SyntaxError
(This is my first question here on StackOverflow:)) (这是我在 StackOverflow 上的第一个问题 :))
I have an assignment where I have to build a Cassandra database.我有一个任务,我必须构建一个 Cassandra 数据库。 I have connected Cassandra with IntelliJ, i'm writing in java and the output is shown in the command line.
我已将 Cassandra 与 IntelliJ 连接,我正在编写 java 并且 output 显示在命令行中。
My keyspace farm_db
contains a couple of tables in wish i'm would like to insert data.我的
farm_db
包含几个表,希望我想插入数据。 I would like to insert the data with two columns and a list all in one row, in the table 'farmers'.我想在“农民”表中插入包含两列和一个列表的数据。 This is a part of my database so far:
到目前为止,这是我的数据库的一部分:
cqlsh:farm_db> use farm_db;
cqlsh:farm_db> Describe tables;
farmers foods_dairy_eggs foods_meat
foods_bread_cookies foods_fruit_vegetables
cqlsh:farm_db> select * from farmers;
farmer_id | delivery | the_farmer
-----------+----------+------------
This is what i'm trying to do:这就是我想要做的:
[Picture of what i'm trying to do][1] [我正在尝试做的图片][1]
I need to insert the collection types 'list' and 'map' in 'farmers' but after a couple of failed attempts with that I tried using hashmap and arraylist instead.我需要在“farmers”中插入集合类型“list”和“map”,但经过几次失败的尝试后,我尝试使用 hashmap 和 arraylist 代替。 I think this could work but i seem to have an error in my syntax and I have no idea what the problem seem to be:
我认为这可以工作,但我的语法似乎有错误,我不知道问题似乎是什么:
Exception in thread "main" com.datastax.driver.core.exceptions.SyntaxError: line 1:31 mismatched input 'int' expecting ')' (INSERT INTO farmers (farmer_id [int]...)
Am I missing something or am I doing something wrong?我错过了什么还是我做错了什么?
This is my code:这是我的代码:
public class FarmersClass {
public static String serverIP = "127.0.0.1";
public static String keyspace = "";
//Create db
public void crateDatabase(String databaseName) {
Cluster cluster = Cluster.builder()
.addContactPoints(serverIP)
.build();
keyspace = databaseName;
Session session = cluster.connect();
String create_db_query = "CREATE KEYSPACE farm_db WITH replication "
+ "= {'class':'SimpleStrategy', 'replication_factor':1};";
session.execute(create_db_query);
}
//Create table
public void createFarmersTable() {
Cluster cluster = Cluster.builder()
.addContactPoints(serverIP)
.build();
Session session = cluster.connect("farm_db");
String create_farm_table_query = "CREATE TABLE farmers(farmer_id int PRIMARY KEY, the_farmer Map <text, text>, delivery list<text>); ";
session.execute(create_farm_table_query);
}
//Insert data in table 'farmer'.
public void insertFarmers(int id, HashMap< String, String> the_farmer, ArrayList <String> delivery) {
Cluster cluster = Cluster.builder()
.addContactPoints(serverIP)
.build();
Session session = cluster.connect("farm_db");
String insert_query = "INSERT INTO farmers (farmer_id int PRIMARY KEY, the_farmer, delivery) values (" + id + "," + the_farmer + "," + delivery + ");";
System.out.println(insert_query);
session.execute(insert_query);
}
}
public static void main(String[] args) {
FarmersClass farmersClass = new FarmersClass();
// FarmersClass.crateDatabase("farm_db");
// FarmersClass.createFarmersTable();
//Collection type map
HashMap<String, String> the_farmer = new HashMap<>();
the_farmer.put("Name", "Ana Petersen ");
the_farmer.put("Farmhouse", "The great farmhouse");
the_farmer.put("Foods", "Fruits & Vegetables");
//Collection type list
ArrayList<String> delivery = new ArrayList<String>();
String delivery_1 = "Village 1";
String delivery_2 = "Village 2";
delivery.add(delivery_1);
delivery.add(delivery_2);
FarmersClass.insertFarmers(1, the_farmer, delivery);
}
The problem is the syntax of your CQL INSERT
query:问题是 CQL
INSERT
查询的语法:
String insert_query = \
"INSERT INTO farmers (farmer_id int PRIMARY KEY, the_farmer, delivery) \
values (" + id + "," + the_farmer + "," + delivery + ");";
You've incorrectly added int PRIMARY KEY
in the list of columns.您在列列表中错误地添加了
int PRIMARY KEY
。
The correct format is:正确的格式是:
INSERT INTO table_name (pk, col2, col3) VALUES ( ... )
For details and examples, see CQL INSERT
.有关详细信息和示例,请参阅CQL
INSERT
。 Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.