{"id":131,"date":"2024-09-07T13:31:00","date_gmt":"2024-09-07T11:31:00","guid":{"rendered":"https:\/\/codeswarm.io\/?p=131"},"modified":"2026-07-19T13:44:02","modified_gmt":"2026-07-19T11:44:02","slug":"apache-iceberg-partitioning-data-with-scala-spark","status":"publish","type":"post","link":"https:\/\/codeswarm.io\/?p=131","title":{"rendered":"Apache Iceberg partitioning data with Scala\/Spark"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Partitioning helps improve query performance by reducing the amount of data scanned during read operations. Iceberg allows dynamic partitioning and supports various partition types such as <strong>identity<\/strong>, <strong>bucket<\/strong>, and <strong>truncate<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example: partitioning data by a column (e.g., Age)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s partition the table by a column like <code>age<\/code> (bucket partitioning).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scala\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def partitionTable(spark: SparkSession): Unit = {\n  \/\/ Create a new partitioned table\n  spark.sql(\n    \"\"\"\n      |CREATE TABLE IF NOT EXISTS spark_catalog.default.iceberg_partitioned_table (\n      | id BIGINT,\n      | name STRING,\n      | age INT\n      |) USING iceberg\n      | PARTITIONED BY (bucket(5, age))\n      |\"\"\".stripMargin)\n  println(\"Partitioned Iceberg table created!\")\n}\n\/\/ To execute:\npartitionTable(spark)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we\u2019ve partitioned the table using the <code>age<\/code> column into 5 buckets. This is useful for optimizing queries when filtering based on <code>age<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Query example with partitioned table<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you query partitioned data, Iceberg only scans the partitions relevant to the query:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scala\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def queryPartitionedTable(spark: SparkSession): Unit = {\n  val partitionedDF = spark.sql(\"SELECT * FROM spark_catalog.default.iceberg_partitioned_table WHERE age > 25\")\n  partitionedDF.show()\n}\n\/\/ To execute:\nqueryPartitionedTable(spark)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Partitioning helps improve query performance by reducing the amount of data scanned during read operations. Iceberg allows dynamic partitioning and supports various partition types such as identity, bucket, and truncate. Example: partitioning data by a column (e.g., Age) Let\u2019s partition the table by a column like age (bucket partitioning). Here, we\u2019ve partitioned the table &hellip; <a href=\"https:\/\/codeswarm.io\/?p=131\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Apache Iceberg partitioning data with Scala\/Spark<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,23],"tags":[21,19],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-big-data","category-programming","tag-iceberg","tag-scala"],"_links":{"self":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":2,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/codeswarm.io\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeswarm.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}