前两篇文章:Kaggle上比特币链上的数据集——使用GoogleBigQueryAPI处理比特币数据(1)总结kaggle上kernal入门代码使用SQL获取bigquery比特币数据——使用GoogleBigQueryAPI处理比特币数据(2)使用最简单的SQL获取原始数据ErrorSQL写了一个统计比特币交易输入的查询和来自google.cloud的输出数量importbigqueryclient=bigquery.Client()query="SELECTSUM(output_count),COUNT(1)AStx_countFROM`bigquery-public-data.bitcoin_blockchain.transactions`astxGROUPBYoutput_count"r=client.query(query)df=r.to_dataframe()resulterror:BadRequest:400Unrecognizedname:output_countat[1:12]意味着没有这样的列?于是又回到数据集首页查看,发现了这一栏目。获取表模式参考notebookdataset_ref=client.dataset("bitcoin_blockchain",project="bigquery-public-data")dataset=client.get_dataset(dataset_ref)#打印并查看数据集中的所有表fortableinclient.list_tables(dataset):print(table.table_id)blockstransactions看这里不对,这个数据集应该有4张表,但是这里只有两张,继续看交易表table_ref=dataset_ref.table("transactions")table=client的内容。get_table(table_ref)forcolintable.schema:print(col)print()SchemaField('timestamp','INTEGER','NULLABLE',None,())SchemaField('transaction_id','STRING','NULLABLE',无,())SchemaField('输入','RECORD','重复',无,(SchemaField('input_script_bytes','BYTES','NULLABLE',无,()),SchemaField('input_script_string','STRING','NULLABLE',None,()),SchemaField('input_script_string_error','STRING','NULLABLE',None,()),SchemaField('input_sequence_number','INTEGER','NULLABLE',无,()),科学hemaField('input_pubkey_base58','STRING','NULLABLE',None,()),SchemaField('input_pubkey_base58_error','STRING','NULLABLE',None,())))SchemaField('outputs','RECORD','REPEATED',None,(SchemaField('output_satoshis','INTEGER','NULLABLE',无,()),SchemaField('output_script_bytes','BYTES','NULLABLE',None,()),SchemaField('output_script_string','STRING','NULLABLE',None,()),SchemaField('output_script_string_error','STRING','NULLABLE',无,()),SchemaField('output_pubkey_base58','STRING','NULLABLE',无,()),SchemaField('output_pubkey_base58_error','STRING','NULLABLE',无,())))SchemaField('block_id','STRING','NULLABLE',None,())SchemaField('previous_block','STRING','NULLABLE',None,())SchemaField('merkle_root','STRING','NULLABLE',None,())SchemaField('nonce','INTEGER','NULLABLE',None,())SchemaField('version','INTEGER','NULLABLE',None,())SchemaField('work_terahash','INTEGER','NULLABLE',None,())SchemaField('work_error','STRING','NULLABLE',None,())还真的没有output_count获取一条数据r1=client('SELECT*FROM`bigquery-public-data.bitcoin_blockchain.transactions`WHEREtransaction_id="4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"')df=r1.to_dataframe()只返回第111列的数据()1dfarticlearesha的结果不同的。事实证明,bigquery有两个不同的比特币数据集。上一篇文章中用到的是:bigquery-public-data.crypto_bitcoin.transactions数据集主页上面提到的错误代码中用到了这个。bigquery-public-data.bitcoin_blockchain.transactions是在数据集主页推荐的官方内核中使用的。两个数据集无论是表的数量还是表的结构都不同。所以最好使用第一个具有可视化效果的最终代码fromgoogle.cloudimportbigqueryclient=bigquery.Client()query="SELECToutput_count,COUNT(1)AStx_countFROM`bigquery-public-data.crypto_bitcoin.transactions`astxGROUPBYoutput_countORDERBYoutput_count"r=client.query(query)df=r.to_dataframe()r.total_bytes_processed/(1024**3)3.7562014535069466dfs=df['tx_count'].sum()s504172778这个数据集有500百万笔交易df['tx_count'][0]/s*100,df['tx_count'][1]/s*100,df['tx_count'][2]/s*100,(11.770812822424935,78.09005467566121,5.974420340480977)output_count为1、2、3的交易占比分别为11.77%、78.09%、5.97%。数据长尾现象明显,90%以上的交易集中在1-3。所以只需绘制前10行的条形图并查看df[:10].set_index('output_count').plot.bar()欢迎来到我的博客:https://codeplot.top/MyBlogBitcoin类别:https://codeplot.top/tags/比特币/
