2017-06-13 61 views
0

我正在研究Google BigQuery的python客戶端。 我正在編寫一個程序來自動創建/導出表格。python - 將字符串分配給字段對象

一切工作正常,但有一個小問題 - 我想借此在表的架構作爲來自用戶的輸入。

下面是表模式目前分配:

table.schema = (
    bigquery.SchemaField('Name', 'STRING'), 
    bigquery.SchemaField('Gender', 'STRING'), 
    bigquery.SchemaField('Frequency', 'INTEGER') 
) 

它在代碼硬編碼。

我寫的代碼來處理用戶輸入並將其轉換成上述格式。

什麼我的代碼返回一個字符串 - bq_schema - 它看起來像:

bigquery.SchemaField(Name, STRING),bigquery.SchemaField(Gender, STRING),bigquery.SchemaField(Frequency, INTEGER) 

現在,當我嘗試這個字符串分配到表模式,

table.schema = (bq_schema) 

我得到一個錯誤,說明Schema items must be fields

那麼,如何使根據用戶輸入的表架構的動態?

編輯:按照要求,在這裏用戶輸入轉換爲字符串代碼:

s_raw = raw_input('\n\nEnter the schema for the table in the following format- fieldName:TYPE, anotherField:TYPE\n') 
s = s_raw.split(',') 
schema = [] 

for obj in s: 
    temp = obj.split(':') 
    schema.append(temp[0]) 
    schema.append(temp[1]) 

bq_schema = '' 

for i in range(0, len(schema), 2): 
    bq_schema+=('bigquery.SchemaField(\'{}\', \'{}\'),'.format(schema[i], schema[i+1])) 
+0

你的代碼的返回不能是字符串確實如此。由於定義了架構'setter屬性':https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/bigquery/google/cloud/bigquery/table.py#L106,您可以看到它評估的是否所有字段都是'SchemaField'的實例,所以你沒有別的選擇,然後返回'SchemaField'值。你有沒有一個例子說明系統中的用戶輸入是什麼?你介意告訴我們你如何將這個輸入轉換成你之前提到的字符串? –

+0

感謝您的幫助! 我已經在OP中添加了代碼 – noobcoder

回答

0

問題:什麼我的代碼返回一個字符串 - bq_schema

你是什麼面臨的是__str__bigquery對象的重新貶低。

更改如下:

bq_schema = [] 
for i in range(0, len(schema), 2): 
    bq_schema.append(SchemaField(schema[i], schema[i+1])) 
1

來定義字段的BigQuery中的行爲方式,你基本上需要3 inputsnametypemode

處理輸入模式時可能發現的一個問題是管理類型爲RECORD的字段,因爲每個字段都在其中定義了其他字段。

既然如此,那就有點困難的用戶,讓您在什麼架構設置他的工作與字符串。

我建議你因此要做的就是接收到類似與通信模式輸入數據的JSON。例如,您可能會收到以下輸入:

[{"name": "sku", "type": "INT64", "mode": "NULLABLE"}, 
    {"name": "name", "type": "STRING", "mode": "NULLABLE"}, 
    {"name": "type", "type": "STRING", "mode": "NULLABLE"}, 
    {"fields": 
    [{"name": "id", "type": "STRING", "mode": "NULLABLE"}, {"name": "name", "type": "STRING", "mode": "NULLABLE"}], 
    "name": "category", "type": "RECORD", "mode": "REPEATED"}, 
    {"name": "description", "type": "STRING", "mode": "NULLABLE"}, 
    {"name": "manufacturer", "type": "STRING", "mode": "NULLABLE"}] 

請注意,此JSON以直接方式完全定義架構。字段「category」的類型是「RECORD」,它包含每個子字段的模式定義,即「name」和「id」。

在Python中,你所要做的就是處理這個JSON輸入它們。此功能可能做的伎倆:

from google.cloud.bigquery import SchemaField 
def get_schema_from_json(input_json, res=[]): 
    if not input_json: 
     return res 
    cur = input_json.pop() 
    res.append(SchemaField(name=cur['name'], field_type=cur['type'], mode=cur['mode'], fields = None if 'fields' not in cur else get_schema_from_json(cur['fields'], []))) 
    return get_schema_from_json(input_json, res) 

然後你纔可以提取模式,像這樣:

table.schema = get_schema_from_json(user_json_input_data)