原文链接
1.http://blog.hubwiz.com/2021/04/18/streaming-ethereum-data-to-questdb/
2.https://medium.com/geekculture/streaming-ethereum-on-chain-data-to-questdb-ea6b51d990ab
Infura是由Consensys提供支持的开发平台,具有免费层(每天10万个请求),可以从 以太坊主网和Testnets提取数据。注册一个免费帐户并在以太坊下创建一个新项目:
记得抄下来Mainnet的HTTPS端点,格式为 https://mainnet.infura.io/v3/<your-project-id>
。
1、QuestDB的schema设计
我们将用于流以太坊数据的ETL脚本提供以下链上信息:
- 区块
- 合约
- 日志
- 代币转移
- 代币
- 交易跟踪
- 交易次数
为简单起见,在此示例中,我们仅抽取区块和代币转账数据,但是所有可用链上数据的模式都位于 ethereum-etl-postgres / schema下(需要将Postgres数据类型转换为Java数据类型)。该数据将 在QuestDB中建立索引,以利用其高性能的时间序列数据。
使用Web控制台和Postgres端点启动QuestDB:
$ docker run -p 9000:9000 -p 8812:8812 questdb/questdb
导航至Web控制台,localhost:9000然后创建blocks表:
create table blocks
(
timestamp string,
number bigint,
hash string,
parent_hash string,
nonce string,
sha3_uncles string,
logs_bloom string,
transactions_root string,
state_root string,
receipts_root string,
miner symbol,
difficulty long,
total_difficulty long,
size long,
extra_data string,
gas_limit long,
gas_used long,
transaction_count long
);
以及token_transfer表:
create table token_transfers
(
token_address symbol,
from_address symbol,
to_address symbol,
value float,
transaction_hash string,
log_index long,
block_timestamp string,
block_number long,
block_hash string
);
刷新表列表后,应该可以看到两个表都已经有数据填充:
现在我们准备将以太坊数据流式传输到我们的数据库。
2、抽取以太坊链上数据
Blockchain-ETL提供了一个Python脚本,可从Infura中提取数据并将其流式传输到Google Pub / Sub或Postgres。 首先,我们需要先安装ethereumetl[streaming]:
$ pip3 install "ethereum-etl[streaming]"
现在,我们需要指定QuestDB凭证以及Infura API详细信息。Postgres输出采用以下形式,
postgresql+pg8000://<user>:<password>@<host>:<port>/<database>
并且API URL通过–provider-uri参数进行指定。我们还可以指定start-block参数(或者从一开始就省略该参数 以下载所有以太坊数据),以及我们感兴趣的数据架构:
$ ethereumetl stream --start-block 600000 -e block,token_transfer \
--output postgresql+pg8000://admin:quest@localhost:8812/qdb \
--provider-uri https://mainnet.infura.io/v3/<my-project-id>
当它开始抽取数据时,你可以在QuestDB中看到索引过程:
该脚本会自动将最后处理的块存储在last_synced_block.txt文件中。如果要稍后运行脚本,则可以删除 脚本中的–start-block标志
3、数据利用与改进方向
QuestDB提供了一些内置的可视化功能。例如,如果你希望按矿工查看gas_used,则可以进入Chart并将其 设置为以下内容:
以太坊ETL脚本当前以RFC3339格式存储时间戳数据,而QuestDB则采用从Unix Epoch开始的有符号偏移量。 这就是为什么我们的表结构为时间序列字段使用字符串而不是日期或时间戳的原因。为了获得更高的性能, 我们可以先修改ETL脚本,或者先将其流式传输到Cloud Pub / Sub,然后转换数据,然后再流式传输到QuestDB。 另外,我们可以在提取数据后使用QuestDB的to_timestamp()功能来修改表。例如,要转换blocks表为 QuestDB理解的正确时间戳,我们可以执行以下操作:
SELECT to_timestamp(timestamp, 'yyyy-MM-dd HH:mm:ss')
AS time, number, miner, difficulty, size, gas_limit, gas_used
FROM (blocks
ORDER BY timestamp);
最后,如果你要抽取合约或代币数据,则需要先将function_sighashes字段展开,因为QuestDB本身还不 支持Array类型。