Reading and writing within a transaction
For more information on transactions, please see the Transactions section.
By default, Spark does not create transactions for reading or take out any locks. To assure reading consistency for changing tables, you can create your own transactions and redirect them to read a Spark DataFrame.
Python example:
with yt.Transaction() as tr:
df = spark.read.option("transaction", tr.transaction_id).yt("//sys/spark/examples/example_1")
df.show()
At the time of the call to spark.read.yt, a lock is taken out on the table (snapshot lock): while a plan is being compiled and until the physical read begins. In the example provided, the two calls to show() will read the table twice, necessarily returning the same result both times.
with yt.Transaction() as tr:
df = spark.read.option("transaction", tr.transaction_id).yt("//sys/spark/examples/example_1")
df.show()
time.sleep(60)
df.show()
Note
You can enable creation of a global transaction that will open at the top of a Spark session and remain open the entire time your job is running. When using a global transaction, you have to keep in mind that the job will take out locks on all the tables it reads and maintain them until it exits. You enable a global transaction by setting configuration parameter spark.yt.globalTransaction.enabled=true.
When you need to write a DataFrame within a transaction you should use write_transaction option:
df.write.option('write_transaction', transaction_id).yt("//target/table/path")
Running operations in shared or separate transactions
Use reader transaction and writer write_transaction options for individual operations,
or when operations need different transactions:
df = spark.read.option("transaction", read_tx).yt("//input")
df.write.option("write_transaction", write_tx).yt("//output")
Use the session configuration when several reads or writes should share a transaction,
such as a pipeline publishing several tables atomically. This avoids passing the transaction
ID to every reader and writer. The session settings also work in an isolated Spark Connect session:
spark.conf.set("spark.datasource.yt.transaction", transaction_id)
spark.conf.set("spark.datasource.yt.write_transaction", transaction_id)
The spark.datasource.yt.transaction session setting specifies the default transaction for
reads, and spark.datasource.yt.write_transaction specifies the default for writes. These
settings apply independently. The transaction and write_transaction options explicitly
set for an individual operation override the corresponding session settings. An empty
option value disables the external transaction for that operation.
Whether the external transaction is specified through per-operation options or session
settings, the caller manages transaction commit and abort. Writes use child transactions
and become visible outside the parent transaction when it commits.
To read and then write the same table, take an exclusive lock in the parent transaction
before creating the input DataFrame; otherwise the read's snapshot lock blocks the write.
The following Python example shows how to acquire this lock:
import yt.wrapper as yt
table_path = "//path/to/table"
# transaction_id is the ID of an existing parent transaction.
with yt.Transaction(transaction_id=transaction_id):
yt.lock(table_path, mode="exclusive")
df = spark.read.option("transaction", transaction_id).yt(table_path)
Working with dynamic tables
Note
SPYT does not support writing to dynamic tables under an external transaction: if
write_transaction is set through a writer option or session setting, the write fails
with an error. You cannot include it in the atomic publication of multiple tables under
a shared external transaction.
To write to a dynamic table independently, set the inconsistent_dynamic_write option
to true. If the external transaction is set through the
spark.datasource.yt.write_transaction session setting, disable it for this write with
.option("write_transaction", ""). This write runs independently of the parent transaction:
aborting the parent transaction does not roll back the changes written to the dynamic table.
The following example appends the data from df to an existing mounted dynamic table
without using the external transaction configured for the session:
(
df.write
.mode("append")
.option("inconsistent_dynamic_write", "true")
.option("write_transaction", "")
.yt("//path/to/dynamic_table")
)