【问题标题】:CiickHouse Insert unique dataClickHouse 插入唯一数据
【发布时间】:2021-11-04 12:56:17
【问题描述】:

我想插入不重复的数据。这些字段的三元组应该是唯一的ticker, kline_type, dateTime

发现我应该使用 ReplacingMergeTree https://clickhouse.com/docs/ru/engines/table-engines/mergetree-family/replacingmergetree/

我正在努力

import clickhouse_driver

def prepare_table():
    client = clickhouse_driver.Client.from_url(f'clickhouse://default:{os.getenv("CLICK_PASSWORD")}@localhost:9000/crypto_exchange')
    
    # field names from binance API
    client.execute('''
CREATE TABLE IF NOT EXISTS historical_data_binance
(
    dateTime DateTime,
    closeTime Int64,
    open Float64,
    high Float64,
    low Float64,
    close Float64,
    volume Float64,
    kline_type String,
    ticker String
) ENGINE = ReplacingMergeTree
ORDER BY (ticker, kline_type, dateTime)
''')
    return client

prepare_table()

但我认为我的解决方案不起作用,因为我看到重复:

  2021-11-04 11:00:00 │ 1636027199999 │ 61894.82 │  62188.78 │ 60866.46 │ 61444.74 │ 20.158382 │ 1h         │ BTCUSDT │
│ 2021-11-04 12:00:00 │ 1636030799999 │ 61420.86 │  61698.74 │ 58754.41 │ 61621.01 │ 15.721483 │ 1h         │ BTCUSDT │
└─────────────────────┴───────────────┴──────────┴───────────┴──────────┴──────────┴───────────┴────────────┴─────────┘
┌────────────dateTime─┬─────closeTime─┬─────open─┬─────high─┬──────low─┬────close─┬────volume─┬─kline_type─┬─ticker──┐
│ 2021-11-04 11:00:00 │ 1636027199999 │ 61894.82 │ 62188.78 │ 60866.46 │ 61444.74 │ 20.158382 │ 1h         │ BTCUSDT 

插入数据的正确方法是什么?

【问题讨论】:

标签: python clickhouse


【解决方案1】:
  1. ReplaceingMergeTree 不保证没有重复项。您需要以某种方式在选择中即时进行最终的重复数据删除。

  2. 重复数据删除是合并的副产品。

create table testD ( Key Int64, ver UInt64, Value String) 
Engine=ReplacingMergeTree(ver) order by Key;

insert into testD values (1, 1, '1');
insert into testD values (1, 2, '2');

SELECT * FROM testD
┌─Key─┬─ver─┬─Value─┐
│   1 │   2 │ 2     │
└─────┴─────┴───────┘
┌─Key─┬─ver─┬─Value─┐
│   1 │   1 │ 1     │
└─────┴─────┴───────┘

1)
a) SELECT * FROM testD final
┌─Key─┬─ver─┬─Value─┐
│   1 │   2 │ 2     │
└─────┴─────┴───────┘

b) SELECT key, argMax(Value, ts) FROM testD group by key;
┌─Key─┬─argMax(Value, ver)─┐
│   1 │ 2                  │
└─────┴────────────────────┘

c) SELECT Key, Value FROM testD order by Key, Value desc limit 1 by Key;
┌─Key─┬─Value─┐
│   1 │ 2     │
└─────┴───────┘

2)

optimize table testD final; -- initiate unplanned merge

SELECT * FROM testD;
┌─Key─┬─ver─┬─Value─┐
│   1 │   2 │ 2     │
└─────┴─────┴───────┘

【讨论】:

猜你喜欢
  • 2019-04-17
  • 2019-03-04
  • 1970-01-01
  • 2021-03-31
  • 2019-01-31
  • 2021-02-19
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多