Stand Up the Stack
Why the catalog gets a tier of its own
An Iceberg table is a tree of immutable files plus one mutable pointer naming the current metadata.json. Every design decision about catalogs comes down to where that pointer lives and what guarantees the thing holding it can offer. The cheapest possible answer is a file next to the table, updated by an atomic rename — that's Iceberg's HadoopCatalog, it needs nothing but a directory, and the Concurrency control part of the main guide already explained why it falls apart the moment two writers or object storage are involved. The Lab: the filesystem catalog part shows you that setup and exactly what it costs.
This lab uses the next answer up: a Hive Metastore backed by Postgres. The pointer becomes a row in a relational database, and swapping it becomes a database operation with a lock or a compare-and-swap around it. Two containers, one docker compose up, and the rest of the lab is unchanged.
| Pointer lives in | A Postgres row |
|---|---|
| Data files live in | Your local filesystem |
| Docker containers | 2 |
| Runs on the host | Spark + your Go binary |
Note what is not in that list: the metastore never touches a single byte of your table. Spark writes Parquet and metadata.json straight to the local filesystem exactly as it would with any catalog; HMS stores one string per table — the path of the current metadata.json — and its entire job is to swap that string atomically. Everything the parts from Lab: create the table to Lab: compaction and cleanup teaches about the file tree is literally true on disk.
What the Hive Metastore actually is
Three tiers, and it is worth being precise about which one contributes what, because the temptation is to think of "the metastore" as one blob:
Read the two arrow labels as the same kind of thing. thrift is how Spark talks to HMS; JDBC is how HMS talks to Postgres. Neither is a box in the diagram — both are just the wire between two boxes, in exactly the sense that "HTTP" is not a component of a website. Count the processes and there are three: Spark, HMS, Postgres. The Thrift, and why it is not a fourth component section is entirely about making sure the first arrow never gets promoted into a fourth.
| Holds | Provides | |
|---|---|---|
| Postgres | The actual rows — one per namespace, table, and table property | Transactions, isolation, durability |
| HMS | Nothing. Restart it and you lose no state | The network API, and the compare-and-swap logic wrapped around alter_table |
Of the roughly two hundred methods HMS exposes, Iceberg leans on a handful, and the one that matters is a single table property. When you CREATE TABLE hms.db.orders, HMS writes a row into TBLS and a row into TABLE_PARAMS holding:
TABLE_PARAMS — the entire Iceberg-relevant payload
PARAM_KEY = metadata_location
PARAM_VALUE = /Users/you/iceberg-go-lab/warehouse/db.db/orders/metadata/00003-abc….metadata.json
PARAM_KEY = previous_metadata_location
PARAM_VALUE = …/00002-def….metadata.jsonThat first string is the Why the catalog gets a tier of its own section's mutable pointer. A commit is therefore three moves: read the current metadata_location (a get_table call), write a brand-new metadata.json to the filesystem, then ask HMS to swap the string only if it still equals what you read (an alter_table call). That conditional swap is the whole reason this tier exists, and it is implemented inside HMS — either behind an explicit HIVE_LOCKS row or as a real compare-and-swap, which the Trading the lock for a real compare-and-swap section pulls apart in detail.
Thrift, and why it is not a fourth component
The fastest way to defuse the word is to put it next to two you already read without flinching. Every hop in this lab names its protocol in a URI, and they are all the same species:
| You write | Which means | Listening there is |
|---|---|---|
http://localhost:8080 | speak HTTP to this address | a web server |
jdbc:postgresql://localhost:5432/… | speak the Postgres wire protocol to this address | Postgres |
thrift://localhost:9083 | speak Thrift to this address | HMS |
Nobody reads the second row as "there is a JDBC process next to Postgres," and the third row deserves the same reading. There is no Thrift daemon in the compose file, no port belongs to Thrift, and nothing needs installing: HMS itself is the Thrift server, and Spark holds the client half as a handful of generated classes already sitting in its jars. That is the entire footprint.
Mechanically it is an RPC, and that is the whole idea. Iceberg's HiveTableOperations writes metastore.get_table("db", "orders") and it reads like an ordinary local call — but metastore is a different process, so underneath, a generated client stub packs the method name and arguments into bytes, sends them to 9083, and unpacks the reply. Both halves of that packing code were generated at build time from one interface file in Hive's source tree, hive_metastore.thrift, which simply lists the methods and their argument types. gRPC does the same job for the Go-to-Spark hop in the Lab: why Go needs Spark Connect part; Thrift is just the older one Hive picked in 2008.
Exactly one detail of that exchange is worth carrying forward, because the lab breaks on it: the method name travels as a plain string, and the server dispatches on it with what amounts to a dictionary lookup.
That dictionary was built by the generator from the server's own copy of the .thrift file. Spark 4.1 bundles a stub generated from Hive 2.3.10's copy, which had get_table, so it sends that string. A Hive 4.0.1 server was generated from a file with get_table deleted, so its handler map has no such key. The lookup misses and the server replies Invalid method name: 'get_table'. There is no version negotiation anywhere in the exchange — nobody asks "what version are you?" — which is precisely why no Spark config can paper over it, and why the only fix is pinning the server version. It is also why the Bring up Postgres and the Hive Metastore section leads with a trap box about an image tag before showing you a single line of the compose file.
Where each process runs, and one INSERT end to end
the whole stack, by address space
YOUR LAPTOP (host) DOCKER
──────────────────────────────────────── ──────────────────────────────
Go binary Hive Metastore
spark-connect-go, no JVM, no jars apache/hive:4.0.0
│ │
│ gRPC :15002 │ JDBC
│ protobuf — SQL text / plans ▼
▼ Postgres 16
Spark Connect Server (JVM) metastore_db
Catalyst · Iceberg jars ▲
thrift client stub ───── thrift :9083 ─────────────┘
│
│ plain file I/O — every Parquet byte, every metadata.json
▼
./warehouse/db.db/orders/{data,metadata}/
▲
└── also bind-mounted into the metastore container at the SAME pathNow trace a single INSERT INTO hms.db.orders VALUES (…) all the way down. Nothing here is hypothetical — every hop is observable, and the pointer, as a database row section has you query the Postgres row directly:
- Go → Spark. Your code calls
spark.Sql(ctx, "INSERT INTO …").spark-connect-gopacks the statement into a protobuf and sends it over gRPC tolocalhost:15002. Go does nothing else for the rest of this list — it has no idea Iceberg, Parquet, HMS or Postgres exist. - Spark plans it. The JVM parses the SQL, Catalyst resolves
orders, and sees the table belongs to an Iceberg catalog oftype=hive. - Spark → HMS: read the pointer.
HiveTableOperations.doRefresh()callsclient.get_table("db", "orders")through the generated stub — bytes onto port 9083. This is the exact call that dies withInvalid method nameon a mispinned image. - HMS → Postgres. One
SELECT, and back comes the table's properties includingmetadata_location = …/00003-abc.metadata.json. Spark now knows whichmetadata.jsonis current. - Spark writes files — no catalog involved. It reads that
metadata.jsonoff the local disk, then writes new.parquetdata files, a new manifest, a new manifest list and a new00004-def.metadata.jsonstraight to the host filesystem. HMS and Postgres see none of it. Not one byte of your table ever transits the metastore. - Spark → HMS: swap the pointer, conditionally. An
alter_tablesaying setmetadata_locationto00004-def…only if it still equals00003-abc…. HMS executes that as a guarded read-check-update against Postgres. If another writer committed in between, the condition fails, HMS refuses, and Iceberg retries from step 3 — which is the Lab: two writers, one table part in one sentence. - The reply unwinds. HMS → Spark → gRPC → your Go binary gets a success. The commit is now visible to anything pointed at
thrift://localhost:9083, whether that is a second Go process,spark-sql, or a Trino cluster that has never heard of this lab.
Bring up Postgres and the Hive Metastore
Two prep steps before the compose file, and they map to the two volumes: lines on the metastore service. One downloads a Postgres JDBC driver, because the Hive image does not ship one — the next callout explains why that is on you. The other creates the warehouse directory, which has to exist at the same absolute path on the host and inside the container, for the reason the Where each process runs, and one INSERT end to end section just laid out:
bash — pick a working directory for the whole lab
$ mkdir -p iceberg-go-lab && cd iceberg-go-lab
$ export WAREHOUSE=$PWD/warehouse
$ mkdir -p "$WAREHOUSE"
$ curl -fLO https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.3/postgresql-42.7.3.jar
$ echo "WAREHOUSE=$WAREHOUSE" > .envdocker-compose.yml
services:
postgres:
image: postgres:16
environment:
POSTGRES_USER: hive
POSTGRES_PASSWORD: hive
POSTGRES_DB: metastore_db
ports: ["5432:5432"]
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U hive -d metastore_db"]
interval: 5s
retries: 20
metastore:
image: apache/hive:4.0.0
depends_on:
postgres:
condition: service_healthy
environment:
SERVICE_NAME: metastore
DB_DRIVER: postgres
SERVICE_OPTS: >-
-Djavax.jdo.option.ConnectionDriverName=org.postgresql.Driver
-Djavax.jdo.option.ConnectionURL=jdbc:postgresql://postgres:5432/metastore_db
-Djavax.jdo.option.ConnectionUserName=hive
-Djavax.jdo.option.ConnectionPassword=hive
-Dhive.metastore.warehouse.dir=${WAREHOUSE}
ports: ["9083:9083"]
volumes:
- ./postgresql-42.7.3.jar:/opt/hive/lib/postgres.jar
- ${WAREHOUSE}:${WAREHOUSE}
volumes:
pgdata:bash
$ docker compose up -d
$ docker compose logs -f metastore
# wait for: "Initialized schema successfully.." then "Starting Hive Metastore Server"The image's entrypoint runs schematool -dbType postgres -initOrUpgradeSchema on every boot for Hive 4.x, and that command is idempotent — so docker compose restart against the persisted pgdata volume is safe. (The 3.x images run a plain -initSchema, which fails the second time; that is what their IS_RESUME=true flag is for.)
Download Spark and start a Connect server pointed at the metastore
Since Spark 4.0 the Connect server ships inside the distribution, so --packages no longer has to fetch it — the only coordinate left to resolve at startup is the Iceberg Spark runtime. That one flag pulls the Iceberg jar; the --conf flags after it wire the Iceberg SQL extensions and a catalog named hms that speaks thrift to the container you just started:
bash — one-time setup, then start the server
$ curl -fLO https://dlcdn.apache.org/spark/spark-4.1.3/spark-4.1.3-bin-hadoop3.tgz
$ tar xzf spark-4.1.3-bin-hadoop3.tgz
$ cd spark-4.1.3-bin-hadoop3
$ export WAREHOUSE=$(cd .. && pwd)/warehouse # same value as .env, in this shell
$ ./sbin/start-connect-server.sh \
--packages org.apache.iceberg:iceberg-spark-runtime-4.1_2.13:1.11.0 \
--conf spark.sql.extensions=org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions \
--conf spark.sql.catalog.hms=org.apache.iceberg.spark.SparkCatalog \
--conf spark.sql.catalog.hms.type=hive \
--conf spark.sql.catalog.hms.uri=thrift://localhost:9083 \
--conf spark.sql.catalog.hms.warehouse=file://$WAREHOUSE \
--conf spark.sql.catalog.hms.cache-enabled=false
# tail the log until you see:
\# "Spark Connect server started at: sc://0.0.0.0:15002"Four of those flags are the whole catalog definition. type=hive selects org.apache.iceberg.hive.HiveCatalog; uri and warehouse are the same two CatalogProperties every Iceberg catalog accepts, one naming the metastore and one naming where new tables get their default location. hms itself is just a name you chose — nothing in Iceberg, Spark, or Go attaches meaning to it, a point the Lab: beyond localhost part leans on hard.
The fourth, cache-enabled=false, is not part of the catalog definition at all and is here deliberately. SparkCatalog caches loaded table objects per session for 30 seconds by default, which means a session that has already read a table keeps serving a stale snapshot even after another session commits. It is the single most confusing thing you can hit in the Lab: two writers, one table part, and it has nothing to do with which catalog sits behind it — turning it off now costs a little read latency and saves you an hour of debugging a bug that isn't there.
Set up the Go side
bash
$ cd .. # back to iceberg-go-lab/
$ go mod init iceberg-go-lab
$ go get github.com/apache/spark-connect-go/v40Every snippet in this lab lives in the same main.go, growing section by section. Start with the smallest possible thing that could work — connect, run one query, print it:
main.go — connectivity check
package main
import (
"context"
"fmt"
"log"
"github.com/apache/spark-connect-go/v40/spark/sql"
)
func main() {
ctx := context.Background()
spark, err := sql.NewSessionBuilder().Remote("sc://localhost:15002").Build(ctx)
if err != nil {
log.Fatalf("connect: %v", err)
}
defer spark.Stop()
df, err := spark.Sql(ctx, "SELECT 'hello from Go' AS msg, current_timestamp() AS ts")
if err != nil {
log.Fatalf("query: %v", err)
}
if err := df.Show(ctx, 10, false); err != nil {
log.Fatalf("show: %v", err)
}
}bash
$ go run main.go
+-----------------+-----------------------+
|msg |ts |
+-----------------+-----------------------+
|hello from Go |2026-08-16 09:41:02.113|
+-----------------+-----------------------+That query never reached the metastore — it touched no table. To prove the thrift hop works end to end, ask for something that has to go through the catalog:
run(ctx, spark, "SHOW NAMESPACES IN hms")An empty result is success: Spark reached HMS, HMS reached Postgres, and there are genuinely no databases yet. A hang followed by a connection error means the container isn't up; Invalid method name: 'get_table' means you didn't pin 4.0.0.
From here on, every code block adds to this file — factor the repetitive error handling into one helper you'll call dozens of times:
main.go — the helper every later snippet uses
func run(ctx context.Context, spark sql.SparkSession, stmt string) {
df, err := spark.Sql(ctx, stmt)
if err != nil {
log.Fatalf("sql failed: %v\nstatement:\n%s", err, stmt)
}
if err := df.Show(ctx, 20, false); err != nil {
log.Fatalf("show failed: %v", err)
}
}