CassandraError:尽管JavaScript中大小写一致,但由于区分大小写,列名显然未定义

hts6caw3  于 2022-09-27  发布在  Cassandra
关注(0)|答案(3)|浏览(172)

我有一个Cassandra CQL语句正在JavaScript文件中执行,其中包含多个参数,包括networkInterfaces TEXT:

client.execute("CREATE TABLE IF NOT EXISTS testdb.networkData (latitude DOUBLE, longitude DOUBLE, allowsVoIP BOOLEAN, speed DOUBLE, uuid TEXT, timestamp TIMESTAMP PRIMARY KEY, networkType TEXT, carrierName TEXT, altitude DOUBLE, macAddress TEXT, publicIP TEXT, ssidLabel TEXT, networkInterfaces TEXT)", function (err, result) {
    if (!err){
        if ( result.rowLength > 0 ) {
            var data = result.rows[0];
            console.log("Total rows : "+ data.count);
        } else {
            console.log("No rows");
        }
    }else{
        console.log("count results err : "+err);
    }
});

在出现networkInterfaces的所有其他示例中使用相同的“networkInterface”。这包括将数据插入表的代码:

// TODO: Save LAN interfaces as well
const recordCommand = 'INSERT INTO testdb.networkData (latitude, longitude, allowsVoIP, speed, uuid, timestamp, networkType, carrierName, altitude, macAddress, publicIP, ssidLabel, networkInterfaces) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
const params = [
    request.body.latitude,
    request.body.longitude,
    request.body.allowsVoIP,
    request.body.speed,
    request.body.uuid,
    request.body.timestamp,
    request.body.networkType,
    request.body.carrierName,
    request.body.altitude,
    request.body.macAddress,
    request.body.publicIP,
    request.body.ssidLabel,
    request.body.networkInterfaces
];

client.execute(recordCommand, params, { prepare: true }, function (error) {
    assert.ifError(error);
    //Inserted in the cluster
  });

然而,当执行该命令时,它表示存在以下未处理的错误:
错误:未定义列名网络接口
未定义的列名似乎大小写不匹配,但networkInterfaces的所有示例都具有相同的大小写。还有其他问题吗?

b5buobof

b5buobof1#

默认情况下,Cassandra将以小写形式转换所有表/列名称。如果需要区分大小写的表/列名称,请用双引号将其括起来。使用desc table_name检查表格详细信息。

Eg:
Create table test(Column1 text primary key,"Column2" text);
 will result in cassandra table with 2 columns (column1 [Note all are lower case] and Column2 [C is uppercase])
mi7gmzs6

mi7gmzs62#

这是更正脚本的示例。这里我的密钥空间名称是yupoint。

CREATE TABLE IF NOT EXISTS yupoint.user (
    id text,
    login text,
    password text,
    firstname text,
    lastname text,
    email text,
    activated boolean,
    lang_key text,
    activation_key text,
    reset_key text,
    reset_date timestamp,
    authorities set<text>,;
    PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS yupoint.user_by_login (
    login text,
    id text,
    PRIMARY KEY(login, id)
);

现在更改使用您的键区并创建一些记录。
使用yupoint;

yupoint>INSERT INTO user (id, login, password , firstname, lastname ,email ,activated ,lang_key, activation_key, authorities)
VALUES('3','admin','$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC','','Administrator','admin@localhost',true,'en','', {'ROLE_USER', 'ROLE_ADMIN'});

yupoint>INSERT INTO user_by_login (login, id)
VALUES('admin','3');
gjmwrych

gjmwrych3#

Cassandra中的所有标识符(表名、列名等)不区分大小写,除非每次引用它们时都将它们用引号括起来DataStax documentation on this
因为这往往很繁琐,所以大多数人使用snake_case来命名表和列,如network_datanetwork_type。如果确实用引号括起来,这些标识符将继续出现在所有DESCRIBE输出中的引号中:

cqlsh:testdb> DESCRIBE TABLES;
table1  table2  network_data  "networkData"  table5

我已将您的查询调整为使用大写,并已在本地成功测试:

const createTable = (
  `CREATE TABLE IF NOT EXISTS testdb."networkData" (
    latitude DOUBLE, 
    longitude DOUBLE, 
    "allowsVoIP" BOOLEAN, 
    speed DOUBLE, 
    uuid TEXT, 
    timestamp TIMESTAMP PRIMARY KEY, 
    "networkType" TEXT, 
    "carrierName" TEXT, 
    altitude DOUBLE, 
    "macAddress" TEXT, 
    "publicIP" TEXT, 
    "ssidLabel" TEXT, 
    "networkInterfaces" TEXT)`
);

const recordCommand = (
  `INSERT INTO testdb."networkData" (
    latitude, 
    longitude, 
    "allowsVoIP", 
    speed, 
    uuid, 
    timestamp, 
    "networkType",
    "carrierName", 
    altitude, 
    "macAddress", 
    "publicIP", 
    "ssidLabel", 
    "networkInterfaces"
  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
);

const params = [
  request.body.latitude,
  request.body.longitude,
  request.body.allowsVoIP,
  request.body.speed,
  request.body.uuid,
  request.body.timestamp,
  request.body.networkType,
  request.body.carrierName,
  request.body.altitude,
  request.body.macAddress,
  request.body.publicIP,
  request.body.ssidLabel,
  request.body.networkInterfaces,
];

const selectData = `SELECT * FROM testdb."networkData"`;

client.connect()
  .then(() => client.execute(createTable))
  .then(() => client.execute(recordCommand, params, { prepare: true }))
  .then(() => client.execute(selectData))
  .then(result => {
    let count = result.rows.length
    if (count > 0)
      console.log(`Total rows : ${count}`)
    else console.log("No rows")
  })
  .catch(error => console.log(error));

相关问题