All posts

Apache XTable on Spark: the CLI, the API and what they report

XTableSparkSync exits 1 on failure and XTableSyncService returns a map you must check. Options, classpath traps, and the Delta Kernel limits that send some tables back to RunSync.

8 min read Lakehouse

TL;DR

The Spark runtime is the same sync as RunSync packaged as a small jar, with an entry point you can submit and an API you can call from your own job.

  • XTableSparkSync takes one table or a YAML of many, and it exits 1 when any target fails. That is the opposite of RunSync, and the reason to prefer it under a scheduler.
  • XTableSyncService.sync(spec, hadoopConf) returns a map of SyncResult per format. A failed target is a status in that map, not an exception, and a run with nothing new returns an empty map.
  • On newer Spark versions it turns on Delta Kernel by itself. Kernel cannot write the generated columns that time and bucket partitions need, and it cannot evolve an existing Delta table’s schema, so those cases sync to Delta only with RunSync.
  • A Hudi target needs hudi-java-client, or xtable.hudi.target.execution.engine: spark, because the Hudi Spark bundle does not carry the Java client.

Two ways exist to run XTable from Spark, and they are built from the same two classes. Which one fits depends on whether the sync is a job of its own or a step at the end of a write. The behaviour below was read from the 0.5.0-incubating source and checked by running it on Spark 3.5.9.

XTableSparkSync and XTableSyncService both build a TableSyncSpec and call the same controller, but the CLI throws on a failed target while the service returns a map of results

The jar and its dependencies

xtable-spark-runtime_2.12 is on Maven Central, 4,573,834 bytes at this release. Every engine dependency is provided by the cluster, which is why it is small. It also contains the Spark SQL extensions for the secondary index, covered in the Iceberg source post. Only a Scala 2.12 artifact is published.

The runtime jar holds only XTable, and the Hudi, Iceberg and Delta libraries come from the cluster or --packages

What you put on the classpath depends on the formats involved:

Involved Needed on the classpath Failure without it
Hudi source or target The Hudi Spark bundle, 1.2.x for this release NoSuchMethodError with the older 0.4.0 jar and newer Hudi
Hudi target hudi-java-client 1.2.1, or xtable.hudi.target.execution.engine: spark NoClassDefFoundError: org/apache/hudi/client/common/HoodieJavaEngineContext
Iceberg The Iceberg Spark runtime Class errors on first use
Delta on Spark 3.5+ delta-spark, plus delta-kernel-api and delta-kernel-defaults 4.0.0 NoClassDefFoundError: io/delta/kernel/engine/Engine

The Hudi Java client row is easy to hit. The runtime writes a Hudi target with Hudi’s Java client by default, and that class is not in the Spark bundle. Setting the target’s execution engine to spark uses the Spark client the bundle does include.

XTableSparkSync, the CLI

It runs the sync as a Spark application:

$SPARK_HOME/bin/spark-submit \
  --packages org.apache.hudi:hudi-spark3.5-bundle_2.12:1.2.1,org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.9.2,io.delta:delta-spark_2.12:3.3.2,io.delta:delta-kernel-api:4.0.0,io.delta:delta-kernel-defaults:4.0.0 \
  --conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
  --class org.apache.xtable.spark.XTableSparkSync \
  xtable-spark-runtime_2.12-0.5.0-incubating.jar \
  --basepath file:///lab/trips --sourceformat HUDI --targets ICEBERG,DELTA \
  --partitionspec city_id:VALUE
Option Meaning
--basepath, --sourceformat, --targets Single-table mode. All three are required without --datasetconfig
--datasetconfig A YAML of many tables, local or on cloud storage, read through the Spark Hadoop config. It is mutually exclusive with the three above
--datapath Where the data files are, if not under the base path
--tablename Defaults to the last segment of the base path
--namespace Dot-separated
--partitionspec For a Hudi or Parquet source
--usedeltakernel Force Delta Kernel. Without it, Kernel is enabled by itself when Spark is 3.5 or newer and Delta is involved
--help Usage

Source formats are HUDI, ICEBERG, DELTA, PAIMON and PARQUET, and targets are HUDI, ICEBERG and DELTA. Parquet needs no converters config here, because XTableSyncService picks the provider itself.

Failure handling

XTableSparkSync checks every target’s SyncStatusCode after each table, because TableFormatSync reports a target failure as a status rather than throwing. It counts the tables with a failed target and, at the end, throws IllegalStateException("N of M table sync(s) failed; see logs above"). Three runs showed the difference from RunSync:

Scenario Exit code What the log said
Iceberg path that does not exist 1 XTable sync failed for nope (...) and 1 of 1 table sync(s) failed
Partial failure: Delta Kernel and Hudi both reject the table 1 XTable sync failed for cmp_bucket (...) on target(s) {DELTA=ERROR: ..., HUDI=ERROR: Bucket partition is not yet supported by Hudi targets}
--basepath missing 1 IllegalArgumentException: --basepath is required unless --datasetconfig is provided

The error message lists each failed target with its reason, which is more useful in a scheduler’s log than RunSync’s. The same failures through RunSync exited 0, as the RunSync guide shows. One caveat: an Error such as NoClassDefFoundError is not an Exception, so it escapes the per-target handling and ends the application with a stack trace, also with exit code 1.

XTableSyncService, the API

Use it when the sync is the last step of a write job:

