我的博客

Kaggle 上的比特币链上数据集 - 使用 Google Big Query API 处理比特币数据

目录
  1. Kaggle 简介
  2. 数据集
  3. 一个例子(使用 SQL 方法访问数据)
    1. 查询每日接收比特币地址数量
    2. 画图
    3. 查询每日交易数量

Kaggle 简介

Kaggle 是一个数据竞赛平台,2010 年创立,2017 年被谷歌收购。平台提供大量开放的数据集和免费的计算资源。只需要注册一个帐号就可以在线编写代码,分析数据。

数据集

数据集首页 https://www.kaggle.com/bigquery/bitcoin-blockchain/

目前有 700 多个 Kernels。介绍中说数据持续更新,目前来看更新到 2018 年 9 月。

比特币链上数据大小超过百 GB。在这里是通过 Google Big Query API 访问,而没有任何数据文件。所以这个数据集只能在线使用,而不能下载,但它们提供了数据抽取的代码(https://github.com/blockchain-etl/bitcoin-etl ),所以可以选择自己在本地创建这部分数据。据文档介绍,每个账户每月访问数据的上限是 5 TB。

数据一共有 4 张表:blocks、inputs、outputs、transactions。

一个例子(使用 SQL 方法访问数据)

代码来自这里,有改动(原代码由于库版本变化,无法执行),还略去一些次要内容。

查询每日接收比特币地址数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from google.cloud import bigquery
import pandas as pd

client = bigquery.Client()

# Query by Allen Day, GooglCloud Developer Advocate (https://medium.com/@allenday)
query = """
#standardSQL
SELECT
o.day,
COUNT(DISTINCT(o.output_key)) AS recipients
FROM (
SELECT
TIMESTAMP_MILLIS((timestamp - MOD(timestamp,
86400000))) AS day,
output.output_pubkey_base58 AS output_key
FROM
`bigquery-public-data.bitcoin_blockchain.transactions`,
UNNEST(outputs) AS output ) AS o
GROUP BY
day
ORDER BY
day
"""

query_job = client.query(query)

iterator = query_job.result(timeout=30)
rows = list(iterator)

# Transform the rows into a nice pandas dataframe
transactions = pd.DataFrame(data=[list(x.values()) for x in rows], columns=list(rows[0].keys()))

# Look at the first 10 headlines
transactions.head(10)

输出:

前 10 条记录

1
transactions.tail(10)

输出:

最后 10 条记录

画图

1
2
3
4
import matplotlib
from matplotlib import pyplot as plt
%matplotlib inline
plt.plot(transactions['day'], transactions['recipients'])

每日接收比特币地址数量

查询每日交易数量

代码来自这里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from google.cloud import bigquery

# Create a "Client" object
client = bigquery.Client()

# Construct a reference to the "crypto_bitcoin" dataset
dataset_ref = client.dataset("crypto_bitcoin", project="bigquery-public-data")

# API request - fetch the dataset
dataset = client.get_dataset(dataset_ref)

# Construct a reference to the "transactions" table
table_ref = dataset_ref.table("transactions")

# API request - fetch the table
table = client.get_table(table_ref)

# Preview the first five lines of the "transactions" table
client.list_rows(table, max_results=5).to_dataframe()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Query to select the number of transactions per date, sorted by date
query_with_CTE = """
WITH time AS
(
SELECT DATE(block_timestamp) AS trans_date
FROM `bigquery-public-data.crypto_bitcoin.transactions`
)
SELECT COUNT(1) AS transactions,
trans_date
FROM time
GROUP BY trans_date
ORDER BY trans_date
"""

# Set up the query (cancel the query if it would use too much of
# your quota, with the limit set to 10 GB)
safe_config = bigquery.QueryJobConfig(maximum_bytes_billed=10**10)
query_job = client.query(query_with_CTE, job_config=safe_config)

# API request - run the query, and convert the results to a pandas DataFrame
transactions_by_date = query_job.to_dataframe()

# Print the first five rows
transactions_by_date.head()

画图

1
transactions_by_date.set_index('trans_date').plot()

比特币每日交易数量

评论无需登录,可以匿名,欢迎评论!