gpt4 book ai didi

cassandra - 如何将 csv 添加到 cassandra 数据库?

转载 作者:行者123 更新时间:2023-12-02 22:36:46 26 4
gpt4 key购买 nike

我知道这可以用传统方式完成,但如果我要使用 Cassandra DB,是否有一种简单/快速且灵活的方法将 csv 作为一组键值对添加到数据库?

能够通过 CSV 文件添加时间序列数据是我的首要要求。我可以切换到任何其他数据库,例如 mongodb、rike,如果在那里方便的话..

最佳答案

编辑 2 2017 年 12 月 2 日
请使用端口 9042。Cassandra 访问已更改为 CQL,默认端口为 9042,Thrift 的默认端口为 9160。

编辑 1
有一种更好的方法可以做到这一点,无需任何编码。看看这个答案https://stackoverflow.com/a/18110080/298455

但是,如果您想要进行预处理或自定义某些内容,您可能需要自己进行。这是一个冗长的方法:

<小时/>
  1. 创建列族。

    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
  2. 编写一个简单的 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()
  3. 执行此操作:

    $ python loadcsv.py 
    {'class': '10', 'id': '1', 'name': 'hello'}
    {'class': '20', 'id': '2', 'name': 'world'}
  4. 查看数据:

    cqlsh:mykeyspace> select * from stackoverflow_question;
    id | class | name
    ----+-------+-------
    2 | 20 | world
    1 | 10 | hello
  5. 另请参阅:

    a.谨防DictReader
    b.看Pycassa
    C。 Google 将现有 CSV 加载器添加到 Cassandra。我想是有的。
    d.使用 CQL 驱动程序可能有更简单的方法,我不知道。
    e.使用适当的数据类型。我只是将它们全部包装成文本。不好。

HTH

<小时/>

我没有看到时间序列要求。这是处理时间序列的方法。

  1. 这是您的数据

    $ cat data.csv
    id,1383799600,1383799601,1383799605,1383799621,1383799714
    1,sensor-on,sensor-ready,flow-out,flow-interrupt,sensor-killAll
  2. 创建传统的宽行。 (CQL 建议不要使用 COMPACT STORAGE ,但这只是为了让您快速前进。)

    cqlsh:mykeyspace> create table timeseries 
    (id text, timestamp text, data text, primary key (id, timestamp))
    with compact storage;
  3. 这是修改后的代码:

    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()
  4. 这是您的时间序列

    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/

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