Cassandra的timestamp类型
来源:互联网 发布:重庆时时彩杀号软件 编辑:程序博客网 时间:2024/06/06 02:23
cqlsh:testspace> CREATE TABLE test_timestamp(id int, date timestamp, PRIMARY KEY(id,date));cqlsh:testspace> INSERT INTO test_timestamp (id, date ) VALUES ( 1,'2015-05-18 09:00');cqlsh:testspace> INSERT INTO test_timestamp (id, date ) VALUES ( 4,'2015-05-17 22:48:38');cqlsh:testspace> INSERT INTO test_timestamp (id, date ) VALUES ( 3,'2015-05-17 21:48:38');cqlsh:testspace> INSERT INTO test_timestamp (id, date ) VALUES ( 2,'2015-05-17 20:48:38');cqlsh:testspace> SELECT * FROM test_timestamp ... ; id | date----+-------------------------- 1 | 2015-05-18 09:00:00+0800 2 | 2015-05-17 20:48:38+0800 4 | 2015-05-17 22:48:38+0800 3 | 2015-05-17 21:48:38+0800(4 rows)
查询语句不限定PRIMARY KEY的话需要用ALLOW FILTERING
cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17';Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERINGcqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17' allow filtering; id | date----+-------------------------- 1 | 2015-05-18 09:00:00+0800 2 | 2015-05-17 20:48:38+0800 4 | 2015-05-17 22:48:38+0800 3 | 2015-05-17 21:48:38+0800(4 rows)
timestamp的字符串表达比较灵活
cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-18' allow filtering; id | date----+-------------------------- 1 | 2015-05-18 09:00:00+0800(1 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17 20:00' and date<'2015-05-17 21:00' allow filtering; id | date----+-------------------------- 2 | 2015-05-17 20:48:38+0800(1 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17';Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERINGcqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17 20:00' and date<'2015-05-17 21:00' allow filtering; id | date----+-------------------------- 2 | 2015-05-17 20:48:38+0800(1 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-18' allow filtering; id | date----+-------------------------- 1 | 2015-05-18 09:00:00+0800(1 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17';Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERINGcqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17' allow filtering; id | date----+-------------------------- 1 | 2015-05-18 09:00:00+0800 2 | 2015-05-17 20:48:38+0800 4 | 2015-05-17 22:48:38+0800 3 | 2015-05-17 21:48:38+0800(4 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE date>'2015-05-17 20:45:00' and date<'2015-05-17 20:50:00' allow filtering; id | date----+-------------------------- 2 | 2015-05-17 20:48:38+0800(1 rows)cqlsh:testspace> SELECT * FROM test_timestamp WHERE id=2 and date>'2015-05-17 20:45:00' and date<'2015-05-17 20:50:00'; id | date----+-------------------------- 2 | 2015-05-17 20:48:38+0800(1 rows)
值得注意的一个问题,使用cassandra python driver
#~/usr/bin/pythonimport timefrom cassandra.cluster import Clustercluster = Cluster()session = cluster.connect()session.execute('USE testspace')cql = "SELECT * FROM test_timestamp WHERE date > '2015-05-17 20:45:00+0800' AND date < '2015-05-17 20:50:00+0800' ALLOW FILTERING"print cql res = session.execute(cql)print res for idx, date in res: print idx, date print idx, int(time.mktime(date.timetuple())) print type(idx), type(date)
output
SELECT * FROM test_timestamp WHERE date > '2015-05-17 20:45:00+0800' AND date < '2015-05-17 20:50:00+0800' ALLOW FILTERING[Row(id=2, date=datetime.datetime(2015, 5, 17, 12, 48, 38))]2 2015-05-17 12:48:382 1431838118<type 'int'> <type 'datetime.datetime'>
可见,cassandra中timestamp类型是带时区的,比如上面的例子中是东8区。但是python查询的结果就有问题了,应该是标准时区的,因此date time的数值从20:48变为了12:48,慢了8个小时。关于datetime与不同时区python中肯定有相关函数处理的。
在cassandra中存储时间还是用int的10位timestamp方便,缺点是不够直观。但存储1431838118肯定比存储datetime格式的'2015-05-17 20:45:00'要节约空间吧。
INT_MAX 2147483647 2038-01-19 11:14:07, 可见用int来存储timestamp足够了,2038年还早。
0 0
- Cassandra的timestamp类型
- Cassandra的timestamp类型
- MySql的timestamp类型
- mysql的timestamp类型
- oracle的timestamp类型使用
- oracle的timestamp类型使用
- Timestamp时间类型的比较
- 浅谈 PostgreSQL 的 timestamp 类型
- 浅谈 PostgreSQL 的 timestamp 类型
- cassandra关于集合类型的操作
- cassandra关于集合类型的操作
- cassandra关于集合类型的操作
- timestamp 类型
- timestamp类型
- mysqldump遇到的timestamp类型的问题
- mysql中timestamp类型的应用释疑
- MYSQL中TIMESTAMP类型的默认值
- MySql中的Timestamp类型字段的使用
- response对象
- 大数据:一场改变我们如何生活、工作和思考的革命(让数据说话)
- leetcode 第124题 Binary Tree Maximum Path Sum
- Java内存管理第二篇 - 内存的分配
- 《中国程序化广告技术生态图》2015年三月号更新发布
- Cassandra的timestamp类型
- swift皮筋弹动发射飞机ios源码
- 网站倒计时功能
- 关于NOLOCK的用法
- Java内存管理第三篇 - 内存可能产生的问题
- 介绍Redis
- Linux下启动指定的Oracle数据库,并解锁/锁定指定数据库账户
- 使用 udev 高效、动态地管理 Linux 设备文件
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践