- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这可以用传统方式完成,但如果我要使用 Cassandra DB,是否有一种简单/快速且灵活的方法将 csv 作为一组键值对添加到数据库?
能够通过 CSV 文件添加时间序列数据是我的首要要求。我可以切换到任何其他数据库,例如 mongodb、rike,如果在那里方便的话..
最佳答案
编辑 2 2017 年 12 月 2 日
请使用端口 9042。Cassandra 访问已更改为 CQL,默认端口为 9042,Thrift 的默认端口为 9160。
编辑 1
有一种更好的方法可以做到这一点,无需任何编码。看看这个答案https://stackoverflow.com/a/18110080/298455
但是,如果您想要进行预处理或自定义某些内容,您可能需要自己进行。这是一个冗长的方法:
<小时/>创建列族。
cqlsh> create keyspace mykeyspace
with strategy_class = 'SimpleStrategy'
and strategy_options:replication_factor = 1;
cqlsh> use mykeyspace;
cqlsh:mykeyspace> create table stackoverflow_question
(id text primary key, name text, class text);
假设您的 CSV 如下所示:
$ cat data.csv
id,name,class
1,hello,10
2,world,20
编写一个简单的 Python 代码来读取该文件并将其转储到您的 CF 中。像这样的事情:
import csv
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
pool = ConnectionPool('mykeyspace', ['localhost:9160'])
cf = ColumnFamily(pool, "stackoverflow_question")
with open('data.csv', 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print str(row)
key = row['id']
del row['id']
cf.insert(key, row)
pool.dispose()
执行此操作:
$ python loadcsv.py
{'class': '10', 'id': '1', 'name': 'hello'}
{'class': '20', 'id': '2', 'name': 'world'}
查看数据:
cqlsh:mykeyspace> select * from stackoverflow_question;
id | class | name
----+-------+-------
2 | 20 | world
1 | 10 | hello
另请参阅:
a.谨防DictReader
b.看Pycassa
C。 Google 将现有 CSV 加载器添加到 Cassandra。我想是有的。
d.使用 CQL 驱动程序可能有更简单的方法,我不知道。
e.使用适当的数据类型。我只是将它们全部包装成文本。不好。
HTH
<小时/>我没有看到时间序列要求。这是处理时间序列的方法。
这是您的数据
$ cat data.csv
id,1383799600,1383799601,1383799605,1383799621,1383799714
1,sensor-on,sensor-ready,flow-out,flow-interrupt,sensor-killAll
创建传统的宽行。 (CQL 建议不要使用 COMPACT STORAGE ,但这只是为了让您快速前进。)
cqlsh:mykeyspace> create table timeseries
(id text, timestamp text, data text, primary key (id, timestamp))
with compact storage;
这是修改后的代码:
import csv
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
pool = ConnectionPool('mykeyspace', ['localhost:9160'])
cf = ColumnFamily(pool, "timeseries")
with open('data.csv', 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print str(row)
key = row['id']
del row['id']
for (timestamp, data) in row.iteritems():
cf.insert(key, {timestamp: data})
pool.dispose()
这是您的时间序列
cqlsh:mykeyspace> select * from timeseries;
id | timestamp | data
----+------------+----------------
1 | 1383799600 | sensor-on
1 | 1383799601 | sensor-ready
1 | 1383799605 | flow-out
1 | 1383799621 | flow-interrupt
1 | 1383799714 | sensor-killAll
关于cassandra - 如何将 csv 添加到 cassandra 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827690/
我们有 2 个 cassandra 集群,第一个有旧数据,第二个有新数据。 现在我们想要将旧数据从第一个集群移动或复制到第二个集群。什么是最好的方法来做到这一点以及如何做到这一点? 我们正在使用 DS
我正在考虑安装 OpsCenter 来监控我们在 RackSpace VM 上运行的 24 节点 Cassandra 集群。过去我听说 OpsCenter 减慢了集群速度。我有点担心 OpsCente
假设我有一个复制因子(RF)= 2 的 2 节点集群。 我使用一致性 2 触发插入。当客户端等待响应时,Cassandra 开始写入这 2 个节点。中间一个节点失败,无法完成写入,而另一节点上的写入成
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足 Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以
我在 Cassandra 中有一个表,其中我用 1000 多个条目填充了一些行(每行有 10000 多列)。行中的条目更新非常频繁,基本上只是一个字段(它是一个整数)被更新为不同的值。列的所有其他值保
当Cassandra端有“掉落的突变”时,它是否向调用客户端返回相应的失败?或者即使在服务器端丢弃相应的突变并导致数据丢失,它总是成功响应调用事务的调用客户端? 在一个特定实例中,当我们的 TPS 约
我有一个 Multi-Tenancy 应用程序,其中 tenantId 将成为每个查询的一部分,因此我将其放入所有表的分区键中。 例子: CREATE TABLE users { tenantId t
根据 Datastax 文档,在 Cassandra 中先读后写是一种反模式。 每当我们在 CQLSH 中使用 UPDATE 或使用 Datastax 驱动程序来设置几列(带有 IF 和集合更新)时,
是否有命令或任何方式可以知道 Cassandra 的哪些节点上存储了哪些数据? 我对 Cassandra 很陌生,在谷歌上搜索这个问题并没有多少运气。 谢谢! 最佳答案 您可以使用 nodetool
我们有一个包含 1500 万条记录的表,而我们的表是一个 10 节点的 cassandra 集群。我们有一列有接近 20 个可重复值。是否建议在此列上建立二级索引? 最佳答案 假设在该列上完全均匀分布
Cassandra 发布了它的 technical limitations但没有提到允许的最大列数。是否有最大列数?我需要存储 400 多个字段。这在 Cassandra 中可能吗? 最佳答案 每行的
我想知道当表中有多个非 PK 列时会发生什么。我读过这个例子: http://johnsanda.blogspot.co.uk/2012/10/why-i-am-ready-to-move-to-cq
我有两个关于 Cassandra 查询结果的问题。 当我在 Cassandra 中对表进行“完全”选择(即 select * from table )时,是否保证结果将按分区标记的递增顺序返回? 例如
我无法为 Cassandra 设置 Hector。我已经浏览了 documentation和 Cassandra wiki .这些文档的问题在于,那里的很多信息都已经过时或过时(或者我缺乏知识)。无论
我正在使用 DataStax Enterprise 中 cassandra 中提供的压力测试。如果有人知道的话,我也想要一些关于它和 cassandra 的信息。 - 首先,压力测试使用哪些节点?我的
当我在 CQL 中创建表时,列的顺序是否必须精确 不是 在主键和 中不是 聚类列: CREATE TABLE user ( a ascii, b ascii, c ascii,
我有一张如下表: CREATE TABLE tab( categoryid text, id text, name text, author text, des
我正在尝试学习 Cassandra,但对术语感到困惑。 很多情况下它表示该行存储键/值对。 但是,当我定义一个表时,它更像是声明一个 SQL 表,即;您创建一个表并指定列名和数据类型。 谁能澄清一下?
如何对 cassandra 数据实现审计? 我正在寻找一个开源选项。 cassandra 是否有任何有助于审计的功能? 我可以使用触发器将记录记录到表中吗?我关注了 Triggers示例并且能够将记录
我遇到了一个问题“me.prettyprint.hector.api.exceptions.HUnavailableException:: 可能没有足够的副本来处理一致性级别。”当我有 RF=1 时,
我是一名优秀的程序员,十分优秀!