You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@beam.apache.org by Juta Staes <ju...@ml6.eu> on 2019/03/20 12:45:58 UTC
Writing bytes to BigQuery with beam
Hi all,
I am working on porting beam to python 3 and discovered the following:
Current handling of bytes in bigquery IO:
When writing bytes to BQ , beam uses
https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API expects
byte values to be base-64 encoded*.
However when writing raw bytes they are currently never transformed to
base-64 encoded strings. This results in the following errors:
-
When writing b’abc’ in python 2 this results in actually writing
b'i\xb7' which is the same as base64.b64decode('abc='))
-
When writing b’abc’ in python 3 this results in “TypeError: b'abc' is
not JSON serializable”
-
When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
values are not JSON compliant”
-
When reading bytes from BQ they are currently returned as base-64
encoded strings rather then the raw bytes.
Example code:
https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
There is also another issue when writing base-64 encoded string to BQ. When
no schema is specified this results in “Invalid schema update. Field bytes
has changed type from BYTES to STRING”.
This error can be reproduced when uploading a file (directly in the BQ UI)
to a table with bytes and using schema autodetect.
Suggested solution:
I suggest to change BigQuery IO to handle the base-64 encoding as follows
to allow the user to read and write raw bytes in BQ
Writing data:
-
When a new table is created we use the provided schema to detect bytes
and handle the base-64 encoding accordingly
-
When data is written to an existing table we use the API to get the
schema of the table and handle the base-64 encoding accordingly. We also
pass the schema as argument to avoid the error from schema autodetect.
Reading data:
-
When reading data we also request the schema and handle the base-64
decoding accordingly to return raw bytes
What are your thoughts on this?
*I could not find this in the documentation of the API or in the
documentation of BigQuery itself which also expects base-64 encoded values.
I discovered this when uploading a file to BQ UI and getting an error:
"Could not decode base64 string to bytes."
--
[image: https://ml6.eu] <https://ml6.eu/>
* Juta Staes*
ML6 Gent
<https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
**** DISCLAIMER ****
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail by mistake and
delete this e-mail from your system. If you are not the intended recipient
you are notified that disclosing, copying, distributing or taking any
action in reliance on the contents of this information is strictly
prohibited.
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
Also, I filed https://issues.apache.org/jira/browse/BEAM-7346 to add more
tests to Go SDK and verify the consistency of BQ IO behavior w.r.t.
handling BYTES.
On Thu, May 16, 2019 at 4:42 PM Valentyn Tymofieiev <va...@google.com>
wrote:
>
> On Thu, May 16, 2019 at 1:12 PM Chamikara Jayalath <ch...@google.com>
> wrote:
>
>>
>>
>> On Wed, May 15, 2019 at 12:26 PM Valentyn Tymofieiev <va...@google.com>
>> wrote:
>>
>>> I took a closer look at BigQuery IO implementation in Beam SDK and
>>> Dataflow runner while reviewing a few PRs to address BEAM-6769, and I think
>>> we have to revise the course of action here.
>>>
>>> It turns out, that when we first added support for BYTES in Java
>>> BiqQuery IO, we designed the API with an expectation that:
>>> - On write path the user must pass base64-encoded bytes to the BQ IO.
>>> [0]
>>> - On read path BQ IO base64-encodes the output result, before serving it
>>> to the user. [1]
>>>
>>> When support for BigQuery was added to Python SDK and Dataflow runner,
>>> the runner authors preserved the behavior of treating bytes to be
>>> consistent with Java BQ IO - bytes must be base64-encoded by the user, and
>>> bytes from BQ IO returned by Dataflow Python runner are base64-encoded.
>>>
>>> Unfortunately, this behavior is not documented in public documentation
>>> or JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up
>>> until we added integration tests a few years down the road [5,6]. Thanks to
>>> these integration tests we discovered BEAM-6769.
>>>
>>> I don't have context why we made a decision to avoid handling raw bytes
>>> in Beam, however I think keeping consistent treatment of bytes across all
>>> SDKs and runners is important for a smooth user experience, especially so
>>> when a particular behavior is not documented well.
>>>
>>> This being said I suggest the following:
>>> 1. Let's keep the current expectation that Beam operates only on
>>> base64-encoded bytes in BQ IO. It may be reasonable to revise this
>>> expectation, but it is beyond the scope of BEAM-6769.
>>> 2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
>>> Chances are that if we had such documentation, we wouldn't have had to
>>> answer questions raised in this thread. Filed BEAM-7326 to track.
>>> 3. Let's revise Python BQ integration tests to clearly communicate that
>>> BQ IO expects base64-encoded bytes. Filed BEAM-7327 to track.
>>>
>>
>> +1 for documentation updates at least.
>> We probably will able to add support for writing both base64 encoded
>> bytes and row bytes without breaking backwards compatibility though. For
>> reading, we can add an option if we want to add support for automatically
>> decoding base64 encoded bytes.
>>
> Filed https://issues.apache.org/jira/browse/BEAM-7344 to consider this.
>
>>
>>> Coming back to the original message:
>>>
>>> When writing b’abc’ in python 2 this results in actually writing
>>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>>
>>> This is expected as Beam BQ IO expect users to base64-encode their bytes.
>>>
>>
>> Does BigQuery still store data as bytes (if the type of the corresponding
>> field is BYTES) ?
>>
>
> Yes, I believe BigQuery stores data as raw BYTES, but in the BQ UI / bq
> shell bytes are visualized in base64-encoded form.
>
>
>> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is not
>>>> JSON serializable”
>>>
>>> This is a Py3-compatibility bug. We should decode bytes to a str on
>>> Python 3. Given that we expect input to be base64-encoded, we can using
>>> 'ascii' codec.
>>>
>>
>> Is this error coming from json library ? We write data to JSON files
>> before exporting to BQ and JSON cannot handle raw bytes. We can probably
>> convert bytes to base64 before writing JSON.
>>
>
> I think it's coming from json.dumps() which does not accept bytes, a
> decode to a str should fix this.
>
>
>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
>>>> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
>>>> values are not JSON compliant
>>>
>>> This expected since b’\xab’ cannot be base64 decoded.
>>>
>>
>> This is definitely from json library.
>>
>>
>>> When reading bytes from BQ they are currently returned as base-64
>>>> encoded strings rather then the raw bytes.
>>>
>>> This is also expected.
>>>
>>
>> We can add an option to support reading raw bytes directly as mentioned
>> above.
>>
> Feel free to comment on BEAM-7344.
>
>>
>>
>>>
>>> [0]
>>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
>>>
>>> [1]
>>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
>>> [2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
>>> [3]
>>> https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
>>> [4]
>>> https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
>>> [5]
>>> https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
>>>
>>> [6]
>>> https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
>>>
>>>
>>> On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com>
>>> wrote:
>>>
>>>> Sure, we can make users explicitly ask for schema autodetection,
>>>> instead of it being the default when no schema is provided. I think that's
>>>> reasonable.
>>>>
>>>>
>>>> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
>>>> wrote:
>>>>
>>>>> Thanks everyone for input on this thread. I think there is a confusion
>>>>> between not specifying the schema, and asking BigQuery to do schema
>>>>> autodetection. This is not the same thing, however in recent changes to BQ
>>>>> IO that happened after 2.11 release, we are forcing schema autodetection,
>>>>> when schema is not specified, see: [1].
>>>>>
>>>>> I think we need to revise this ahead of 2.12. It may be better if
>>>>> users explicitly opt-in to schema autodetection if they wish. Autodetection
>>>>> is an approximation, and in particular, as we figured out in this thread,
>>>>> it does not work correctly for BYTES data.
>>>>>
>>>>> I suspect that if we disable schema autodetection, and/or make
>>>>> previous implementation of BQ sink a default option, we will be able to
>>>>> write BYTES data to a previously created BQ table without specifying the
>>>>> schema, and making a call to BQ to fetch the schema won't be necessary.
>>>>> We'd need to verify that.
>>>>>
>>>>
>>>>> Another interesting note, as per Juta's analysis
>>>>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>>>>> google-cloud-bigquery client does not require additional base64 encoding
>>>>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>>>>> of Bytes data won't be necessary in Beam.
>>>>>
>>>>> [1]
>>>>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>>>>> .
>>>>> [2]
>>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>>>>
>>>>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <
>>>>> chamikara@google.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>>>>> wrote:
>>>>>>
>>>>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery
>>>>>>> sink, schema autodetection is supported (it's a very simple thing to have).
>>>>>>> Do you think we should not have it?
>>>>>>> Best
>>>>>>> -P.
>>>>>>>
>>>>>>
>>>>>> Ah good to know. But IMO users should be able to write to existing
>>>>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>>>>> for example). How do users enable schema auto-detection ? Probably this
>>>>>> should not be enabled by default and we should clearly advertise that bytes
>>>>>> type is not supported (or support it with extra information). Just my 2
>>>>>> cents.
>>>>>>
>>>>>> Thanks,
>>>>>> Cham
>>>>>>
>>>>>>
>>>>>>>
>>>>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>>>>> chamikara@google.com> wrote:
>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>>>>> valentyn@google.com> wrote:
>>>>>>>>>
>>>>>>>>>> We received feedback on
>>>>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>>>>> they write to BQ
>>>>>>>>>
>>>>>>>>> So new summary:
>>>>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ
>>>>>>>>> over rest API. This will be a change in behavior for Python 2 (for good
>>>>>>>>> reasons).
>>>>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>>>>> base64-decoded.
>>>>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>>>>> whenever schema is not supplied, to work around
>>>>>>>>> https://issuetracker.google.com/issues/129006689. Beam will
>>>>>>>>> require users to specify the schema when writing bytes to BQ.
>>>>>>>>>
>>>>>>>>
>>>>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use
>>>>>>>> BQ schema auto detection feature currently. So why not just send an API
>>>>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>>>>> even if we decide to support schema auto detection in the future we will
>>>>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>>>>
>>>>>>>>
>>>>>>>>> Thanks all for your input on this!
>>>>>>>>> Juta
>>>>>>>>>
>>>>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
On Thu, May 16, 2019 at 1:12 PM Chamikara Jayalath <ch...@google.com>
wrote:
>
>
> On Wed, May 15, 2019 at 12:26 PM Valentyn Tymofieiev <va...@google.com>
> wrote:
>
>> I took a closer look at BigQuery IO implementation in Beam SDK and
>> Dataflow runner while reviewing a few PRs to address BEAM-6769, and I think
>> we have to revise the course of action here.
>>
>> It turns out, that when we first added support for BYTES in Java BiqQuery
>> IO, we designed the API with an expectation that:
>> - On write path the user must pass base64-encoded bytes to the BQ IO. [0]
>> - On read path BQ IO base64-encodes the output result, before serving it
>> to the user. [1]
>>
>> When support for BigQuery was added to Python SDK and Dataflow runner,
>> the runner authors preserved the behavior of treating bytes to be
>> consistent with Java BQ IO - bytes must be base64-encoded by the user, and
>> bytes from BQ IO returned by Dataflow Python runner are base64-encoded.
>>
>> Unfortunately, this behavior is not documented in public documentation or
>> JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up until
>> we added integration tests a few years down the road [5,6]. Thanks to these
>> integration tests we discovered BEAM-6769.
>>
>> I don't have context why we made a decision to avoid handling raw bytes
>> in Beam, however I think keeping consistent treatment of bytes across all
>> SDKs and runners is important for a smooth user experience, especially so
>> when a particular behavior is not documented well.
>>
>> This being said I suggest the following:
>> 1. Let's keep the current expectation that Beam operates only on
>> base64-encoded bytes in BQ IO. It may be reasonable to revise this
>> expectation, but it is beyond the scope of BEAM-6769.
>> 2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
>> Chances are that if we had such documentation, we wouldn't have had to
>> answer questions raised in this thread. Filed BEAM-7326 to track.
>> 3. Let's revise Python BQ integration tests to clearly communicate that
>> BQ IO expects base64-encoded bytes. Filed BEAM-7327 to track.
>>
>
> +1 for documentation updates at least.
> We probably will able to add support for writing both base64 encoded bytes
> and row bytes without breaking backwards compatibility though. For reading,
> we can add an option if we want to add support for automatically decoding
> base64 encoded bytes.
>
Filed https://issues.apache.org/jira/browse/BEAM-7344 to consider this.
>
>> Coming back to the original message:
>>
>> When writing b’abc’ in python 2 this results in actually writing b'i\xb7'
>>> which is the same as base64.b64decode('abc='))
>>
>> This is expected as Beam BQ IO expect users to base64-encode their bytes.
>>
>
> Does BigQuery still store data as bytes (if the type of the corresponding
> field is BYTES) ?
>
Yes, I believe BigQuery stores data as raw BYTES, but in the BQ UI / bq
shell bytes are visualized in base64-encoded form.
> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is not
>>> JSON serializable”
>>
>> This is a Py3-compatibility bug. We should decode bytes to a str on
>> Python 3. Given that we expect input to be base64-encoded, we can using
>> 'ascii' codec.
>>
>
> Is this error coming from json library ? We write data to JSON files
> before exporting to BQ and JSON cannot handle raw bytes. We can probably
> convert bytes to base64 before writing JSON.
>
I think it's coming from json.dumps() which does not accept bytes, a decode
to a str should fix this.
> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
>>> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
>>> values are not JSON compliant
>>
>> This expected since b’\xab’ cannot be base64 decoded.
>>
>
> This is definitely from json library.
>
>
>> When reading bytes from BQ they are currently returned as base-64 encoded
>>> strings rather then the raw bytes.
>>
>> This is also expected.
>>
>
> We can add an option to support reading raw bytes directly as mentioned
> above.
>
Feel free to comment on BEAM-7344.
>
>
>>
>> [0]
>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
>>
>> [1]
>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
>> [2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
>> [3]
>> https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
>> [4]
>> https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
>> [5]
>> https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
>>
>> [6]
>> https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
>>
>>
>> On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com>
>> wrote:
>>
>>> Sure, we can make users explicitly ask for schema autodetection, instead
>>> of it being the default when no schema is provided. I think that's
>>> reasonable.
>>>
>>>
>>> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
>>> wrote:
>>>
>>>> Thanks everyone for input on this thread. I think there is a confusion
>>>> between not specifying the schema, and asking BigQuery to do schema
>>>> autodetection. This is not the same thing, however in recent changes to BQ
>>>> IO that happened after 2.11 release, we are forcing schema autodetection,
>>>> when schema is not specified, see: [1].
>>>>
>>>> I think we need to revise this ahead of 2.12. It may be better if users
>>>> explicitly opt-in to schema autodetection if they wish. Autodetection is an
>>>> approximation, and in particular, as we figured out in this thread, it does
>>>> not work correctly for BYTES data.
>>>>
>>>> I suspect that if we disable schema autodetection, and/or make previous
>>>> implementation of BQ sink a default option, we will be able to write BYTES
>>>> data to a previously created BQ table without specifying the schema, and
>>>> making a call to BQ to fetch the schema won't be necessary. We'd need to
>>>> verify that.
>>>>
>>>
>>>> Another interesting note, as per Juta's analysis
>>>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>>>> google-cloud-bigquery client does not require additional base64 encoding
>>>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>>>> of Bytes data won't be necessary in Beam.
>>>>
>>>> [1]
>>>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>>>> .
>>>> [2]
>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>>>
>>>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <
>>>> chamikara@google.com> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>>>> wrote:
>>>>>
>>>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery
>>>>>> sink, schema autodetection is supported (it's a very simple thing to have).
>>>>>> Do you think we should not have it?
>>>>>> Best
>>>>>> -P.
>>>>>>
>>>>>
>>>>> Ah good to know. But IMO users should be able to write to existing
>>>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>>>> for example). How do users enable schema auto-detection ? Probably this
>>>>> should not be enabled by default and we should clearly advertise that bytes
>>>>> type is not supported (or support it with extra information). Just my 2
>>>>> cents.
>>>>>
>>>>> Thanks,
>>>>> Cham
>>>>>
>>>>>
>>>>>>
>>>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>>>> chamikara@google.com> wrote:
>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu>
>>>>>>> wrote:
>>>>>>>
>>>>>>>>
>>>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>>>> valentyn@google.com> wrote:
>>>>>>>>
>>>>>>>>> We received feedback on
>>>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>>>
>>>>>>>>
>>>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>>>> they write to BQ
>>>>>>>>
>>>>>>>> So new summary:
>>>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ
>>>>>>>> over rest API. This will be a change in behavior for Python 2 (for good
>>>>>>>> reasons).
>>>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>>>> base64-decoded.
>>>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>>>> whenever schema is not supplied, to work around
>>>>>>>> https://issuetracker.google.com/issues/129006689. Beam will
>>>>>>>> require users to specify the schema when writing bytes to BQ.
>>>>>>>>
>>>>>>>
>>>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use
>>>>>>> BQ schema auto detection feature currently. So why not just send an API
>>>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>>>> even if we decide to support schema auto detection in the future we will
>>>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>>>
>>>>>>>
>>>>>>>> Thanks all for your input on this!
>>>>>>>> Juta
>>>>>>>>
>>>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Wed, May 15, 2019 at 12:26 PM Valentyn Tymofieiev <va...@google.com>
wrote:
> I took a closer look at BigQuery IO implementation in Beam SDK and
> Dataflow runner while reviewing a few PRs to address BEAM-6769, and I think
> we have to revise the course of action here.
>
> It turns out, that when we first added support for BYTES in Java BiqQuery
> IO, we designed the API with an expectation that:
> - On write path the user must pass base64-encoded bytes to the BQ IO. [0]
> - On read path BQ IO base64-encodes the output result, before serving it
> to the user. [1]
>
> When support for BigQuery was added to Python SDK and Dataflow runner, the
> runner authors preserved the behavior of treating bytes to be consistent
> with Java BQ IO - bytes must be base64-encoded by the user, and bytes from
> BQ IO returned by Dataflow Python runner are base64-encoded.
>
> Unfortunately, this behavior is not documented in public documentation or
> JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up until
> we added integration tests a few years down the road [5,6]. Thanks to these
> integration tests we discovered BEAM-6769.
>
> I don't have context why we made a decision to avoid handling raw bytes in
> Beam, however I think keeping consistent treatment of bytes across all SDKs
> and runners is important for a smooth user experience, especially so when a
> particular behavior is not documented well.
>
> This being said I suggest the following:
> 1. Let's keep the current expectation that Beam operates only on
> base64-encoded bytes in BQ IO. It may be reasonable to revise this
> expectation, but it is beyond the scope of BEAM-6769.
> 2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
> Chances are that if we had such documentation, we wouldn't have had to
> answer questions raised in this thread. Filed BEAM-7326 to track.
> 3. Let's revise Python BQ integration tests to clearly communicate that BQ
> IO expects base64-encoded bytes. Filed BEAM-7327 to track.
>
+1 for documentation updates at least.
We probably will able to add support for writing both base64 encoded bytes
and row bytes without breaking backwards compatibility though. For reading,
we can add an option if we want to add support for automatically decoding
base64 encoded bytes.
> Coming back to the original message:
>
> When writing b’abc’ in python 2 this results in actually writing b'i\xb7'
>> which is the same as base64.b64decode('abc='))
>
> This is expected as Beam BQ IO expect users to base64-encode their bytes.
>
Does BigQuery still store data as bytes (if the type of the corresponding
field is BYTES) ?
> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is not
>> JSON serializable”
>
> This is a Py3-compatibility bug. We should decode bytes to a str on Python
> 3. Given that we expect input to be base64-encoded, we can using 'ascii'
> codec.
>
Is this error coming from json library ? We write data to JSON files before
exporting to BQ and JSON cannot handle raw bytes. We can probably convert
bytes to base64 before writing JSON.
> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
>> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
>> values are not JSON compliant
>
> This expected since b’\xab’ cannot be base64 decoded.
>
This is definitely from json library.
> When reading bytes from BQ they are currently returned as base-64 encoded
>> strings rather then the raw bytes.
>
> This is also expected.
>
We can add an option to support reading raw bytes directly as mentioned
above.
>
> [0]
> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
>
> [1]
> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
> [2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
> [3]
> https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
> [4]
> https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
> [5]
> https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
>
> [6]
> https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
>
>
> On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com> wrote:
>
>> Sure, we can make users explicitly ask for schema autodetection, instead
>> of it being the default when no schema is provided. I think that's
>> reasonable.
>>
>>
>> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
>> wrote:
>>
>>> Thanks everyone for input on this thread. I think there is a confusion
>>> between not specifying the schema, and asking BigQuery to do schema
>>> autodetection. This is not the same thing, however in recent changes to BQ
>>> IO that happened after 2.11 release, we are forcing schema autodetection,
>>> when schema is not specified, see: [1].
>>>
>>> I think we need to revise this ahead of 2.12. It may be better if users
>>> explicitly opt-in to schema autodetection if they wish. Autodetection is an
>>> approximation, and in particular, as we figured out in this thread, it does
>>> not work correctly for BYTES data.
>>>
>>> I suspect that if we disable schema autodetection, and/or make previous
>>> implementation of BQ sink a default option, we will be able to write BYTES
>>> data to a previously created BQ table without specifying the schema, and
>>> making a call to BQ to fetch the schema won't be necessary. We'd need to
>>> verify that.
>>>
>>
>>> Another interesting note, as per Juta's analysis
>>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>>> google-cloud-bigquery client does not require additional base64 encoding
>>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>>> of Bytes data won't be necessary in Beam.
>>>
>>> [1]
>>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>>> .
>>> [2]
>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>>
>>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <ch...@google.com>
>>> wrote:
>>>
>>>>
>>>>
>>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>>> wrote:
>>>>
>>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery
>>>>> sink, schema autodetection is supported (it's a very simple thing to have).
>>>>> Do you think we should not have it?
>>>>> Best
>>>>> -P.
>>>>>
>>>>
>>>> Ah good to know. But IMO users should be able to write to existing
>>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>>> for example). How do users enable schema auto-detection ? Probably this
>>>> should not be enabled by default and we should clearly advertise that bytes
>>>> type is not supported (or support it with extra information). Just my 2
>>>> cents.
>>>>
>>>> Thanks,
>>>> Cham
>>>>
>>>>
>>>>>
>>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>>> chamikara@google.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>>>
>>>>>>>
>>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>>> valentyn@google.com> wrote:
>>>>>>>
>>>>>>>> We received feedback on
>>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>>
>>>>>>>
>>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>>> they write to BQ
>>>>>>>
>>>>>>> So new summary:
>>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>>> base64-decoded.
>>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>>> whenever schema is not supplied, to work around
>>>>>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>>>>>> users to specify the schema when writing bytes to BQ.
>>>>>>>
>>>>>>
>>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use
>>>>>> BQ schema auto detection feature currently. So why not just send an API
>>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>>> even if we decide to support schema auto detection in the future we will
>>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>>
>>>>>>
>>>>>>> Thanks all for your input on this!
>>>>>>> Juta
>>>>>>>
>>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Robert Burke <ro...@frantil.com>.
For the Go SDK:
BigQueryIO
<https://github.com/apache/beam/tree/master/sdks/go/pkg/beam/io/bigqueryio>
exists,
but other than maybe one PR that added batching of writes (to avoid the
size limit communicating with BigQuery), the reads are probably going to be
re-written
I don't believe there's any special handling of base64 bytes by the IO
code. Users pass in their types, and the assumption is they use BigQuery
compatible Go schemas. eg. tornadoes example
<https://github.com/apache/beam/blob/master/sdks/go/examples/cookbook/tornadoes/tornadoes.go#L60>
,
On Wed, 15 May 2019 at 12:41, Valentyn Tymofieiev <va...@google.com>
wrote:
> By the way, does anyone know what is the status of BigQuery connector in
> Beam Go and Beam SQL? Perhaps some folks working on these SDKs can chime in
> here.
> I am curious whether these SDKs also make / will make it a responsibility
> of the user to base64-encode bytes. As I mentioned above, it is desirable
> to have a consistent UX across SDK, especially given that we are working on
> adding support for cross-language pipelines (
> https://beam.apache.org/roadmap/connectors-multi-sdk/).
>
> On Wed, May 15, 2019 at 12:26 PM Valentyn Tymofieiev <va...@google.com>
> wrote:
>
>> I took a closer look at BigQuery IO implementation in Beam SDK and
>> Dataflow runner while reviewing a few PRs to address BEAM-6769, and I think
>> we have to revise the course of action here.
>>
>> It turns out, that when we first added support for BYTES in Java BiqQuery
>> IO, we designed the API with an expectation that:
>> - On write path the user must pass base64-encoded bytes to the BQ IO. [0]
>> - On read path BQ IO base64-encodes the output result, before serving it
>> to the user. [1]
>>
>> When support for BigQuery was added to Python SDK and Dataflow runner,
>> the runner authors preserved the behavior of treating bytes to be
>> consistent with Java BQ IO - bytes must be base64-encoded by the user, and
>> bytes from BQ IO returned by Dataflow Python runner are base64-encoded.
>>
>> Unfortunately, this behavior is not documented in public documentation or
>> JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up until
>> we added integration tests a few years down the road [5,6]. Thanks to these
>> integration tests we discovered BEAM-6769.
>>
>> I don't have context why we made a decision to avoid handling raw bytes
>> in Beam, however I think keeping consistent treatment of bytes across all
>> SDKs and runners is important for a smooth user experience, especially so
>> when a particular behavior is not documented well.
>>
>> This being said I suggest the following:
>> 1. Let's keep the current expectation that Beam operates only on
>> base64-encoded bytes in BQ IO. It may be reasonable to revise this
>> expectation, but it is beyond the scope of BEAM-6769.
>> 2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
>> Chances are that if we had such documentation, we wouldn't have had to
>> answer questions raised in this thread. Filed BEAM-7326 to track.
>> 3. Let's revise Python BQ integration tests to clearly communicate that
>> BQ IO expects base64-encoded bytes. Filed BEAM-7327 to track.
>>
>> Coming back to the original message:
>>
>> When writing b’abc’ in python 2 this results in actually writing b'i\xb7'
>>> which is the same as base64.b64decode('abc='))
>>
>> This is expected as Beam BQ IO expect users to base64-encode their bytes.
>>
>>> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is
>>> not JSON serializable”
>>
>> This is a Py3-compatibility bug. We should decode bytes to a str on
>> Python 3. Given that we expect input to be base64-encoded, we can using
>> 'ascii' codec.
>>
>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
>>> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
>>> values are not JSON compliant
>>
>> This expected since b’\xab’ cannot be base64 decoded.
>>
>>> When reading bytes from BQ they are currently returned as base-64
>>> encoded strings rather then the raw bytes.
>>
>> This is also expected.
>>
>> [0]
>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
>>
>> [1]
>> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
>> [2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
>> [3]
>> https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
>> [4]
>> https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
>> [5]
>> https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
>>
>> [6]
>> https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
>>
>>
>> On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com>
>> wrote:
>>
>>> Sure, we can make users explicitly ask for schema autodetection, instead
>>> of it being the default when no schema is provided. I think that's
>>> reasonable.
>>>
>>>
>>> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
>>> wrote:
>>>
>>>> Thanks everyone for input on this thread. I think there is a confusion
>>>> between not specifying the schema, and asking BigQuery to do schema
>>>> autodetection. This is not the same thing, however in recent changes to BQ
>>>> IO that happened after 2.11 release, we are forcing schema autodetection,
>>>> when schema is not specified, see: [1].
>>>>
>>>> I think we need to revise this ahead of 2.12. It may be better if users
>>>> explicitly opt-in to schema autodetection if they wish. Autodetection is an
>>>> approximation, and in particular, as we figured out in this thread, it does
>>>> not work correctly for BYTES data.
>>>>
>>>> I suspect that if we disable schema autodetection, and/or make previous
>>>> implementation of BQ sink a default option, we will be able to write BYTES
>>>> data to a previously created BQ table without specifying the schema, and
>>>> making a call to BQ to fetch the schema won't be necessary. We'd need to
>>>> verify that.
>>>>
>>>
>>>> Another interesting note, as per Juta's analysis
>>>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>>>> google-cloud-bigquery client does not require additional base64 encoding
>>>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>>>> of Bytes data won't be necessary in Beam.
>>>>
>>>> [1]
>>>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>>>> .
>>>> [2]
>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>>>
>>>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <
>>>> chamikara@google.com> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>>>> wrote:
>>>>>
>>>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery
>>>>>> sink, schema autodetection is supported (it's a very simple thing to have).
>>>>>> Do you think we should not have it?
>>>>>> Best
>>>>>> -P.
>>>>>>
>>>>>
>>>>> Ah good to know. But IMO users should be able to write to existing
>>>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>>>> for example). How do users enable schema auto-detection ? Probably this
>>>>> should not be enabled by default and we should clearly advertise that bytes
>>>>> type is not supported (or support it with extra information). Just my 2
>>>>> cents.
>>>>>
>>>>> Thanks,
>>>>> Cham
>>>>>
>>>>>
>>>>>>
>>>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>>>> chamikara@google.com> wrote:
>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu>
>>>>>>> wrote:
>>>>>>>
>>>>>>>>
>>>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>>>> valentyn@google.com> wrote:
>>>>>>>>
>>>>>>>>> We received feedback on
>>>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>>>
>>>>>>>>
>>>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>>>> they write to BQ
>>>>>>>>
>>>>>>>> So new summary:
>>>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ
>>>>>>>> over rest API. This will be a change in behavior for Python 2 (for good
>>>>>>>> reasons).
>>>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>>>> base64-decoded.
>>>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>>>> whenever schema is not supplied, to work around
>>>>>>>> https://issuetracker.google.com/issues/129006689. Beam will
>>>>>>>> require users to specify the schema when writing bytes to BQ.
>>>>>>>>
>>>>>>>
>>>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use
>>>>>>> BQ schema auto detection feature currently. So why not just send an API
>>>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>>>> even if we decide to support schema auto detection in the future we will
>>>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>>>
>>>>>>>
>>>>>>>> Thanks all for your input on this!
>>>>>>>> Juta
>>>>>>>>
>>>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
By the way, does anyone know what is the status of BigQuery connector in
Beam Go and Beam SQL? Perhaps some folks working on these SDKs can chime in
here.
I am curious whether these SDKs also make / will make it a responsibility
of the user to base64-encode bytes. As I mentioned above, it is desirable
to have a consistent UX across SDK, especially given that we are working on
adding support for cross-language pipelines (
https://beam.apache.org/roadmap/connectors-multi-sdk/).
On Wed, May 15, 2019 at 12:26 PM Valentyn Tymofieiev <va...@google.com>
wrote:
> I took a closer look at BigQuery IO implementation in Beam SDK and
> Dataflow runner while reviewing a few PRs to address BEAM-6769, and I think
> we have to revise the course of action here.
>
> It turns out, that when we first added support for BYTES in Java BiqQuery
> IO, we designed the API with an expectation that:
> - On write path the user must pass base64-encoded bytes to the BQ IO. [0]
> - On read path BQ IO base64-encodes the output result, before serving it
> to the user. [1]
>
> When support for BigQuery was added to Python SDK and Dataflow runner, the
> runner authors preserved the behavior of treating bytes to be consistent
> with Java BQ IO - bytes must be base64-encoded by the user, and bytes from
> BQ IO returned by Dataflow Python runner are base64-encoded.
>
> Unfortunately, this behavior is not documented in public documentation or
> JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up until
> we added integration tests a few years down the road [5,6]. Thanks to these
> integration tests we discovered BEAM-6769.
>
> I don't have context why we made a decision to avoid handling raw bytes in
> Beam, however I think keeping consistent treatment of bytes across all SDKs
> and runners is important for a smooth user experience, especially so when a
> particular behavior is not documented well.
>
> This being said I suggest the following:
> 1. Let's keep the current expectation that Beam operates only on
> base64-encoded bytes in BQ IO. It may be reasonable to revise this
> expectation, but it is beyond the scope of BEAM-6769.
> 2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
> Chances are that if we had such documentation, we wouldn't have had to
> answer questions raised in this thread. Filed BEAM-7326 to track.
> 3. Let's revise Python BQ integration tests to clearly communicate that BQ
> IO expects base64-encoded bytes. Filed BEAM-7327 to track.
>
> Coming back to the original message:
>
> When writing b’abc’ in python 2 this results in actually writing b'i\xb7'
>> which is the same as base64.b64decode('abc='))
>
> This is expected as Beam BQ IO expect users to base64-encode their bytes.
>
>> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is not
>> JSON serializable”
>
> This is a Py3-compatibility bug. We should decode bytes to a str on Python
> 3. Given that we expect input to be base64-encoded, we can using 'ascii'
> codec.
>
>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
>> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
>> values are not JSON compliant
>
> This expected since b’\xab’ cannot be base64 decoded.
>
>> When reading bytes from BQ they are currently returned as base-64 encoded
>> strings rather then the raw bytes.
>
> This is also expected.
>
> [0]
> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
>
> [1]
> https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
> [2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
> [3]
> https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
> [4]
> https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
> [5]
> https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
>
> [6]
> https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
>
>
> On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com> wrote:
>
>> Sure, we can make users explicitly ask for schema autodetection, instead
>> of it being the default when no schema is provided. I think that's
>> reasonable.
>>
>>
>> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
>> wrote:
>>
>>> Thanks everyone for input on this thread. I think there is a confusion
>>> between not specifying the schema, and asking BigQuery to do schema
>>> autodetection. This is not the same thing, however in recent changes to BQ
>>> IO that happened after 2.11 release, we are forcing schema autodetection,
>>> when schema is not specified, see: [1].
>>>
>>> I think we need to revise this ahead of 2.12. It may be better if users
>>> explicitly opt-in to schema autodetection if they wish. Autodetection is an
>>> approximation, and in particular, as we figured out in this thread, it does
>>> not work correctly for BYTES data.
>>>
>>> I suspect that if we disable schema autodetection, and/or make previous
>>> implementation of BQ sink a default option, we will be able to write BYTES
>>> data to a previously created BQ table without specifying the schema, and
>>> making a call to BQ to fetch the schema won't be necessary. We'd need to
>>> verify that.
>>>
>>
>>> Another interesting note, as per Juta's analysis
>>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>>> google-cloud-bigquery client does not require additional base64 encoding
>>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>>> of Bytes data won't be necessary in Beam.
>>>
>>> [1]
>>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>>> .
>>> [2]
>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>>
>>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <ch...@google.com>
>>> wrote:
>>>
>>>>
>>>>
>>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>>> wrote:
>>>>
>>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery
>>>>> sink, schema autodetection is supported (it's a very simple thing to have).
>>>>> Do you think we should not have it?
>>>>> Best
>>>>> -P.
>>>>>
>>>>
>>>> Ah good to know. But IMO users should be able to write to existing
>>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>>> for example). How do users enable schema auto-detection ? Probably this
>>>> should not be enabled by default and we should clearly advertise that bytes
>>>> type is not supported (or support it with extra information). Just my 2
>>>> cents.
>>>>
>>>> Thanks,
>>>> Cham
>>>>
>>>>
>>>>>
>>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>>> chamikara@google.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>>>
>>>>>>>
>>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>>> valentyn@google.com> wrote:
>>>>>>>
>>>>>>>> We received feedback on
>>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>>
>>>>>>>
>>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>>> they write to BQ
>>>>>>>
>>>>>>> So new summary:
>>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>>> base64-decoded.
>>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>>> whenever schema is not supplied, to work around
>>>>>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>>>>>> users to specify the schema when writing bytes to BQ.
>>>>>>>
>>>>>>
>>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use
>>>>>> BQ schema auto detection feature currently. So why not just send an API
>>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>>> even if we decide to support schema auto detection in the future we will
>>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>>
>>>>>>
>>>>>>> Thanks all for your input on this!
>>>>>>> Juta
>>>>>>>
>>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
I took a closer look at BigQuery IO implementation in Beam SDK and Dataflow
runner while reviewing a few PRs to address BEAM-6769, and I think we have
to revise the course of action here.
It turns out, that when we first added support for BYTES in Java BiqQuery
IO, we designed the API with an expectation that:
- On write path the user must pass base64-encoded bytes to the BQ IO. [0]
- On read path BQ IO base64-encodes the output result, before serving it to
the user. [1]
When support for BigQuery was added to Python SDK and Dataflow runner, the
runner authors preserved the behavior of treating bytes to be consistent
with Java BQ IO - bytes must be base64-encoded by the user, and bytes from
BQ IO returned by Dataflow Python runner are base64-encoded.
Unfortunately, this behavior is not documented in public documentation or
JavaDoc/PyDocs [2-4], and there were no examples illustrating it, up until
we added integration tests a few years down the road [5,6]. Thanks to these
integration tests we discovered BEAM-6769.
I don't have context why we made a decision to avoid handling raw bytes in
Beam, however I think keeping consistent treatment of bytes across all SDKs
and runners is important for a smooth user experience, especially so when a
particular behavior is not documented well.
This being said I suggest the following:
1. Let's keep the current expectation that Beam operates only on
base64-encoded bytes in BQ IO. It may be reasonable to revise this
expectation, but it is beyond the scope of BEAM-6769.
2. Let's document current behavior of BQ IO w.r.t. of handling bytes.
Chances are that if we had such documentation, we wouldn't have had to
answer questions raised in this thread. Filed BEAM-7326 to track.
3. Let's revise Python BQ integration tests to clearly communicate that BQ
IO expects base64-encoded bytes. Filed BEAM-7327 to track.
Coming back to the original message:
When writing b’abc’ in python 2 this results in actually writing b'i\xb7'
> which is the same as base64.b64decode('abc='))
This is expected as Beam BQ IO expect users to base64-encode their bytes.
> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is not
> JSON serializable”
This is a Py3-compatibility bug. We should decode bytes to a str on Python
3. Given that we expect input to be base64-encoded, we can using 'ascii'
codec.
> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
> values are not JSON compliant
This expected since b’\xab’ cannot be base64 decoded.
> When reading bytes from BQ they are currently returned as base-64 encoded
> strings rather then the raw bytes.
This is also expected.
[0]
https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-1016cd1e3092d30556292ab7b983c4c8R103
[1]
https://github.com/apache/beam/commit/c7e0010b0d4a3c45148d05f5101f5310bb84c40c#diff-44025ee9b9c94123967e1df92bfb1c04R207
[2] https://beam.apache.org/documentation/io/built-in/google-bigquery/
[3]
https://beam.apache.org/releases/pydoc/2.12.0/apache_beam.io.gcp.bigquery.html
[4]
https://beam.apache.org/releases/javadoc/2.12.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.html
[5]
https://github.com/apache/beam/blob/7b1abc923183a9f6336d3d44681b8fcd8785104c/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryToTableIT.java#L92
[6]
https://github.com/apache/beam/commit/d6b456dd922655b216b2c5af6548b0f5fe4eb507#diff-7f1bb65cbe782f5a27c5a75b6fe89fbcR112
On Tue, Mar 26, 2019 at 11:27 AM Pablo Estrada <pa...@google.com> wrote:
> Sure, we can make users explicitly ask for schema autodetection, instead
> of it being the default when no schema is provided. I think that's
> reasonable.
>
>
> On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
> wrote:
>
>> Thanks everyone for input on this thread. I think there is a confusion
>> between not specifying the schema, and asking BigQuery to do schema
>> autodetection. This is not the same thing, however in recent changes to BQ
>> IO that happened after 2.11 release, we are forcing schema autodetection,
>> when schema is not specified, see: [1].
>>
>> I think we need to revise this ahead of 2.12. It may be better if users
>> explicitly opt-in to schema autodetection if they wish. Autodetection is an
>> approximation, and in particular, as we figured out in this thread, it does
>> not work correctly for BYTES data.
>>
>> I suspect that if we disable schema autodetection, and/or make previous
>> implementation of BQ sink a default option, we will be able to write BYTES
>> data to a previously created BQ table without specifying the schema, and
>> making a call to BQ to fetch the schema won't be necessary. We'd need to
>> verify that.
>>
>
>> Another interesting note, as per Juta's analysis
>> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
>> google-cloud-bigquery client does not require additional base64 encoding
>> for bytes, so once we migrate to use this client, base64 encoding/decoding
>> of Bytes data won't be necessary in Beam.
>>
>> [1]
>> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
>> .
>> [2]
>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>>
>> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <ch...@google.com>
>> wrote:
>>
>>>
>>>
>>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com>
>>> wrote:
>>>
>>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery sink,
>>>> schema autodetection is supported (it's a very simple thing to have). Do
>>>> you think we should not have it?
>>>> Best
>>>> -P.
>>>>
>>>
>>> Ah good to know. But IMO users should be able to write to existing
>>> tables without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER
>>> for example). How do users enable schema auto-detection ? Probably this
>>> should not be enabled by default and we should clearly advertise that bytes
>>> type is not supported (or support it with extra information). Just my 2
>>> cents.
>>>
>>> Thanks,
>>> Cham
>>>
>>>
>>>>
>>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>>> chamikara@google.com> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>>
>>>>>>
>>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <
>>>>>> valentyn@google.com> wrote:
>>>>>>
>>>>>>> We received feedback on
>>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers
>>>>>>> say that schema identification is done and they discourage to use schema
>>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>>> unless schema is specified. Does that make sense?
>>>>>>>
>>>>>>
>>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>>> they write to BQ
>>>>>>
>>>>>> So new summary:
>>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>>> base64-decoded.
>>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>>> whenever schema is not supplied, to work around
>>>>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>>>>> users to specify the schema when writing bytes to BQ.
>>>>>>
>>>>>
>>>>> I'm not sure why we reached this conclusion. We (Beam) does not use BQ
>>>>> schema auto detection feature currently. So why not just send an API
>>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>>> even if we decide to support schema auto detection in the future we will
>>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>>
>>>>>
>>>>>> Thanks all for your input on this!
>>>>>> Juta
>>>>>>
>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Pablo Estrada <pa...@google.com>.
Sure, we can make users explicitly ask for schema autodetection, instead of
it being the default when no schema is provided. I think that's reasonable.
On Mon, Mar 25, 2019, 7:19 PM Valentyn Tymofieiev <va...@google.com>
wrote:
> Thanks everyone for input on this thread. I think there is a confusion
> between not specifying the schema, and asking BigQuery to do schema
> autodetection. This is not the same thing, however in recent changes to BQ
> IO that happened after 2.11 release, we are forcing schema autodetection,
> when schema is not specified, see: [1].
>
> I think we need to revise this ahead of 2.12. It may be better if users
> explicitly opt-in to schema autodetection if they wish. Autodetection is an
> approximation, and in particular, as we figured out in this thread, it does
> not work correctly for BYTES data.
>
> I suspect that if we disable schema autodetection, and/or make previous
> implementation of BQ sink a default option, we will be able to write BYTES
> data to a previously created BQ table without specifying the schema, and
> making a call to BQ to fetch the schema won't be necessary. We'd need to
> verify that.
>
> Another interesting note, as per Juta's analysis
> <https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
> google-cloud-bigquery client does not require additional base64 encoding
> for bytes, so once we migrate to use this client, base64 encoding/decoding
> of Bytes data won't be necessary in Beam.
>
> [1]
> https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
> .
> [2]
> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
>
> On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <ch...@google.com>
> wrote:
>
>>
>>
>> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com> wrote:
>>
>>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery sink,
>>> schema autodetection is supported (it's a very simple thing to have). Do
>>> you think we should not have it?
>>> Best
>>> -P.
>>>
>>
>> Ah good to know. But IMO users should be able to write to existing tables
>> without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER for
>> example). How do users enable schema auto-detection ? Probably this should
>> not be enabled by default and we should clearly advertise that bytes type
>> is not supported (or support it with extra information). Just my 2 cents.
>>
>> Thanks,
>> Cham
>>
>>
>>>
>>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <
>>> chamikara@google.com> wrote:
>>>
>>>>
>>>>
>>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>
>>>>>
>>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
>>>>> wrote:
>>>>>
>>>>>> We received feedback on
>>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers say
>>>>>> that schema identification is done and they discourage to use schema
>>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>>> unless schema is specified. Does that make sense?
>>>>>>
>>>>>
>>>>> Given that schema autodetect does not work for bytes I think it is
>>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>>> they write to BQ
>>>>>
>>>>> So new summary:
>>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>>> base64-decoded.
>>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>>> whenever schema is not supplied, to work around
>>>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>>>> users to specify the schema when writing bytes to BQ.
>>>>>
>>>>
>>>> I'm not sure why we reached this conclusion. We (Beam) does not use BQ
>>>> schema auto detection feature currently. So why not just send an API
>>>> signal to get the schema when users are writing to existing tables ? Also,
>>>> even if we decide to support schema auto detection in the future we will
>>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>>
>>>>
>>>>> Thanks all for your input on this!
>>>>> Juta
>>>>>
>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
Thanks everyone for input on this thread. I think there is a confusion
between not specifying the schema, and asking BigQuery to do schema
autodetection. This is not the same thing, however in recent changes to BQ
IO that happened after 2.11 release, we are forcing schema autodetection,
when schema is not specified, see: [1].
I think we need to revise this ahead of 2.12. It may be better if users
explicitly opt-in to schema autodetection if they wish. Autodetection is an
approximation, and in particular, as we figured out in this thread, it does
not work correctly for BYTES data.
I suspect that if we disable schema autodetection, and/or make previous
implementation of BQ sink a default option, we will be able to write BYTES
data to a previously created BQ table without specifying the schema, and
making a call to BQ to fetch the schema won't be necessary. We'd need to
verify that.
Another interesting note, as per Juta's analysis
<https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing>,
google-cloud-bigquery client does not require additional base64 encoding
for bytes, so once we migrate to use this client, base64 encoding/decoding
of Bytes data won't be necessary in Beam.
[1]
https://github.com/apache/beam/blob/0b71f541e93f3bd69af87ad8a6db46ccb4a01ddc/sdks/python/apache_beam/io/gcp/bigquery_tools.py#L321
.
[2]
https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit#bookmark=id.7pfrsz1c8hcj
On Mon, Mar 25, 2019 at 2:26 PM Chamikara Jayalath <ch...@google.com>
wrote:
>
>
> On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com> wrote:
>
>> +Chamikara Jayalath <ch...@google.com> with the new BigQuery sink,
>> schema autodetection is supported (it's a very simple thing to have). Do
>> you think we should not have it?
>> Best
>> -P.
>>
>
> Ah good to know. But IMO users should be able to write to existing tables
> without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER for
> example). How do users enable schema auto-detection ? Probably this should
> not be enabled by default and we should clearly advertise that bytes type
> is not supported (or support it with extra information). Just my 2 cents.
>
> Thanks,
> Cham
>
>
>>
>> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <ch...@google.com>
>> wrote:
>>
>>>
>>>
>>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>>
>>>>
>>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
>>>> wrote:
>>>>
>>>>> We received feedback on
>>>>> https://issuetracker.google.com/issues/129006689 - BQ developers say
>>>>> that schema identification is done and they discourage to use schema
>>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>>> unless schema is specified. Does that make sense?
>>>>>
>>>>
>>>> Given that schema autodetect does not work for bytes I think it is
>>>> indeed a good solution to require users to specify BQ schemas as well when
>>>> they write to BQ
>>>>
>>>> So new summary:
>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>> 2. When reading data from BQ, all fields of type BYTES will be
>>>> base64-decoded.
>>>> 3. Beam will send an API call to BigQuery to get table schema,
>>>> whenever schema is not supplied, to work around
>>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>>> users to specify the schema when writing bytes to BQ.
>>>>
>>>
>>> I'm not sure why we reached this conclusion. We (Beam) does not use BQ
>>> schema auto detection feature currently. So why not just send an API
>>> signal to get the schema when users are writing to existing tables ? Also,
>>> even if we decide to support schema auto detection in the future we will
>>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>>
>>>
>>>> Thanks all for your input on this!
>>>> Juta
>>>>
>>>>
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Mon, Mar 25, 2019 at 2:16 PM Pablo Estrada <pa...@google.com> wrote:
> +Chamikara Jayalath <ch...@google.com> with the new BigQuery sink,
> schema autodetection is supported (it's a very simple thing to have). Do
> you think we should not have it?
> Best
> -P.
>
Ah good to know. But IMO users should be able to write to existing tables
without specifying a schema (when CEATE_DISPOSITION is CREATE_NEVER for
example). How do users enable schema auto-detection ? Probably this should
not be enabled by default and we should clearly advertise that bytes type
is not supported (or support it with extra information). Just my 2 cents.
Thanks,
Cham
>
> On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <ch...@google.com>
> wrote:
>
>>
>>
>> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>>
>>>
>>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
>>> wrote:
>>>
>>>> We received feedback on
>>>> https://issuetracker.google.com/issues/129006689 - BQ developers say
>>>> that schema identification is done and they discourage to use schema
>>>> autodetection in tables using BYTES. In light of this, I think may be fair
>>>> to recommend Beam users to specify BQ schemas as well when they interact
>>>> with BQ, and call out that writing binary data to BQ will likely fail
>>>> unless schema is specified. Does that make sense?
>>>>
>>>
>>> Given that schema autodetect does not work for bytes I think it is
>>> indeed a good solution to require users to specify BQ schemas as well when
>>> they write to BQ
>>>
>>> So new summary:
>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>> 2. When reading data from BQ, all fields of type BYTES will be
>>> base64-decoded.
>>> 3. Beam will send an API call to BigQuery to get table schema, whenever
>>> schema is not supplied, to work around
>>> https://issuetracker.google.com/issues/129006689. Beam will require
>>> users to specify the schema when writing bytes to BQ.
>>>
>>
>> I'm not sure why we reached this conclusion. We (Beam) does not use BQ
>> schema auto detection feature currently. So why not just send an API
>> signal to get the schema when users are writing to existing tables ? Also,
>> even if we decide to support schema auto detection in the future we will
>> not be able to support this for BYTEs type (due to the restriction by BQ).
>>
>>
>>> Thanks all for your input on this!
>>> Juta
>>>
>>>
Re: Writing bytes to BigQuery with beam
Posted by Pablo Estrada <pa...@google.com>.
+Chamikara Jayalath <ch...@google.com> with the new BigQuery sink,
schema autodetection is supported (it's a very simple thing to have). Do
you think we should not have it?
Best
-P.
On Mon, Mar 25, 2019 at 11:01 AM Chamikara Jayalath <ch...@google.com>
wrote:
>
>
> On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>
>>
>> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
>> wrote:
>>
>>> We received feedback on https://issuetracker.google.com/issues/129006689 -
>>> BQ developers say that schema identification is done and they discourage to
>>> use schema autodetection in tables using BYTES. In light of this, I think
>>> may be fair to recommend Beam users to specify BQ schemas as well when they
>>> interact with BQ, and call out that writing binary data to BQ will likely
>>> fail unless schema is specified. Does that make sense?
>>>
>>
>> Given that schema autodetect does not work for bytes I think it is indeed
>> a good solution to require users to specify BQ schemas as well when they
>> write to BQ
>>
>> So new summary:
>> 1. Beam will base64-encode raw bytes, before passing them to BQ over rest
>> API. This will be a change in behavior for Python 2 (for good reasons).
>> 2. When reading data from BQ, all fields of type BYTES will be
>> base64-decoded.
>> 3. Beam will send an API call to BigQuery to get table schema, whenever
>> schema is not supplied, to work around
>> https://issuetracker.google.com/issues/129006689. Beam will require
>> users to specify the schema when writing bytes to BQ.
>>
>
> I'm not sure why we reached this conclusion. We (Beam) does not use BQ
> schema auto detection feature currently. So why not just send an API
> signal to get the schema when users are writing to existing tables ? Also,
> even if we decide to support schema auto detection in the future we will
> not be able to support this for BYTEs type (due to the restriction by BQ).
>
>
>> Thanks all for your input on this!
>> Juta
>>
>>
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Mon, Mar 25, 2019 at 2:03 AM Juta Staes <ju...@ml6.eu> wrote:
>
> On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
> wrote:
>
>> We received feedback on https://issuetracker.google.com/issues/129006689 -
>> BQ developers say that schema identification is done and they discourage to
>> use schema autodetection in tables using BYTES. In light of this, I think
>> may be fair to recommend Beam users to specify BQ schemas as well when they
>> interact with BQ, and call out that writing binary data to BQ will likely
>> fail unless schema is specified. Does that make sense?
>>
>
> Given that schema autodetect does not work for bytes I think it is indeed
> a good solution to require users to specify BQ schemas as well when they
> write to BQ
>
> So new summary:
> 1. Beam will base64-encode raw bytes, before passing them to BQ over rest
> API. This will be a change in behavior for Python 2 (for good reasons).
> 2. When reading data from BQ, all fields of type BYTES will be
> base64-decoded.
> 3. Beam will send an API call to BigQuery to get table schema, whenever
> schema is not supplied, to work around
> https://issuetracker.google.com/issues/129006689. Beam will require users
> to specify the schema when writing bytes to BQ.
>
I'm not sure why we reached this conclusion. We (Beam) does not use BQ
schema auto detection feature currently. So why not just send an API
signal to get the schema when users are writing to existing tables ? Also,
even if we decide to support schema auto detection in the future we will
not be able to support this for BYTEs type (due to the restriction by BQ).
> Thanks all for your input on this!
> Juta
>
>
Re: Writing bytes to BigQuery with beam
Posted by Juta Staes <ju...@ml6.eu>.
On Mon, 25 Mar 2019 at 06:15, Valentyn Tymofieiev <va...@google.com>
wrote:
> We received feedback on https://issuetracker.google.com/issues/129006689 -
> BQ developers say that schema identification is done and they discourage to
> use schema autodetection in tables using BYTES. In light of this, I think
> may be fair to recommend Beam users to specify BQ schemas as well when they
> interact with BQ, and call out that writing binary data to BQ will likely
> fail unless schema is specified. Does that make sense?
>
Given that schema autodetect does not work for bytes I think it is indeed a
good solution to require users to specify BQ schemas as well when they
write to BQ
So new summary:
1. Beam will base64-encode raw bytes, before passing them to BQ over rest
API. This will be a change in behavior for Python 2 (for good reasons).
2. When reading data from BQ, all fields of type BYTES will be
base64-decoded.
3. Beam will send an API call to BigQuery to get table schema, whenever
schema is not supplied, to work around
https://issuetracker.google.com/issues/129006689. Beam will require users
to specify the schema when writing bytes to BQ.
Thanks all for your input on this!
Juta
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
We received feedback on https://issuetracker.google.com/issues/129006689 -
BQ developers say that schema identification is done and they discourage to
use schema autodetection in tables using BYTES. In light of this, I think
may be fair to recommend Beam users to specify BQ schemas as well when they
interact with BQ, and call out that writing binary data to BQ will likely
fail unless schema is specified. Does that make sense?
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Wed, Mar 20, 2019 at 7:37 PM Valentyn Tymofieiev <va...@google.com>
wrote:
> Pablo, according to Juta's analysis (1.c in the document) and also
> https://issuetracker.google.com/issues/129006689, I think BQ confuses
> BYTES and STRING when schema is not specified... This seems to me like a BQ
> bug, so for Beam this means that we either have to wait until BQ fixes or,
> or work around it. If we work around it, we can ask users to always supply
> schema if their table has BYTES data (temporary limitation), or try to pull
> schema from BQ before (every?) write operation.
>
> Cham, according to BQ documentation, BQ *can* auto-detect schema when
> populating new tables using a data source, for example a json file with
> records : https://cloud.google.com/bigquery/docs/schema-detect.
>
Ah, we don't support that AFAIK so currently we require users to provide a
schema to create tables. But good point, in case if we ever want to support
that feature.
>
> On Wed, Mar 20, 2019 at 7:15 PM Chamikara Jayalath <ch...@google.com>
> wrote:
>
>>
>>
>> On Wed, Mar 20, 2019 at 6:30 PM Pablo Estrada <pa...@google.com> wrote:
>>
>>> That sounds reasonable to me, Valentyn.
>>>
>>> Regarding (3), when the table already exists, it's not necessary to get
>>> the schema. BQ is smart enough to load everything in appropriately. (as
>>> long as bytes fields are base64 encoded)
>>>
>>> The problem is when the table does not exist and the user does not
>>> provide a schema. In that case, there is no simple way of auto-inferring
>>> the schema, as you correctly point out. I think it's reasonable to simply
>>> expect users provide schemas if their data will have tricky types to infer.
>>> Best
>>> -P.
>>>
>>
>> Is this even an option ? I think when table is not available users have
>> to provide a schema to create a new table.
>>
>>
>>>
>>>
>>> On Wed, Mar 20, 2019 at 3:44 PM Valentyn Tymofieiev <va...@google.com>
>>> wrote:
>>>
>>>> Thanks Juta for detailed analysis.
>>>>
>>>> I reached out to BigQuery team to improve documentation around
>>>> treatment of Bytes and reported the issue that schema autodetection does
>>>> not work <https://issuetracker.google.com/issues/129006689> for BYTES
>>>> in GCP issue tracker
>>>> <https://cloud.google.com/support/docs/issue-trackers>.
>>>>
>>>> Is this a correct summary of your proposal?
>>>>
>>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>>> 2. When reading data from BQ, all fileds of type BYTES will be
>>>> base64-decoded.
>>>> 3. Beam will send an API call to BigQuery to get table schema, whenever
>>>> schema is not supplied, to work around
>>>> https://issuetracker.google.com/issues/129006689. Does anyone see any
>>>> concerns with this? Is it always possible?
>>>>
>>>> Thanks,
>>>> Valentyn
>>>>
>>>> On Wed, Mar 20, 2019 at 12:45 PM Reuven Lax <re...@google.com> wrote:
>>>>
>>>>> The Java SDK relies on Jackson to do the encoding.
>>>>>
>>>>> On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <
>>>>> chamikara@google.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>>>
>>>>>>> Hi all,
>>>>>>>
>>>>>>>
>>>>>>> I am working on porting beam to python 3 and discovered the
>>>>>>> following:
>>>>>>>
>>>>>>>
>>>>>>> Current handling of bytes in bigquery IO:
>>>>>>>
>>>>>>> When writing bytes to BQ , beam uses
>>>>>>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>>>>>>> expects byte values to be base-64 encoded*.
>>>>>>>
>>>>>>> However when writing raw bytes they are currently never transformed
>>>>>>> to base-64 encoded strings. This results in the following errors:
>>>>>>>
>>>>>>> -
>>>>>>>
>>>>>>> When writing b’abc’ in python 2 this results in actually writing
>>>>>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>>>>>> -
>>>>>>>
>>>>>>> When writing b’abc’ in python 3 this results in “TypeError:
>>>>>>> b'abc' is not JSON serializable”
>>>>>>> -
>>>>>>>
>>>>>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>>>>>>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>>>>>>> and -INF values are not JSON compliant”
>>>>>>> -
>>>>>>>
>>>>>>> When reading bytes from BQ they are currently returned as
>>>>>>> base-64 encoded strings rather then the raw bytes.
>>>>>>>
>>>>>>>
>>>>>>> Example code:
>>>>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>>>>>>
>>>>>>> There is also another issue when writing base-64 encoded string to
>>>>>>> BQ. When no schema is specified this results in “Invalid schema update.
>>>>>>> Field bytes has changed type from BYTES to STRING”.
>>>>>>>
>>>>>>> This error can be reproduced when uploading a file (directly in the
>>>>>>> BQ UI) to a table with bytes and using schema autodetect.
>>>>>>>
>>>>>>> Suggested solution:
>>>>>>>
>>>>>>> I suggest to change BigQuery IO to handle the base-64 encoding as
>>>>>>> follows to allow the user to read and write raw bytes in BQ
>>>>>>>
>>>>>>> Writing data:
>>>>>>>
>>>>>>> -
>>>>>>>
>>>>>>> When a new table is created we use the provided schema to detect
>>>>>>> bytes and handle the base-64 encoding accordingly
>>>>>>> -
>>>>>>>
>>>>>>> When data is written to an existing table we use the API to get
>>>>>>> the schema of the table and handle the base-64 encoding accordingly. We
>>>>>>> also pass the schema as argument to avoid the error from schema autodetect.
>>>>>>>
>>>>>>> Reading data:
>>>>>>>
>>>>>>> -
>>>>>>>
>>>>>>> When reading data we also request the schema and handle the
>>>>>>> base-64 decoding accordingly to return raw bytes
>>>>>>>
>>>>>>>
>>>>>>> What are your thoughts on this?
>>>>>>>
>>>>>>
>>>>>> Thanks for the update. More context here:
>>>>>> https://issues.apache.org/jira/browse/BEAM-6769
>>>>>>
>>>>>> Suggested solution sounds good to me. BTW do you know how Java SDK
>>>>>> handles bytes type ? I believe we write JSON files and execute load jobs
>>>>>> there as well (when method is FILE_LOADS).
>>>>>>
>>>>>> Thanks,
>>>>>> Cham
>>>>>>
>>>>>>
>>>>>>>
>>>>>>> *I could not find this in the documentation of the API or in the
>>>>>>> documentation of BigQuery itself which also expects base-64 encoded values.
>>>>>>> I discovered this when uploading a file to BQ UI and getting an error:
>>>>>>> "Could not decode base64 string to bytes."
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>
>>>>>>> [image: https://ml6.eu] <https://ml6.eu/>
>>>>>>>
>>>>>>> * Juta Staes*
>>>>>>> ML6 Gent
>>>>>>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>>>>>>
>>>>>>> **** DISCLAIMER ****
>>>>>>> This email and any files transmitted with it are confidential and
>>>>>>> intended solely for the use of the individual or entity to whom they are
>>>>>>> addressed. If you have received this email in error please notify the
>>>>>>> system manager. This message contains confidential information and is
>>>>>>> intended only for the individual named. If you are not the named addressee
>>>>>>> you should not disseminate, distribute or copy this e-mail. Please notify
>>>>>>> the sender immediately by e-mail if you have received this e-mail by
>>>>>>> mistake and delete this e-mail from your system. If you are not the
>>>>>>> intended recipient you are notified that disclosing, copying, distributing
>>>>>>> or taking any action in reliance on the contents of this information is
>>>>>>> strictly prohibited.
>>>>>>>
>>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
Pablo, according to Juta's analysis (1.c in the document) and also
https://issuetracker.google.com/issues/129006689, I think BQ confuses BYTES
and STRING when schema is not specified... This seems to me like a BQ bug,
so for Beam this means that we either have to wait until BQ fixes or, or
work around it. If we work around it, we can ask users to always supply
schema if their table has BYTES data (temporary limitation), or try to pull
schema from BQ before (every?) write operation.
Cham, according to BQ documentation, BQ *can* auto-detect schema when
populating new tables using a data source, for example a json file with
records : https://cloud.google.com/bigquery/docs/schema-detect.
On Wed, Mar 20, 2019 at 7:15 PM Chamikara Jayalath <ch...@google.com>
wrote:
>
>
> On Wed, Mar 20, 2019 at 6:30 PM Pablo Estrada <pa...@google.com> wrote:
>
>> That sounds reasonable to me, Valentyn.
>>
>> Regarding (3), when the table already exists, it's not necessary to get
>> the schema. BQ is smart enough to load everything in appropriately. (as
>> long as bytes fields are base64 encoded)
>>
>> The problem is when the table does not exist and the user does not
>> provide a schema. In that case, there is no simple way of auto-inferring
>> the schema, as you correctly point out. I think it's reasonable to simply
>> expect users provide schemas if their data will have tricky types to infer.
>> Best
>> -P.
>>
>
> Is this even an option ? I think when table is not available users have to
> provide a schema to create a new table.
>
>
>>
>>
>> On Wed, Mar 20, 2019 at 3:44 PM Valentyn Tymofieiev <va...@google.com>
>> wrote:
>>
>>> Thanks Juta for detailed analysis.
>>>
>>> I reached out to BigQuery team to improve documentation around treatment
>>> of Bytes and reported the issue that schema autodetection does not work
>>> <https://issuetracker.google.com/issues/129006689> for BYTES in GCP
>>> issue tracker <https://cloud.google.com/support/docs/issue-trackers>.
>>>
>>> Is this a correct summary of your proposal?
>>>
>>> 1. Beam will base64-encode raw bytes, before passing them to BQ over
>>> rest API. This will be a change in behavior for Python 2 (for good reasons).
>>> 2. When reading data from BQ, all fileds of type BYTES will be
>>> base64-decoded.
>>> 3. Beam will send an API call to BigQuery to get table schema, whenever
>>> schema is not supplied, to work around
>>> https://issuetracker.google.com/issues/129006689. Does anyone see any
>>> concerns with this? Is it always possible?
>>>
>>> Thanks,
>>> Valentyn
>>>
>>> On Wed, Mar 20, 2019 at 12:45 PM Reuven Lax <re...@google.com> wrote:
>>>
>>>> The Java SDK relies on Jackson to do the encoding.
>>>>
>>>> On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <
>>>> chamikara@google.com> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>>
>>>>>> I am working on porting beam to python 3 and discovered the following:
>>>>>>
>>>>>>
>>>>>> Current handling of bytes in bigquery IO:
>>>>>>
>>>>>> When writing bytes to BQ , beam uses
>>>>>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>>>>>> expects byte values to be base-64 encoded*.
>>>>>>
>>>>>> However when writing raw bytes they are currently never transformed
>>>>>> to base-64 encoded strings. This results in the following errors:
>>>>>>
>>>>>> -
>>>>>>
>>>>>> When writing b’abc’ in python 2 this results in actually writing
>>>>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>>>>> -
>>>>>>
>>>>>> When writing b’abc’ in python 3 this results in “TypeError:
>>>>>> b'abc' is not JSON serializable”
>>>>>> -
>>>>>>
>>>>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>>>>>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>>>>>> and -INF values are not JSON compliant”
>>>>>> -
>>>>>>
>>>>>> When reading bytes from BQ they are currently returned as base-64
>>>>>> encoded strings rather then the raw bytes.
>>>>>>
>>>>>>
>>>>>> Example code:
>>>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>>>>>
>>>>>> There is also another issue when writing base-64 encoded string to
>>>>>> BQ. When no schema is specified this results in “Invalid schema update.
>>>>>> Field bytes has changed type from BYTES to STRING”.
>>>>>>
>>>>>> This error can be reproduced when uploading a file (directly in the
>>>>>> BQ UI) to a table with bytes and using schema autodetect.
>>>>>>
>>>>>> Suggested solution:
>>>>>>
>>>>>> I suggest to change BigQuery IO to handle the base-64 encoding as
>>>>>> follows to allow the user to read and write raw bytes in BQ
>>>>>>
>>>>>> Writing data:
>>>>>>
>>>>>> -
>>>>>>
>>>>>> When a new table is created we use the provided schema to detect
>>>>>> bytes and handle the base-64 encoding accordingly
>>>>>> -
>>>>>>
>>>>>> When data is written to an existing table we use the API to get
>>>>>> the schema of the table and handle the base-64 encoding accordingly. We
>>>>>> also pass the schema as argument to avoid the error from schema autodetect.
>>>>>>
>>>>>> Reading data:
>>>>>>
>>>>>> -
>>>>>>
>>>>>> When reading data we also request the schema and handle the
>>>>>> base-64 decoding accordingly to return raw bytes
>>>>>>
>>>>>>
>>>>>> What are your thoughts on this?
>>>>>>
>>>>>
>>>>> Thanks for the update. More context here:
>>>>> https://issues.apache.org/jira/browse/BEAM-6769
>>>>>
>>>>> Suggested solution sounds good to me. BTW do you know how Java SDK
>>>>> handles bytes type ? I believe we write JSON files and execute load jobs
>>>>> there as well (when method is FILE_LOADS).
>>>>>
>>>>> Thanks,
>>>>> Cham
>>>>>
>>>>>
>>>>>>
>>>>>> *I could not find this in the documentation of the API or in the
>>>>>> documentation of BigQuery itself which also expects base-64 encoded values.
>>>>>> I discovered this when uploading a file to BQ UI and getting an error:
>>>>>> "Could not decode base64 string to bytes."
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>> [image: https://ml6.eu] <https://ml6.eu/>
>>>>>>
>>>>>> * Juta Staes*
>>>>>> ML6 Gent
>>>>>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>>>>>
>>>>>> **** DISCLAIMER ****
>>>>>> This email and any files transmitted with it are confidential and
>>>>>> intended solely for the use of the individual or entity to whom they are
>>>>>> addressed. If you have received this email in error please notify the
>>>>>> system manager. This message contains confidential information and is
>>>>>> intended only for the individual named. If you are not the named addressee
>>>>>> you should not disseminate, distribute or copy this e-mail. Please notify
>>>>>> the sender immediately by e-mail if you have received this e-mail by
>>>>>> mistake and delete this e-mail from your system. If you are not the
>>>>>> intended recipient you are notified that disclosing, copying, distributing
>>>>>> or taking any action in reliance on the contents of this information is
>>>>>> strictly prohibited.
>>>>>>
>>>>>
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Wed, Mar 20, 2019 at 6:30 PM Pablo Estrada <pa...@google.com> wrote:
> That sounds reasonable to me, Valentyn.
>
> Regarding (3), when the table already exists, it's not necessary to get
> the schema. BQ is smart enough to load everything in appropriately. (as
> long as bytes fields are base64 encoded)
>
> The problem is when the table does not exist and the user does not provide
> a schema. In that case, there is no simple way of auto-inferring the
> schema, as you correctly point out. I think it's reasonable to simply
> expect users provide schemas if their data will have tricky types to infer.
> Best
> -P.
>
Is this even an option ? I think when table is not available users have to
provide a schema to create a new table.
>
>
> On Wed, Mar 20, 2019 at 3:44 PM Valentyn Tymofieiev <va...@google.com>
> wrote:
>
>> Thanks Juta for detailed analysis.
>>
>> I reached out to BigQuery team to improve documentation around treatment
>> of Bytes and reported the issue that schema autodetection does not work
>> <https://issuetracker.google.com/issues/129006689> for BYTES in GCP
>> issue tracker <https://cloud.google.com/support/docs/issue-trackers>.
>>
>> Is this a correct summary of your proposal?
>>
>> 1. Beam will base64-encode raw bytes, before passing them to BQ over rest
>> API. This will be a change in behavior for Python 2 (for good reasons).
>> 2. When reading data from BQ, all fileds of type BYTES will be
>> base64-decoded.
>> 3. Beam will send an API call to BigQuery to get table schema, whenever
>> schema is not supplied, to work around
>> https://issuetracker.google.com/issues/129006689. Does anyone see any
>> concerns with this? Is it always possible?
>>
>> Thanks,
>> Valentyn
>>
>> On Wed, Mar 20, 2019 at 12:45 PM Reuven Lax <re...@google.com> wrote:
>>
>>> The Java SDK relies on Jackson to do the encoding.
>>>
>>> On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <
>>> chamikara@google.com> wrote:
>>>
>>>>
>>>>
>>>> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>>>>
>>>>> Hi all,
>>>>>
>>>>>
>>>>> I am working on porting beam to python 3 and discovered the following:
>>>>>
>>>>>
>>>>> Current handling of bytes in bigquery IO:
>>>>>
>>>>> When writing bytes to BQ , beam uses
>>>>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>>>>> expects byte values to be base-64 encoded*.
>>>>>
>>>>> However when writing raw bytes they are currently never transformed to
>>>>> base-64 encoded strings. This results in the following errors:
>>>>>
>>>>> -
>>>>>
>>>>> When writing b’abc’ in python 2 this results in actually writing
>>>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>>>> -
>>>>>
>>>>> When writing b’abc’ in python 3 this results in “TypeError: b'abc'
>>>>> is not JSON serializable”
>>>>> -
>>>>>
>>>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>>>>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>>>>> and -INF values are not JSON compliant”
>>>>> -
>>>>>
>>>>> When reading bytes from BQ they are currently returned as base-64
>>>>> encoded strings rather then the raw bytes.
>>>>>
>>>>>
>>>>> Example code:
>>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>>>>
>>>>> There is also another issue when writing base-64 encoded string to BQ.
>>>>> When no schema is specified this results in “Invalid schema update. Field
>>>>> bytes has changed type from BYTES to STRING”.
>>>>>
>>>>> This error can be reproduced when uploading a file (directly in the BQ
>>>>> UI) to a table with bytes and using schema autodetect.
>>>>>
>>>>> Suggested solution:
>>>>>
>>>>> I suggest to change BigQuery IO to handle the base-64 encoding as
>>>>> follows to allow the user to read and write raw bytes in BQ
>>>>>
>>>>> Writing data:
>>>>>
>>>>> -
>>>>>
>>>>> When a new table is created we use the provided schema to detect
>>>>> bytes and handle the base-64 encoding accordingly
>>>>> -
>>>>>
>>>>> When data is written to an existing table we use the API to get
>>>>> the schema of the table and handle the base-64 encoding accordingly. We
>>>>> also pass the schema as argument to avoid the error from schema autodetect.
>>>>>
>>>>> Reading data:
>>>>>
>>>>> -
>>>>>
>>>>> When reading data we also request the schema and handle the
>>>>> base-64 decoding accordingly to return raw bytes
>>>>>
>>>>>
>>>>> What are your thoughts on this?
>>>>>
>>>>
>>>> Thanks for the update. More context here:
>>>> https://issues.apache.org/jira/browse/BEAM-6769
>>>>
>>>> Suggested solution sounds good to me. BTW do you know how Java SDK
>>>> handles bytes type ? I believe we write JSON files and execute load jobs
>>>> there as well (when method is FILE_LOADS).
>>>>
>>>> Thanks,
>>>> Cham
>>>>
>>>>
>>>>>
>>>>> *I could not find this in the documentation of the API or in the
>>>>> documentation of BigQuery itself which also expects base-64 encoded values.
>>>>> I discovered this when uploading a file to BQ UI and getting an error:
>>>>> "Could not decode base64 string to bytes."
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> [image: https://ml6.eu] <https://ml6.eu/>
>>>>>
>>>>> * Juta Staes*
>>>>> ML6 Gent
>>>>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>>>>
>>>>> **** DISCLAIMER ****
>>>>> This email and any files transmitted with it are confidential and
>>>>> intended solely for the use of the individual or entity to whom they are
>>>>> addressed. If you have received this email in error please notify the
>>>>> system manager. This message contains confidential information and is
>>>>> intended only for the individual named. If you are not the named addressee
>>>>> you should not disseminate, distribute or copy this e-mail. Please notify
>>>>> the sender immediately by e-mail if you have received this e-mail by
>>>>> mistake and delete this e-mail from your system. If you are not the
>>>>> intended recipient you are notified that disclosing, copying, distributing
>>>>> or taking any action in reliance on the contents of this information is
>>>>> strictly prohibited.
>>>>>
>>>>
Re: Writing bytes to BigQuery with beam
Posted by Pablo Estrada <pa...@google.com>.
That sounds reasonable to me, Valentyn.
Regarding (3), when the table already exists, it's not necessary to get the
schema. BQ is smart enough to load everything in appropriately. (as long as
bytes fields are base64 encoded)
The problem is when the table does not exist and the user does not provide
a schema. In that case, there is no simple way of auto-inferring the
schema, as you correctly point out. I think it's reasonable to simply
expect users provide schemas if their data will have tricky types to infer.
Best
-P.
On Wed, Mar 20, 2019 at 3:44 PM Valentyn Tymofieiev <va...@google.com>
wrote:
> Thanks Juta for detailed analysis.
>
> I reached out to BigQuery team to improve documentation around treatment
> of Bytes and reported the issue that schema autodetection does not work
> <https://issuetracker.google.com/issues/129006689> for BYTES in GCP issue
> tracker <https://cloud.google.com/support/docs/issue-trackers>.
>
> Is this a correct summary of your proposal?
>
> 1. Beam will base64-encode raw bytes, before passing them to BQ over rest
> API. This will be a change in behavior for Python 2 (for good reasons).
> 2. When reading data from BQ, all fileds of type BYTES will be
> base64-decoded.
> 3. Beam will send an API call to BigQuery to get table schema, whenever
> schema is not supplied, to work around
> https://issuetracker.google.com/issues/129006689. Does anyone see any
> concerns with this? Is it always possible?
>
> Thanks,
> Valentyn
>
> On Wed, Mar 20, 2019 at 12:45 PM Reuven Lax <re...@google.com> wrote:
>
>> The Java SDK relies on Jackson to do the encoding.
>>
>> On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <ch...@google.com>
>> wrote:
>>
>>>
>>>
>>> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>>>
>>>> Hi all,
>>>>
>>>>
>>>> I am working on porting beam to python 3 and discovered the following:
>>>>
>>>>
>>>> Current handling of bytes in bigquery IO:
>>>>
>>>> When writing bytes to BQ , beam uses
>>>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>>>> expects byte values to be base-64 encoded*.
>>>>
>>>> However when writing raw bytes they are currently never transformed to
>>>> base-64 encoded strings. This results in the following errors:
>>>>
>>>> -
>>>>
>>>> When writing b’abc’ in python 2 this results in actually writing
>>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>>> -
>>>>
>>>> When writing b’abc’ in python 3 this results in “TypeError: b'abc'
>>>> is not JSON serializable”
>>>> -
>>>>
>>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>>>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>>>> and -INF values are not JSON compliant”
>>>> -
>>>>
>>>> When reading bytes from BQ they are currently returned as base-64
>>>> encoded strings rather then the raw bytes.
>>>>
>>>>
>>>> Example code:
>>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>>>
>>>> There is also another issue when writing base-64 encoded string to BQ.
>>>> When no schema is specified this results in “Invalid schema update. Field
>>>> bytes has changed type from BYTES to STRING”.
>>>>
>>>> This error can be reproduced when uploading a file (directly in the BQ
>>>> UI) to a table with bytes and using schema autodetect.
>>>>
>>>> Suggested solution:
>>>>
>>>> I suggest to change BigQuery IO to handle the base-64 encoding as
>>>> follows to allow the user to read and write raw bytes in BQ
>>>>
>>>> Writing data:
>>>>
>>>> -
>>>>
>>>> When a new table is created we use the provided schema to detect
>>>> bytes and handle the base-64 encoding accordingly
>>>> -
>>>>
>>>> When data is written to an existing table we use the API to get the
>>>> schema of the table and handle the base-64 encoding accordingly. We also
>>>> pass the schema as argument to avoid the error from schema autodetect.
>>>>
>>>> Reading data:
>>>>
>>>> -
>>>>
>>>> When reading data we also request the schema and handle the base-64
>>>> decoding accordingly to return raw bytes
>>>>
>>>>
>>>> What are your thoughts on this?
>>>>
>>>
>>> Thanks for the update. More context here:
>>> https://issues.apache.org/jira/browse/BEAM-6769
>>>
>>> Suggested solution sounds good to me. BTW do you know how Java SDK
>>> handles bytes type ? I believe we write JSON files and execute load jobs
>>> there as well (when method is FILE_LOADS).
>>>
>>> Thanks,
>>> Cham
>>>
>>>
>>>>
>>>> *I could not find this in the documentation of the API or in the
>>>> documentation of BigQuery itself which also expects base-64 encoded values.
>>>> I discovered this when uploading a file to BQ UI and getting an error:
>>>> "Could not decode base64 string to bytes."
>>>>
>>>>
>>>> --
>>>>
>>>> [image: https://ml6.eu] <https://ml6.eu/>
>>>>
>>>> * Juta Staes*
>>>> ML6 Gent
>>>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>>>
>>>> **** DISCLAIMER ****
>>>> This email and any files transmitted with it are confidential and
>>>> intended solely for the use of the individual or entity to whom they are
>>>> addressed. If you have received this email in error please notify the
>>>> system manager. This message contains confidential information and is
>>>> intended only for the individual named. If you are not the named addressee
>>>> you should not disseminate, distribute or copy this e-mail. Please notify
>>>> the sender immediately by e-mail if you have received this e-mail by
>>>> mistake and delete this e-mail from your system. If you are not the
>>>> intended recipient you are notified that disclosing, copying, distributing
>>>> or taking any action in reliance on the contents of this information is
>>>> strictly prohibited.
>>>>
>>>
Re: Writing bytes to BigQuery with beam
Posted by Valentyn Tymofieiev <va...@google.com>.
Thanks Juta for detailed analysis.
I reached out to BigQuery team to improve documentation around treatment of
Bytes and reported the issue that schema autodetection does not work
<https://issuetracker.google.com/issues/129006689> for BYTES in GCP issue
tracker <https://cloud.google.com/support/docs/issue-trackers>.
Is this a correct summary of your proposal?
1. Beam will base64-encode raw bytes, before passing them to BQ over rest
API. This will be a change in behavior for Python 2 (for good reasons).
2. When reading data from BQ, all fileds of type BYTES will be
base64-decoded.
3. Beam will send an API call to BigQuery to get table schema, whenever
schema is not supplied, to work around
https://issuetracker.google.com/issues/129006689. Does anyone see any
concerns with this? Is it always possible?
Thanks,
Valentyn
On Wed, Mar 20, 2019 at 12:45 PM Reuven Lax <re...@google.com> wrote:
> The Java SDK relies on Jackson to do the encoding.
>
> On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <ch...@google.com>
> wrote:
>
>>
>>
>> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>>
>>> Hi all,
>>>
>>>
>>> I am working on porting beam to python 3 and discovered the following:
>>>
>>>
>>> Current handling of bytes in bigquery IO:
>>>
>>> When writing bytes to BQ , beam uses
>>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>>> expects byte values to be base-64 encoded*.
>>>
>>> However when writing raw bytes they are currently never transformed to
>>> base-64 encoded strings. This results in the following errors:
>>>
>>> -
>>>
>>> When writing b’abc’ in python 2 this results in actually writing
>>> b'i\xb7' which is the same as base64.b64decode('abc='))
>>> -
>>>
>>> When writing b’abc’ in python 3 this results in “TypeError: b'abc'
>>> is not JSON serializable”
>>> -
>>>
>>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>>> and -INF values are not JSON compliant”
>>> -
>>>
>>> When reading bytes from BQ they are currently returned as base-64
>>> encoded strings rather then the raw bytes.
>>>
>>>
>>> Example code:
>>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>>
>>> There is also another issue when writing base-64 encoded string to BQ.
>>> When no schema is specified this results in “Invalid schema update. Field
>>> bytes has changed type from BYTES to STRING”.
>>>
>>> This error can be reproduced when uploading a file (directly in the BQ
>>> UI) to a table with bytes and using schema autodetect.
>>>
>>> Suggested solution:
>>>
>>> I suggest to change BigQuery IO to handle the base-64 encoding as
>>> follows to allow the user to read and write raw bytes in BQ
>>>
>>> Writing data:
>>>
>>> -
>>>
>>> When a new table is created we use the provided schema to detect
>>> bytes and handle the base-64 encoding accordingly
>>> -
>>>
>>> When data is written to an existing table we use the API to get the
>>> schema of the table and handle the base-64 encoding accordingly. We also
>>> pass the schema as argument to avoid the error from schema autodetect.
>>>
>>> Reading data:
>>>
>>> -
>>>
>>> When reading data we also request the schema and handle the base-64
>>> decoding accordingly to return raw bytes
>>>
>>>
>>> What are your thoughts on this?
>>>
>>
>> Thanks for the update. More context here:
>> https://issues.apache.org/jira/browse/BEAM-6769
>>
>> Suggested solution sounds good to me. BTW do you know how Java SDK
>> handles bytes type ? I believe we write JSON files and execute load jobs
>> there as well (when method is FILE_LOADS).
>>
>> Thanks,
>> Cham
>>
>>
>>>
>>> *I could not find this in the documentation of the API or in the
>>> documentation of BigQuery itself which also expects base-64 encoded values.
>>> I discovered this when uploading a file to BQ UI and getting an error:
>>> "Could not decode base64 string to bytes."
>>>
>>>
>>> --
>>>
>>> [image: https://ml6.eu] <https://ml6.eu/>
>>>
>>> * Juta Staes*
>>> ML6 Gent
>>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>>
>>> **** DISCLAIMER ****
>>> This email and any files transmitted with it are confidential and
>>> intended solely for the use of the individual or entity to whom they are
>>> addressed. If you have received this email in error please notify the
>>> system manager. This message contains confidential information and is
>>> intended only for the individual named. If you are not the named addressee
>>> you should not disseminate, distribute or copy this e-mail. Please notify
>>> the sender immediately by e-mail if you have received this e-mail by
>>> mistake and delete this e-mail from your system. If you are not the
>>> intended recipient you are notified that disclosing, copying, distributing
>>> or taking any action in reliance on the contents of this information is
>>> strictly prohibited.
>>>
>>
Re: Writing bytes to BigQuery with beam
Posted by Reuven Lax <re...@google.com>.
The Java SDK relies on Jackson to do the encoding.
On Wed, Mar 20, 2019 at 11:33 AM Chamikara Jayalath <ch...@google.com>
wrote:
>
>
> On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
>
>> Hi all,
>>
>>
>> I am working on porting beam to python 3 and discovered the following:
>>
>>
>> Current handling of bytes in bigquery IO:
>>
>> When writing bytes to BQ , beam uses
>> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
>> expects byte values to be base-64 encoded*.
>>
>> However when writing raw bytes they are currently never transformed to
>> base-64 encoded strings. This results in the following errors:
>>
>> -
>>
>> When writing b’abc’ in python 2 this results in actually writing
>> b'i\xb7' which is the same as base64.b64decode('abc='))
>> -
>>
>> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is
>> not JSON serializable”
>> -
>>
>> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8'
>> codec can't decode byte 0xab in position 0: invalid start byte. NAN, INF
>> and -INF values are not JSON compliant”
>> -
>>
>> When reading bytes from BQ they are currently returned as base-64
>> encoded strings rather then the raw bytes.
>>
>>
>> Example code:
>> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>>
>> There is also another issue when writing base-64 encoded string to BQ.
>> When no schema is specified this results in “Invalid schema update. Field
>> bytes has changed type from BYTES to STRING”.
>>
>> This error can be reproduced when uploading a file (directly in the BQ
>> UI) to a table with bytes and using schema autodetect.
>>
>> Suggested solution:
>>
>> I suggest to change BigQuery IO to handle the base-64 encoding as follows
>> to allow the user to read and write raw bytes in BQ
>>
>> Writing data:
>>
>> -
>>
>> When a new table is created we use the provided schema to detect
>> bytes and handle the base-64 encoding accordingly
>> -
>>
>> When data is written to an existing table we use the API to get the
>> schema of the table and handle the base-64 encoding accordingly. We also
>> pass the schema as argument to avoid the error from schema autodetect.
>>
>> Reading data:
>>
>> -
>>
>> When reading data we also request the schema and handle the base-64
>> decoding accordingly to return raw bytes
>>
>>
>> What are your thoughts on this?
>>
>
> Thanks for the update. More context here:
> https://issues.apache.org/jira/browse/BEAM-6769
>
> Suggested solution sounds good to me. BTW do you know how Java SDK handles
> bytes type ? I believe we write JSON files and execute load jobs there as
> well (when method is FILE_LOADS).
>
> Thanks,
> Cham
>
>
>>
>> *I could not find this in the documentation of the API or in the
>> documentation of BigQuery itself which also expects base-64 encoded values.
>> I discovered this when uploading a file to BQ UI and getting an error:
>> "Could not decode base64 string to bytes."
>>
>>
>> --
>>
>> [image: https://ml6.eu] <https://ml6.eu/>
>>
>> * Juta Staes*
>> ML6 Gent
>> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>>
>> **** DISCLAIMER ****
>> This email and any files transmitted with it are confidential and
>> intended solely for the use of the individual or entity to whom they are
>> addressed. If you have received this email in error please notify the
>> system manager. This message contains confidential information and is
>> intended only for the individual named. If you are not the named addressee
>> you should not disseminate, distribute or copy this e-mail. Please notify
>> the sender immediately by e-mail if you have received this e-mail by
>> mistake and delete this e-mail from your system. If you are not the
>> intended recipient you are notified that disclosing, copying, distributing
>> or taking any action in reliance on the contents of this information is
>> strictly prohibited.
>>
>
Re: Writing bytes to BigQuery with beam
Posted by Chamikara Jayalath <ch...@google.com>.
On Wed, Mar 20, 2019 at 5:46 AM Juta Staes <ju...@ml6.eu> wrote:
> Hi all,
>
>
> I am working on porting beam to python 3 and discovered the following:
>
>
> Current handling of bytes in bigquery IO:
>
> When writing bytes to BQ , beam uses
> https://cloud.google.com/bigquery/docs/reference/rest/v2/. This API
> expects byte values to be base-64 encoded*.
>
> However when writing raw bytes they are currently never transformed to
> base-64 encoded strings. This results in the following errors:
>
> -
>
> When writing b’abc’ in python 2 this results in actually writing
> b'i\xb7' which is the same as base64.b64decode('abc='))
> -
>
> When writing b’abc’ in python 3 this results in “TypeError: b'abc' is
> not JSON serializable”
> -
>
> When writing b’\xab’ in py2/py3 this gives a “ValueError: 'utf8' codec
> can't decode byte 0xab in position 0: invalid start byte. NAN, INF and -INF
> values are not JSON compliant”
> -
>
> When reading bytes from BQ they are currently returned as base-64
> encoded strings rather then the raw bytes.
>
>
> Example code:
> https://docs.google.com/document/d/19zvDycWzF82MmtCmxrhqqyXKaRq8slRIjdxE6E8MObA/edit?usp=sharing
>
> There is also another issue when writing base-64 encoded string to BQ.
> When no schema is specified this results in “Invalid schema update. Field
> bytes has changed type from BYTES to STRING”.
>
> This error can be reproduced when uploading a file (directly in the BQ UI)
> to a table with bytes and using schema autodetect.
>
> Suggested solution:
>
> I suggest to change BigQuery IO to handle the base-64 encoding as follows
> to allow the user to read and write raw bytes in BQ
>
> Writing data:
>
> -
>
> When a new table is created we use the provided schema to detect bytes
> and handle the base-64 encoding accordingly
> -
>
> When data is written to an existing table we use the API to get the
> schema of the table and handle the base-64 encoding accordingly. We also
> pass the schema as argument to avoid the error from schema autodetect.
>
> Reading data:
>
> -
>
> When reading data we also request the schema and handle the base-64
> decoding accordingly to return raw bytes
>
>
> What are your thoughts on this?
>
Thanks for the update. More context here:
https://issues.apache.org/jira/browse/BEAM-6769
Suggested solution sounds good to me. BTW do you know how Java SDK handles
bytes type ? I believe we write JSON files and execute load jobs there as
well (when method is FILE_LOADS).
Thanks,
Cham
>
> *I could not find this in the documentation of the API or in the
> documentation of BigQuery itself which also expects base-64 encoded values.
> I discovered this when uploading a file to BQ UI and getting an error:
> "Could not decode base64 string to bytes."
>
>
> --
>
> [image: https://ml6.eu] <https://ml6.eu/>
>
> * Juta Staes*
> ML6 Gent
> <https://www.google.be/maps/place/ML6/@51.037408,3.7044893,17z/data=!3m1!4b1!4m5!3m4!1s0x47c37161feeca14b:0xb8f72585fdd21c90!8m2!3d51.037408!4d3.706678?hl=nl>
>
> **** DISCLAIMER ****
> This email and any files transmitted with it are confidential and intended
> solely for the use of the individual or entity to whom they are addressed.
> If you have received this email in error please notify the system manager.
> This message contains confidential information and is intended only for the
> individual named. If you are not the named addressee you should not
> disseminate, distribute or copy this e-mail. Please notify the sender
> immediately by e-mail if you have received this e-mail by mistake and
> delete this e-mail from your system. If you are not the intended recipient
> you are notified that disclosing, copying, distributing or taking any
> action in reliance on the contents of this information is strictly
> prohibited.
>