gpt4 book ai didi

node.js - 试图在 bigtable 中模拟单元级 TTL,但整个列族数据被垃圾收集删除

转载 作者:行者123 更新时间:2023-12-04 18:55:15 27 4
gpt4 key购买 nike

创建了一个具有以下规则的表:
所以有了这个,数据应该在 1 秒后过期( as per docs )

async function createTable() {
console.log("Creating Table");
const options = {
families: [
{
name: 'payloads',
rule: {
age: {
seconds: 1,
nanos: 0,
},
},
},
],
};
try {
await table.create(options);
console.log("Successfully Created Table");
} catch (err) {
console.error(`Error reading rows :`, err);
}
}

然后像这样插入数据:
      const rowsToInsert = {
key: "SUMEET",
data: {
payloads: {
'1': {
value: "NOTIFICATIONS_PAYLOAD_1",
timestamp: 1576500927000,
},
'2': {
value: "NOTIFICATIONS_PAYLOAD_2",
timestamp: 1576587327000,
},
'3': {
value: "NOTIFICATIONS_PAYLOAD_3",
timestamp: 1576673727000,
},
'4': {
value: "NOTIFICATIONS_PAYLOAD_4",
timestamp: 1576760127000,
},
},
},
};

await table.insert(rowsToInsert);

所以我添加了四个具有不同时间戳的单元格:
  • 首先在写数据时提前5分钟
  • 领先 1 小时的第二名
  • 第三名,提前 1 天
  • 第四名,提前 2 天

  • 这里的问题是当我读取数据时整个列族数据被删除但它应该只根据规则集删除第一个和第二个单元格

    有什么遗漏或做错了吗?

    最佳答案

    问题在于您的时间戳,您很可能设置了过去的日期。我建议您使用 date methods from javascript 设置日期而不是像您现在所做的那样手动设置它们。

    我用以下代码做了一些测试:

    const Bigtable = require('@google-cloud/bigtable');
    const bigtable = Bigtable();
    const instance = bigtable.instance([instance]);
    const table = instance.table([table]);
    const now = new Date();


    async function writeSimple() {
    var now = new Date(Date.now());
    var fiveMinutes = new Date(now.getTime() + 5 * 60000);
    var anHour = new Date(now.getTime() + 60 * 60000);
    var aDay = new Date(now.getTime() + 24 * 60 * 60000);
    var twoDays = new Date(now.getTime() + 48 * 60 * 60000);
    const rowsToInsert = {
    key: "SUMEET",
    data: {
    payloads: {
    '1': {
    value: "NOTIFICATIONS_PAYLOAD_1",
    timestamp: now,
    },
    '2': {
    value: "NOTIFICATIONS_PAYLOAD_2",
    timestamp: fiveMinutes,
    },
    '3': {
    value: "NOTIFICATIONS_PAYLOAD_3",
    timestamp: anHour,
    },
    '4': {
    value: "NOTIFICATIONS_PAYLOAD_4",
    timestamp: aDay,
    },
    '5': {
    value: "NOTIFICATIONS_PAYLOAD_5",
    timestamp: twoDays,
    },
    },
    },
    };

    await table.insert(rowsToInsert);
    console.log(`Successfully wrote row ${rowsToInsert.key}`);
    }

    并获得如下一行:
    2019/12/17 16:53:33 -creds flag unset, will use gcloud credential
    ----------------------------------------
    SUMEET
    payloads:1 @ 2019/12/17-16:30:34.343000
    "NOTIFICATIONS_PAYLOAD_1"
    payloads:2 @ 2019/12/17-16:35:34.343000
    "NOTIFICATIONS_PAYLOAD_2"
    payloads:3 @ 2019/12/17-17:30:34.343000
    "NOTIFICATIONS_PAYLOAD_3"
    payloads:4 @ 2019/12/18-16:30:34.343000
    "NOTIFICATIONS_PAYLOAD_4"
    payloads:5 @ 2019/12/19-16:30:34.343000
    "NOTIFICATIONS_PAYLOAD_5"

    在垃圾收集器通过后(大约 15 分钟后),我得到了你想要的结果:
    2019/12/17 16:59:47 -creds flag unset, will use gcloud credential
    ----------------------------------------
    SUMEET
    payloads:3 @ 2019/12/17-17:30:34.343000
    "NOTIFICATIONS_PAYLOAD_3"
    payloads:4 @ 2019/12/18-16:30:34.343000
    "NOTIFICATIONS_PAYLOAD_4"
    payloads:5 @ 2019/12/19-16:30:34.343000
    "NOTIFICATIONS_PAYLOAD_5"

    希望这个对你有帮助!

    关于node.js - 试图在 bigtable 中模拟单元级 TTL,但整个列族数据被垃圾收集删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59368005/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com