1. 程式人生 > 實用技巧 >Kaggle-SQL(1)

Kaggle-SQL(1)

Getting-started-with-sql-and-bigquery

教程

結構化查詢語言(SQL)是資料庫使用的程式語言,它是任何資料科學家的一項重要技能。 在本課程中,您將使用BigQuery來提高SQL技能,BigQuery是一種Web服務,可用於將SQL應用於龐大的資料集。
在本課程中,您將學習訪問和檢查BigQuery資料集的基礎知識。 在您掌握了這些基礎知識之後,我們將再次建立您的SQL技能。

Your first BigQuery commands

要使用BigQuery,我們將在下面匯入Python包

from google.cloud import bigquery

工作流程的第一步是建立一個Client物件。 您將很快看到,此Client物件將在從BigQuery資料集中檢索資訊中發揮核心作用。

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

我們將使用Hacker News(一個專注於電腦科學和網路安全新聞的網站)上的帖子資料集。
在BigQuery中,每個資料集都包含在相應的專案中。 在這種情況下,我們的hacker_news資料集包含在bigquery-public-data專案中。 要訪問資料集,
我們首先使用dataset()方法構造對資料集的引用。接下來,我們使用get_dataset()方法以及剛剛構造的引用來獲取資料集。

# Construct a reference to the "hacker_news" dataset
dataset_ref = client.dataset("hacker_news", project="bigquery-public-data") # API request - fetch the dataset dataset = client.get_dataset(dataset_ref)

每個資料集都只是表的集合。 您可以將資料集視為包含多個表(均由行和列組成)的電子表格檔案。我們使用list_tables()方法列出資料集中的表。

# List all the tables in the "hacker_news" dataset
tables = list(client.list_tables(dataset))

# Print names of all tables in the dataset (there are four!) for table in tables: print(table.table_id)

output

comments
full
full_201510
stories

與獲取資料集相似,我們可以獲取表。 在下面的程式碼單元中,我們在hacker_news資料集中獲取完整表。

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

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

Table schema

表的結構稱為其架構。 我們需要了解表的架構以有效地提取所需的資料。
在此示例中,我們將調查上面獲取的完整表。

# Print information on all the columns in the "full" table in the "hacker_news" dataset
table.schema

output

[SchemaField('title', 'STRING', 'NULLABLE', 'Story title', ()),
 SchemaField('url', 'STRING', 'NULLABLE', 'Story url', ()),
 SchemaField('text', 'STRING', 'NULLABLE', 'Story or comment text', ()),
 SchemaField('dead', 'BOOLEAN', 'NULLABLE', 'Is dead?', ()),
 SchemaField('by', 'STRING', 'NULLABLE', "The username of the item's author.", ()),
 SchemaField('score', 'INTEGER', 'NULLABLE', 'Story score', ()),
 SchemaField('time', 'INTEGER', 'NULLABLE', 'Unix time', ()),
 SchemaField('timestamp', 'TIMESTAMP', 'NULLABLE', 'Timestamp for the unix time', ()),
 SchemaField('type', 'STRING', 'NULLABLE', 'Type of details (comment, comment_ranking, poll, story, job, pollopt)', ()),
 SchemaField('id', 'INTEGER', 'NULLABLE', "The item's unique id.", ()),
 SchemaField('parent', 'INTEGER', 'NULLABLE', 'Parent comment ID', ()),
 SchemaField('descendants', 'INTEGER', 'NULLABLE', 'Number of story or poll descendants', ()),
 SchemaField('ranking', 'INTEGER', 'NULLABLE', 'Comment ranking', ()),
 SchemaField('deleted', 'BOOLEAN', 'NULLABLE', 'Is deleted?', ())]

每個SchemaField都會告訴我們一個特定的列(也稱為欄位)。 按順序,資訊為:

列名
列中的欄位型別(或資料型別)
列的模式(“ NULLABLE”表示列允許NULL值,並且是預設值)
該列中資料的描述

比如 SchemaField('by', 'STRING', 'NULLABLE', "The username of the item's author.", ()) 告訴我們:這個列名字為"by",資料為字串型,允許為空,這個列儲存了作者的名字

我們可以使用list_rows()方法來檢查整個表的前五行,以確保這是正確的。 (有時資料庫的描述已經過時,因此最好檢查一下。)這將返回一個BigQuery RowIterator物件,該物件可以使用to_dataframe()方法快速轉換為pandas DataFrame。

# Preview the first five lines of the "full" table
client.list_rows(table, max_results=5).to_dataframe()

list_rows()方法還將使我們僅檢視特定列中的資訊。 例如,如果我們要檢視by列中的前五個條目,則可以這樣做:

# Preview the first five entries in the "by" column of the "full" table
client.list_rows(table, selected_fields=table.schema[:1], max_results=5).to_dataframe()

Disclaimer

在進行編碼練習之前,對已經知道一些SQL的人快速宣告一下:

每個Kaggle使用者可以每30天免費掃描5TB。 達到該限制後,您將不得不等待重置。

到目前為止,您所看到的命令將不需要該限制的有意義的一部分。 但是某些BiqQuery資料集非常龐大。 因此,如果您已經瞭解SQL,請等待執行SELECT查詢,直到您瞭解如何有效使用分配。 如果您像大多數閱讀此書的人一樣,則還不知道如何編寫這些查詢,因此您無需擔心此免責宣告。