簡體   English   中英

帶有Python的Google Cloud Dataflow

[英]Google Cloud Dataflow with Python

在將數據插入BigQuery時,嘗試實現示例的簡單形式時出現錯誤

這是代碼

from __future__ import absolute_import
import argparse
import logging
import re
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions


class DataIngestion:
    def parse_method(self, string_input):
        values = re.split(",",re.sub('\r\n', '', re.sub(u'"', '', string_input)))
        row = dict(zip('Mensaje',values))
        return row



def run(argv=None):
    """The main function which creates the pipeline and runs it."""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--input', dest='input', required=False,
        help='Input file to read.  This can be a local file or '
             'a file in a Google Storage Bucket.',
        default='C:\XXXX\prueba.csv')

    parser.add_argument('--output', dest='output', required=False,
                        help='Output BQ table to write results to.',
                        default='PruebasIoT.TablaIoT')

    known_args, pipeline_args = parser.parse_known_args(argv)

    data_ingestion = DataIngestion()

    p = beam.Pipeline(options=PipelineOptions(pipeline_args))

    (p
     | 'Read from a File' >> beam.io.ReadFromText(known_args.input,
                                                  skip_header_lines=1)

     | 'String To BigQuery Row' >> beam.Map(lambda s:
                                            data_ingestion.parse_method(s))
     | 'Write to BigQuery' >> beam.io.Write(
                beam.io.BigQuerySink
                    (
                    known_args.output,
                    schema='Mensaje:STRING'
                 )
            )
     )
    p.run().wait_until_finish()


if __name__ == '__main__':
    #  logging.getLogger().setLevel(logging.INFO)
    run()

這是錯誤:

RuntimeError: Could not successfully insert rows to BigQuery table [XXX]. Errors: [<InsertErrorsValueListEntry
 errors: [<ErrorProto
 debugInfo: u''
 location: u'm'
 message: u'no such field.'
 reason: u'invalid'>]
 index: 0>, <InsertErrorsValueListEntry
 errors: [<ErrorProto
 debugInfo: u''
 location: u'm'
 message: u'no such field.'
 reason: u'invalid'>]
 index: 1>]

我是python的新手,也許解決方法很簡單,但是我該怎么做呢?

可以將String中的單個字符串傳遞給BigQuery Row而不是

'String To BigQuery Row' >> beam.Map(lambda s:
                                        data_ingestion.parse_method(s))

與使用csv文件相比,這是一種比使用csv文件更好地啟動的更簡單方法。

我了解您有一個輸入CSV文件,該文件的單列格式為:

Message
This is a message
This is another message
I am writing to BQ

如果我的理解是正確的,則無需使用parse_method()方法,因為正如您共享的樣本所述 ,這只是一個將CSV值映射到字典的輔助方法( beam.io.BigQuerySink接受了beam.io.BigQuerySink )。

然后,您可以簡單地執行以下操作:

p = beam.Pipeline(options=PipelineOptions(pipeline_args))

(p
 | 'Read from a File' >> beam.io.ReadFromText(known_args.input, skip_header_lines=1)
 | 'String To BigQuery Row' >> beam.Map(lambda s: dict(Message = s))
 | 'Write to BigQuery' >> beam.io.Write(
    beam.io.BigQuerySink(known_args.output, schema='Message:STRING')))

p.run().wait_until_finish()

請注意,唯一相關的區別是“ 字符串到BigQuery行 ”的映射不再需要復雜的方法,它要做的只是創建一個Python字典,例如{Message: "This is a message"} ,其中Message是名稱BQ表中該列的位置。 在此映射中, sbeam.io.ReadFromText轉換中讀取的每個String元素,並且我們應用了lambda函數

要解決使用每行只有一個值的CSV文件的問題,我必須使用以下命令:

    values = re.split(",",re.sub('\r\n', '', re.sub(u'"', '', string_input)))
    row = dict(zip(('Name',),values))

我不知道為什么必須在“名稱”后面加上“,”,但如果我不這樣做,則dict(zip(...無法正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM