04 Bigquery
04 Bigquery
04 Bigquery
[32]: %%bigquery
SELECT * FROM sparktobq.kdd_cup_raw LIMIT 5
1
3 238 tcp IRC RSTO 132
4 0 tcp IRC RSTO 0
double_field_40 string_field_41
0 1.00 normal.
1 0.88 normal.
2 0.85 normal.
3 0.73 normal.
4 0.74 normal.
[5 rows x 42 columns]
[33]: %%bigquery
SELECT
int64_field_0 AS duration,
string_field_1 AS protocol_type,
string_field_2 AS service,
string_field_3 AS flag,
int64_field_4 AS src_bytes,
int64_field_5 AS dst_bytes,
int64_field_6 AS wrong_fragment,
2
int64_field_7 AS urgent,
int64_field_8 AS hot,
int64_field_9 AS num_failed_logins,
int64_field_11 AS num_compromised,
int64_field_13 AS su_attempted,
int64_field_14 AS num_root,
int64_field_15 AS num_file_creations,
string_field_41 AS label
FROM
sparktobq.kdd_cup_raw
[34]: %%bigquery
SELECT * FROM sparktobq.kdd_cup LIMIT 5
num_file_creations label
0 0 normal.
1 0 normal.
2 0 normal.
3 0 satan.
4 0 normal.
3
[35]: %%bigquery connections_by_protocol
SELECT COUNT(*) AS count
FROM sparktobq.kdd_cup
GROUP BY protocol_type
ORDER by count ASC
[36]: connections_by_protocol
[36]: count
0 20354
1 190065
2 283602
4
5
0.1.6 Write out report
Copy the output to GCS so that we can safely delete the AI Platform Notebooks instance.
# save locally
ax[0].get_figure().savefig('report.png');
connections_by_protocol.to_csv("connections_by_protocol.csv")
# upload to GCS
bucket = gcs.Client().get_bucket(BUCKET)
for blob in bucket.list_blobs(prefix='sparktobq/'):
blob.delete()
for fname in ['report.png', 'connections_by_protocol.csv']:
bucket.blob('sparktobq/{}'.format(fname)).upload_from_filename(fname)
Copyright 2019 Google Inc. Licensed under the Apache License, Version 2.0 (the “License”); you
may not use this file except in compliance with the License. You may obtain a copy of the License
at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to
in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.