Google cloud platform 如何在Google Cloud Bigtable中设置未来的插入日期?尝试使用TTL计算它
我有一个只有一个列族的表,该列的TTL为172800秒(2天),我需要在截止日期前删除一些数据。如果希望值在5分钟内过期,则计算过期时间并将插入日期设置为过期时间前5分钟 我正在使用HBase Client for Java来实现这一点 但是这个值似乎没有过期。有什么建议吗 我使用cbt创建表:Google cloud platform 如何在Google Cloud Bigtable中设置未来的插入日期?尝试使用TTL计算它,google-cloud-platform,bigtable,google-cloud-bigtable,Google Cloud Platform,Bigtable,Google Cloud Bigtable,我有一个只有一个列族的表,该列的TTL为172800秒(2天),我需要在截止日期前删除一些数据。如果希望值在5分钟内过期,则计算过期时间并将插入日期设置为过期时间前5分钟 我正在使用HBase Client for Java来实现这一点 但是这个值似乎没有过期。有什么建议吗 我使用cbt创建表: cbt createtable my_table families=cf1:maxage=2d HColumnDescriptor: {NAME => 'cf1', BLOOMFILTER =&
cbt createtable my_table families=cf1:maxage=2d
HColumnDescriptor:
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '2147483647', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => '172800 SECONDS (2 DAYS)', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
Java代码:
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public class BigTable {
public static void main(String... args) {
String projectId = "my-gcp-project-id";
String instanceId = "my-bigtable-instance-id";
String tableId = "my-table"; // my-bigtable-table-id
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
try (Table table = connection.getTable(TableName.valueOf(tableId))) {
HTableDescriptor hTableDescriptor = table.getTableDescriptor();
hTableDescriptor.setCompactionEnabled(true);
byte[] cf1 = Bytes.toBytes("cf1");
byte[] rk1 = Bytes.toBytes("rowkey1");
byte[] q1 = Bytes.toBytes("q1");
HColumnDescriptor cfDescriptor1 = hTableDescriptor.getFamily(cf1);
System.out.println("\n " + cfDescriptor1);
Calendar now = Calendar.getInstance();
Calendar now1 = Calendar.getInstance();
now1.setTime(now.getTime());
long nowMillis = now.getTimeInMillis(); // Current time
now.add(Calendar.SECOND, cfDescriptor1.getTimeToLive()); // Adding 172800 SECONDS (2 DAYS) to current time
long cfTTLMillis = now.getTimeInMillis(); // Time the values in the column family will expire at
now1.add(Calendar.SECOND, 300); // Adding 300 secs (5mins)
long expiry = now1.getTimeInMillis(); // Time the value should actually live
long creationTime = nowMillis + cfTTLMillis - expiry;
System.out.println("\n Date nowMillis:\t" + new Date(nowMillis) + "\n Date creationTime:\t" + new Date(creationTime) + "\n Date cfTTLMillis:\t" + new Date(cfTTLMillis));
//Add Data
Put p = new Put(rk1, creationTime);
p.addColumn(cf1, q1, Bytes.toBytes("CFExpiry_2d_ExpTime_5mins"));
//p.setTTL(creationtime); // What does this do?
table.put(p);
}
} catch (IOException e) {
e.printStackTrace();
}
}}
计算日期:
Date nowMillis: Wed Oct 03 10:34:15 EDT 2018
Date creationTime: Fri Oct 05 10:29:15 EDT 2018
Date cfTTLMillis: Fri Oct 05 10:34:15 EDT 2018
该值插入正确,计算日期正确。但似乎没有过期?如果我的概念错了,请纠正
编辑:
在日期计算中进行以下更正后,值确实过期
long nowMillis = System.currentTimeMillis() / 1000;
long cfTTLMillis = nowMillis - cfDescriptor1.getTimeToLive();
long creationTime = (cfTTLMillis + 300) * 1000;
在压缩发生之前,Cloud Bigtable不会对行进行垃圾收集。这可能发生在预期到期后数小时(或几天) 如果要确保不读取应已过期的数据,请在读取的数据上设置时间戳范围筛选器,以便在查询中不返回超出允许范围的值
或者,在返回数据后,您必须将其过滤掉,但在服务器端将其过滤掉更有效,这样客户端就不必下载或处理它。您似乎也在反向计算。至少比timeNow早2天的值将被删除(正如Solomon指出的那样,最终会被删除)。这意味着您希望插入时间现在为-2d+。换句话说,这应该是过去的事了。但是,实际删除可能是在值过期后数小时甚至数天。感谢Solomon和@Douglas的回复。道格拉斯,今天早上我意识到了这个错误,并更正了我的代码。现在,我设置了过去的日期,这些值确实会按预期过期。虽然我仍然可以读取一些过期的值,但在压缩尚未发生时。Solomon,正如您所建议的,我可以计算类似的时间范围并应用TimestampsFilter,但我想知道是否可以只读取在特定给定时间(现在)之后仍然有效(尚未过期)的值。