samredai commented on code in PR #75: URL: https://github.com/apache/iceberg-docs/pull/75#discussion_r858860786
########## landing-page/content/common/spark-quickstart.md: ########## @@ -0,0 +1,370 @@ +--- +url: spark-quickstart +toc: true +aliases: + - "spark-quickstart" +--- +<!-- + - Licensed to the Apache Software Foundation (ASF) under one or more + - contributor license agreements. See the NOTICE file distributed with + - this work for additional information regarding copyright ownership. + - The ASF licenses this file to You 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. + --> + +# Getting Started With Spark and Iceberg + +Spark is currently the most feature-rich compute engine for Iceberg operations. +We recommend you to get started with Spark to understand Iceberg concepts and features with examples. +You can also view documentations of using Iceberg with other compute engine under the +[**Compute Frameworks**](/compute-frameworks) tab. + +## Docker Quickstart (Spark) + +The fastest way to get started is to use a docker-compose file to launch a local Spark cluster with a configured +Iceberg catalog. Save the following into a file named `docker-compose.yml`. + +```yaml +version: "3" + +services: + spark-iceberg: + image: tabulario/spark-iceberg + depends_on: + - postgres + container_name: spark-iceberg + environment: + - SPARK_HOME=/opt/spark + - PYSPARK_PYTON=/usr/bin/python3.9 + - PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/spark/bin + volumes: + - ./warehouse:/home/iceberg/warehouse + - ./notebooks:/home/iceberg/notebooks/notebooks + ports: + - 8888:8888 + - 8080:8080 + - 18080:18080 + postgres: + image: postgres:13.4-bullseye + container_name: postgres + environment: + - POSTGRES_USER=admin + - POSTGRES_PASSWORD=password + - POSTGRES_DB=demo_catalog + volumes: + - ./postgres/data:/var/lib/postgresql/data +``` + +Next, run the following to start up the docker containers. +```sh +docker-compose up +``` + +You can then run any of the following to start a spark session. + +{{% codetabs "LaunchSparkClient" %}} +{{% addtab "SparkSQL" checked %}} +{{% addtab "SparkShell" %}} +{{% addtab "PySpark" %}} +{{% addtab "Notebook" %}} +{{% tabcontent "SparkSQL" %}} +```sh +docker exec -it spark-iceberg spark-sql +``` +{{% /tabcontent %}} +{{% tabcontent "SparkShell" %}} +```sh +docker exec -it spark-iceberg spark-shell +``` +{{% /tabcontent %}} +{{% tabcontent "PySpark" %}} +```sh +docker exec -it spark-iceberg pyspark +``` +{{% /tabcontent %}} +{{% tabcontent "Notebook" %}} +```sh +docker exec -it spark-iceberg notebook +``` +{{< hint warning >}} +The notebook server will be available at [http://localhost:8888](http://localhost:8888) +{{< /hint >}} +{{% /tabcontent %}} +{{% /codetabs %}} + + +### Creating a table + +To create your first Iceberg table in Spark, run a [`CREATE TABLE`](../spark-ddl#create-table) command. + + +{{% codetabs "CreatATable" %}} +{{% addtab "SparkSQL" checked %}} +{{% addtab "SparkShell" %}} +{{% addtab "PySpark" %}} +{{% tabcontent "SparkSQL" %}} +```sql +CREATE TABLE prod.nyc.taxis +( + VendorID bigint, + TripID bigint, + Trip_distance float, + Fare_amount double, + Store_and_fwd_flag string +) +USING iceberg; +``` +{{% /tabcontent %}} +{{% tabcontent "SparkShell" %}} +```scala +import org.apache.spark.sql.types.{DoubleType, FloatType, LongType, StringType, StructField, StructType} +import org.apache.spark.sql.Row +val schema = StructType( Array( + StructField("VendorID", LongType,true), + StructField("TripID", LongType,true), + StructField("Trip_distance", FloatType,true), + StructField("Fare_amount", DoubleType,true), + StructField("Store_and_fwd_flag", StringType,true) +)) +val df = spark.createDataFrame(spark.sparkContext.emptyRDD[Row],schema) +df.writeTo("prod.nyc.taxis").create() +``` +{{% /tabcontent %}} +{{% tabcontent "PySpark" %}} +```py +from pyspark.sql.types import DoubleType, FloatType, LongType, StructType,StructField, StringType +schema = StructType([ + StructField("VendorID", LongType(), True), + StructField("TripID", LongType(), True), + StructField("Trip_distance", FloatType(), True), + StructField("Fare_amount', DoubleType(), True), + StructField("Store_and_fwd_flag', StringType(), True) +]) + +df = spark.createDataFrame([], schema) +df.writeTo("prod.nyc.taxis").create() +``` +{{% /tabcontent %}} +{{% /codetabs %}} + +Iceberg catalogs support the full range of SQL DDL commands, including: + +* [`CREATE TABLE ... PARTITIONED BY`](../spark-ddl#create-table) +* [`CREATE TABLE ... AS SELECT`](../spark-ddl#create-table--as-select) +* [`ALTER TABLE`](../spark-ddl#alter-table) +* [`DROP TABLE`](../spark-ddl#drop-table) + +### Writing + +Once your table is created, insert data using [`INSERT INTO`](../spark-writes#insert-into): + +{{% codetabs "InsertData" %}} +{{% addtab "SparkSQL" checked %}} +{{% addtab "SparkShell" %}} +{{% addtab "PySpark" %}} +{{% tabcontent "SparkSQL" %}} +```sql +INSERT INTO prod.nyc.taxis +VALUES (1, 1000371, 1.8, 15.32, 'N'), (2, 1000372, 2.5, 22.15, 'N'), (2, 1000373, 0.9, 9.01, 'N'), (1, 1000374, 8.4, 42.13, 'Y'); +``` +{{% /tabcontent %}} +{{% tabcontent "SparkShell" %}} +```scala +import org.apache.spark.sql.Row + +val schema = spark.table("prod.nyc.taxis").schema +val data = Seq( + Row(1: Long, 1000371: Long, 1.8f: Float, 15.32: Double, "N": String), + Row(2: Long, 1000372: Long, 2.5f: Float, 22.15: Double, "N": String), + Row(2: Long, 1000373: Long, 0.9f: Float, 9.01: Double, "N": String), + Row(1: Long, 1000374: Long, 8.4f: Float, 42.13: Double, "Y": String) +) +val df = spark.createDataFrame(spark.sparkContext.parallelize(data), schema) +df.writeTo("prod.nyc.taxis").append() +``` +{{% /tabcontent %}} +{{% tabcontent "PySpark" %}} +```py +schema = spark.table("prod.nyc.taxis").schema +data = [ + (1, 1000371, 1.8, 15.32, "N"), + (2, 1000372, 2.5, 22.15, "N"), + (2, 1000373, 0.9, 9.01, "N"), + (1, 1000374, 8.4, 42.13, "Y") + ] +df = spark.createDataFrame(data, schema) +df.writeTo("prod.nyc.taxis").append() +``` +{{% /tabcontent %}} +{{% /codetabs %}} + +### Reading + +To read a table, simply use the Iceberg table's name: + +{{% codetabs "SelectData" %}} +{{% addtab "SparkSQL" checked %}} +{{% addtab "SparkShell" %}} +{{% addtab "PySpark" %}} +{{% tabcontent "SparkSQL" %}} +```sql +SELECT * FROM prod.nyc.taxis; +``` +{{% /tabcontent %}} +{{% tabcontent "SparkShell" %}} +```scala +val df = spark.table("prod.nyc.taxis").show() +``` +{{% /tabcontent %}} +{{% tabcontent "PySpark" %}} +```py +df = spark.table("prod.nyc.taxis").show() +``` +{{% /tabcontent %}} +{{% /codetabs %}} + +For [inspecting tables](../spark-queries#inspecting-tables) SQL is recommended. To view all of the snapshots in a table, use the `snapshots` metadata table: +```sql +SELECT committed_at, snapshot_id, parent_id, operation FROM prod.nyc.taxis.snapshots; +``` +``` +2022-04-25 23:23:49.692 6317817083814616920 NULL append +``` + +### Row-Level SQL Updates + +Iceberg also adds row-level SQL updates to Spark, [`MERGE INTO`](../spark-writes#merge-into) and [`DELETE FROM`](../spark-writes#delete-from). For example, +if you wanted to merge in records from a table named `prod.nyc.updates`, you could run a `MERGE INTO` command to merge the updates into the `prod.nyc.taxis` +table and simply add the `Fare_amount` for records where the `VendorID` and `Trip_ID` already exist. + +First, create the `updates` table. +```sql +CREATE TABLE prod.nyc.updates +( + VendorID bigint, + TripID bigint, + Trip_distance float, + Fare_amount double, + Store_and_fwd_flag string +) +USING iceberg; +``` +Next, insert a few records. +```sql +INSERT INTO prod.nyc.updates +VALUES (1, 1000371, 1.8, 0.50, 'N'), (3, 1000375, 8.9, 37.15, 'Y'); +``` +Finally, merge the `updates` table into the `taxis` table. +```sql +MERGE INTO prod.nyc.taxis t USING (SELECT * FROM prod.nyc.updates) u ON t.VendorID = u.VendorID and t.TripID = u.TripID +WHEN MATCHED THEN UPDATE SET t.Fare_amount = t.Fare_amount + u.Fare_amount +WHEN NOT MATCHED THEN INSERT *; +``` +Querying the `taxis` table, you can see that the fare amount of `0.50` has been added to `15.32` for `VendorID = 1 AND TripID = 1000371`. +```sql +SELECT VendorID, TripID, Fare_amount +FROM prod.nyc.taxis +WHERE VendorID = 1 AND TripID = 1000371; +``` +``` +1 1000371 15.82 +``` +{{< hint warning >}} +Iceberg supports writing DataFrames using the new [v2 DataFrame write API](../spark-writes#writing-with-dataframes). The old `write` API is supported, but _not_ recommended. +{{< /hint >}} + +## Adding Iceberg to your Existing Spark Environment Review Comment: You're totally right, I added in: > To create your first Iceberg table in Spark, run a `CREATE TABLE` command. In the following example, we'll create a table using `prod.nyc.taxis` where `prod` is the catalog name, `nyc` is the schema name, and `taxis` is the table name. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
