blob: 69addef310c9f1b1714272bd319bd9c5ea492439 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/apache/beam/blob/master//Users/dcavazos/src/beam/examples/notebooks/documentation/transforms/python/elementwise/tostring-py.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "view-the-docs-top"
},
"source": [
"<table align=\"left\"><td><a target=\"_blank\" href=\"https://beam.apache.org/documentation/transforms/python/elementwise/tostring\"><img src=\"https://beam.apache.org/images/logos/full-color/name-bottom/beam-logo-full-color-name-bottom-100.png\" width=\"32\" height=\"32\" />View the docs</a></td></table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "_-code"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\")\n",
"# Licensed to the Apache Software Foundation (ASF) under one\n",
"# or more contributor license agreements. See the NOTICE file\n",
"# distributed with this work for additional information\n",
"# regarding copyright ownership. The ASF licenses this file\n",
"# to you under the Apache License, Version 2.0 (the\n",
"# \"License\"); you may not use this file except in compliance\n",
"# with the License. You may obtain a copy of the License at\n",
"#\n",
"# http://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing,\n",
"# software distributed under the License is distributed on an\n",
"# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
"# KIND, either express or implied. See the License for the\n",
"# specific language governing permissions and limitations\n",
"# under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tostring"
},
"source": [
"# ToString\n",
"\n",
"<script type=\"text/javascript\">\n",
"localStorage.setItem('language', 'language-py')\n",
"</script>\n",
"\n",
"<table align=\"left\" style=\"margin-right:1em\">\n",
" <td>\n",
" <a class=\"button\" target=\"_blank\" href=\"https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.util.html#apache_beam.transforms.util.ToString\"><img src=\"https://beam.apache.org/images/logos/sdks/python.png\" width=\"32px\" height=\"32px\" alt=\"Pydoc\"/> Pydoc</a>\n",
" </td>\n",
"</table>\n",
"\n",
"<br/><br/><br/>\n",
"\n",
"Transforms every element in an input collection to a string."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "setup"
},
"source": [
"## Setup\n",
"\n",
"To run a code cell, you can click the **Run cell** button at the top left of the cell,\n",
"or select it and press **`Shift+Enter`**.\n",
"Try modifying a code cell and re-running it to see what happens.\n",
"\n",
"> To learn more about Colab, see\n",
"> [Welcome to Colaboratory!](https://colab.sandbox.google.com/notebooks/welcome.ipynb).\n",
"\n",
"First, let's install the `apache-beam` module."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "setup-code"
},
"outputs": [],
"source": [
"!pip install --quiet -U apache-beam"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "examples"
},
"source": [
"## Examples\n",
"\n",
"Any non-string element can be converted to a string using standard Python functions and methods.\n",
"Many I/O transforms, such as\n",
"[`textio.WriteToText`](https://beam.apache.org/releases/pydoc/current/apache_beam.io.textio.html#apache_beam.io.textio.WriteToText),\n",
"expect their input elements to be strings."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-1-key-value-pairs-to-string"
},
"source": [
"### Example 1: Key-value pairs to string\n",
"\n",
"The following example converts a `(key, value)` pair into a string delimited by `','`.\n",
"You can specify a different delimiter using the `delimiter` argument."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "example-1-key-value-pairs-to-string-code"
},
"outputs": [],
"source": [
"import apache_beam as beam\n",
"\n",
"with beam.Pipeline() as pipeline:\n",
" plants = (\n",
" pipeline\n",
" | 'Garden plants' >> beam.Create([\n",
" ('🍓', 'Strawberry'),\n",
" ('🥕', 'Carrot'),\n",
" ('🍆', 'Eggplant'),\n",
" ('🍅', 'Tomato'),\n",
" ('🥔', 'Potato'),\n",
" ])\n",
" | 'To string' >> beam.ToString.Kvs()\n",
" | beam.Map(print)\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-1-key-value-pairs-to-string-2"
},
"source": [
"<table align=\"left\" style=\"margin-right:1em\">\n",
" <td>\n",
" <a class=\"button\" target=\"_blank\" href=\"https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/elementwise/tostring.py\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" width=\"32px\" height=\"32px\" alt=\"View source code\"/> View source code</a>\n",
" </td>\n",
"</table>\n",
"\n",
"<br/><br/><br/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-2-elements-to-string"
},
"source": [
"### Example 2: Elements to string\n",
"\n",
"The following example converts a dictionary into a string.\n",
"The string output will be equivalent to `str(element)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "example-2-elements-to-string-code"
},
"outputs": [],
"source": [
"import apache_beam as beam\n",
"\n",
"with beam.Pipeline() as pipeline:\n",
" plant_lists = (\n",
" pipeline\n",
" | 'Garden plants' >> beam.Create([\n",
" ['🍓', 'Strawberry', 'perennial'],\n",
" ['🥕', 'Carrot', 'biennial'],\n",
" ['🍆', 'Eggplant', 'perennial'],\n",
" ['🍅', 'Tomato', 'annual'],\n",
" ['🥔', 'Potato', 'perennial'],\n",
" ])\n",
" | 'To string' >> beam.ToString.Element()\n",
" | beam.Map(print)\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-2-elements-to-string-2"
},
"source": [
"<table align=\"left\" style=\"margin-right:1em\">\n",
" <td>\n",
" <a class=\"button\" target=\"_blank\" href=\"https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/elementwise/tostring.py\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" width=\"32px\" height=\"32px\" alt=\"View source code\"/> View source code</a>\n",
" </td>\n",
"</table>\n",
"\n",
"<br/><br/><br/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-3-iterables-to-string"
},
"source": [
"### Example 3: Iterables to string\n",
"\n",
"The following example converts an iterable, in this case a list of strings,\n",
"into a string delimited by `','`.\n",
"You can specify a different delimiter using the `delimiter` argument.\n",
"The string output will be equivalent to `iterable.join(delimiter)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "example-3-iterables-to-string-code"
},
"outputs": [],
"source": [
"import apache_beam as beam\n",
"\n",
"with beam.Pipeline() as pipeline:\n",
" plants_csv = (\n",
" pipeline\n",
" | 'Garden plants' >> beam.Create([\n",
" ['🍓', 'Strawberry', 'perennial'],\n",
" ['🥕', 'Carrot', 'biennial'],\n",
" ['🍆', 'Eggplant', 'perennial'],\n",
" ['🍅', 'Tomato', 'annual'],\n",
" ['🥔', 'Potato', 'perennial'],\n",
" ])\n",
" | 'To string' >> beam.ToString.Iterables()\n",
" | beam.Map(print)\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "example-3-iterables-to-string-2"
},
"source": [
"<table align=\"left\" style=\"margin-right:1em\">\n",
" <td>\n",
" <a class=\"button\" target=\"_blank\" href=\"https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/snippets/transforms/elementwise/tostring.py\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" width=\"32px\" height=\"32px\" alt=\"View source code\"/> View source code</a>\n",
" </td>\n",
"</table>\n",
"\n",
"<br/><br/><br/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "related-transforms"
},
"source": [
"## Related transforms\n",
"\n",
"* [Map](https://beam.apache.org/documentation/transforms/python/elementwise/map) applies a simple 1-to-1 mapping function over each element in the collection\n",
"\n",
"<table align=\"left\" style=\"margin-right:1em\">\n",
" <td>\n",
" <a class=\"button\" target=\"_blank\" href=\"https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.util.html#apache_beam.transforms.util.ToString\"><img src=\"https://beam.apache.org/images/logos/sdks/python.png\" width=\"32px\" height=\"32px\" alt=\"Pydoc\"/> Pydoc</a>\n",
" </td>\n",
"</table>\n",
"\n",
"<br/><br/><br/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "view-the-docs-bottom"
},
"source": [
"<table align=\"left\"><td><a target=\"_blank\" href=\"https://beam.apache.org/documentation/transforms/python/elementwise/tostring\"><img src=\"https://beam.apache.org/images/logos/full-color/name-bottom/beam-logo-full-color-name-bottom-100.png\" width=\"32\" height=\"32\" />View the docs</a></td></table>"
]
}
],
"metadata": {
"colab": {
"name": "ToString - element-wise transform",
"toc_visible": true
},
"kernelspec": {
"display_name": "python3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}