import java.util.{Arrays => JArrays}
import scala.collection.JavaConverters._
import org.apache.xtable.spark.{TableSyncSpec, XTableSyncService}
import org.apache.xtable.model.sync.SyncStatusCode

val results = new XTableSyncService().sync(
  TableSyncSpec.builder()
    .key("trips")
    .basePath("file:///lab/trips")
    .sourceFormat("HUDI")
    .partitionSpec("city_id:VALUE")
    .targets(JArrays.asList("ICEBERG", "DELTA"))
    .useDeltaKernel(true)
    .build(),
  spark.sparkContext.hadoopConfiguration)

val failed = results.asScala
  .filter(_._2.getTableFormatSyncStatus.getStatusCode != SyncStatusCode.SUCCESS).keys
if (failed.nonEmpty) throw new IllegalStateException("XTable sync failed for " + failed.mkString(","))

Run twice in spark-shell, the first call returned ICEBERG FULL SUCCESS and DELTA FULL SUCCESS, and the second, with no new commit, returned an empty map. An empty map means nothing needed syncing, which is not a failure. Treat it that way.

TableSyncSpec has nine fields: key (used as the table name), basePath, dataPath, namespace, partitionSpec, sourceFormat, targets, useDeltaKernel and targetProperties. What the service does with them, from the code:

  • dataPath defaults to basePath. A Hudi target is written at the data path, and every other target at the base path.
  • partitionSpec goes to the Parquet source for PARQUET and to the Hudi source for everything else.
  • useDeltaKernel selects the Kernel source provider for a Delta source and sets the Delta target’s USE_KERNEL property.
  • The sync mode is always INCREMENTAL, so a first run falls back to a snapshot by itself.
  • An unknown source format throws Unsupported source format for spark-runtime sync: <format>.

.useDeltaKernel(true) matters when the session has delta-spark 3.3.2 on its classpath. Without it the older Delta writer ran and failed with NoSuchMethodError: DeltaLog.ensureLogDirectoryExist().

Delta Kernel and partitions

Kernel is selected by itself on Spark 3.5 and newer, and the runtime logs that it did. Kernel’s writer cannot create generated columns, and that is how XTable expresses time and bucket partitions in Delta. An Iceberg table partitioned by days(order_ts) failed on the Delta target through the runtime with Unsupported Delta writer feature: ... requires writer table feature "[generatedColumns]", and converted fine through RunSync. Identity partitions work either way.

The Delta Kernel target cannot write generated columns or evolve a schema, while the standard Delta target used by RunSync can

Kernel has a second limit. After a column was added to an Iceberg source, the second sync of an existing Delta target through the runtime failed, with exit code 1:

XTable sync failed for trips_ice (...) on target(s) {DELTA=ERROR: Schema evolution on an existing Delta table
is not supported by DeltaKernelConversionTarget (see https://github.com/delta-io/delta/issues/4305). Set
xtable.delta.target.use_kernel=false on the target's additional properties to use the Delta Standalone target instead.}

The same table synced through RunSync. The message’s advice, switching Kernel off, only helps where the delta-core path runs. The runtime’s own comment says it does not on Spark 3.5 and newer, so for tables whose schema evolves, use RunSync for the Delta target, or run the runtime on an older Spark. The compatibility post has the details.

Patterns that follow

  • A step after the write. Call XTableSyncService.sync after the commit and check the map. Decide whether a failed target fails the ingestion job or only marks the target stale, because the API will not decide for you.
  • A job of its own. Submit XTableSparkSync on a schedule. The exit code is trustworthy, which makes it the simpler choice for an orchestrator, unless a Delta target must evolve its schema or use time or bucket partitions.
  • Many tables. Use --datasetconfig. One table failing does not stop the others, and the final exception reports how many failed.
  • No retry. Neither entry point retries. The flag on a failed result says whether a retry makes sense, and acting on it is the caller’s job.

Scheduling and the Glue job settings are in Running Apache XTable in production. Tuning was not measured: the sync is metadata work, and only the secondary index build reads every data file.

Checked against the source

Claim Where
Options, formats, mutual exclusion, Kernel auto-enable XTableSparkSync
Failure counting and the thrown exception XTableSparkSync.syncAll
TableSyncSpec fields TableSyncSpec
Data path defaults, Hudi target location, partitionSpec routing, source provider switch XTableSyncService
Targets report failure as a status The comment in syncAll and TableFormatSync

The exit codes, the two spark-shell calls and the Kernel failures are from runs on Spark 3.5.9. The Glue job settings in the production post come from the documentation.

Key takeaways

The Spark runtime gives you the CLI that fails properly and an API that leaves failure checking to you. Pick the CLI for a scheduled job, call the service from a write job when you want the sync in the same application, and check the returned map every time. Watch the two classpath traps, the Hudi Java client and Delta Kernel, and remember that Kernel changes what can be written to Delta.

References

Trademarks

Apache XTable (incubating), Apache Hudi, Apache Iceberg, Apache Spark, Apache Parquet and Apache are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. Delta Lake is a trademark of the Linux Foundation. All other marks are the property of their respective owners.

Found this useful?

These posts and tools are free. If one saved you an afternoon, you can buy me a coffee.

Buy me a coffee

Comments

Questions, corrections and reviews are welcome. Sign in with GitHub to post; comments are stored in this site's public GitHub Discussions.