【问题标题】:How to update collection in cassandra whose key is timestamp type如何在 cassandra 中更新其键为时间戳类型的集合
【发布时间】:2015-02-05 06:08:07
【问题描述】:

我正在尝试更新“地图”列,但尝试这样做会导致错误:

错误:

error: database errorTypeError: Not a valid bigint, expected Long/Number/Buffer, obtained '2014-12-07T13:53:10.658Z'

尝试

var query_attendance = 'UPDATE emp_attendance SET attendace = attendace + ? where year = ? and month = ? and emp_id = ?';
var udpateAttendance = function(empId, timestampMillis, cb){
    var foundDate = new Date(timestampMillis);
    var year = foundDate.getUTCFullYear();
    var month = foundDate.getUTCMonth();
    var date = foundDate.getUTCDate();
    var attendace = {};
    attendace['2014-12-07T13:53:10.658Z'] = 'Present';
//  winston.info('attendace' + JSON.stringify(foundDate));
    var values = [attendace, year, month, empId];
    var options = {
        hints: ['map','int','int','int'],
        prepare: true
    };
    winston.info('Values: ' + JSON.stringify(values));
    client.execute(query_attendance, values, options, function(err, resultSet){
        winston.info('Query Completed');
        if(err){
            winston.error('database error' + err);
            cb(err, null);
            return;         
        }
        winston.info('Query successful');
        cb(null, resultSet);
    });
}

我的观点:

我想我需要告诉驱动程序地图集合中的键类型是时间戳类型,但我没有找到如何为驱动程序指定此类输入。

【问题讨论】:

    标签: javascript node.js cassandra nosql


    【解决方案1】:

    在对 cassandra 驱动库进行大量调试后,我终于发现了那里的错误。

    当“cassandra-driver”中的“encoder.js”接收到日期类型的输入时,“encodeTimestamp()”会使用“instanceof”关键字检查类型。 这是我的错误导致错误的地方,因为

    attendace[foundDate]
    or 
    attendace['2014-12-07T13:53:10.658Z']
    

    可能正在制作'string'类型的密钥(作为js新手,无法确认)。 所以,问题的解决方案是i need to pass date object as key of attendace variable

    注意:

    虽然,我仍然无法将日期作为对象作为另一个对象的键传递,但这完全是另一个问题。 只是为了继续运行流程(直到我找到将日期作为对象传递的方法),我在 cassandra 驱动程序中对 'encoder.js' 的 'encodeTimestamp()' 做了一个小改动。 改变

      function encodeTimestamp (value, type) {
        console.log('Encoding time stamp: ' + value + '\ttype:'+type);
        //parsing the date object, 
        var convertedDate = new Date(value);
        // check if supplied value is converted into date, if so, assign its value to value variable and continue with original process
        if((convertedDate) && (convertedDate!= NaN)){
          value = convertedDate;
        }
        if (value instanceof Date) {
          console.log('Value is date ');
          value = value.getTime();
        }else{
          console.log('input value ' + value + ' is not a date');
        }
        return encodeBigNumber (value, type);
      }
    

    This is hack only,所以我希望一定有一些真正的解决方案,并且有人会想出那个,

    【讨论】:

    • 不可能,我创建了一个Jira ticket for it
    • 只是为了完成我现在的工作,我的问题解决方案是否正确,否则会影响 API 中的其他查询或流程
    • 解决方案是正确的,应该不会影响您应用程序的其他部分。
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 2019-01-09
    • 2018-07-17
    • 2016-08-10
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多