You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "Yohahaha (via GitHub)" <gi...@apache.org> on 2026/02/02 14:43:47 UTC
[PR] [WIP][spark] Add streaming read for sparksql [fluss]
Yohahaha opened a new pull request, #2548:
URL: https://github.com/apache/fluss/pull/2548
<!--
*Thank you very much for contributing to Fluss - we are happy that you want to help us improve Fluss. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [GitHub issue](https://github.com/apache/fluss/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no issue.
- Name the pull request in the format "[component] Title of the pull request", where *[component]* should be replaced by the name of the component being changed. Typically, this corresponds to the component label assigned to the issue (e.g., [kv], [log], [client], [flink]). Skip *[component]* if you are unsure about which is the best component.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes.
- Each pull request should address only one issue, not mix up code from multiple issues.
**(The sections below can be removed for hotfixes or typos)**
-->
### Purpose
<!-- Linking this pull request to the issue -->
Linked issue: close #xxx
<!-- What is the purpose of the change -->
### Brief change log
<!-- Please describe the changes made in this pull request and explain how they address the issue -->
### Tests
<!-- List UT and IT cases to verify this change -->
### API and Format
<!-- Does this change affect API or storage format -->
### Documentation
<!-- Does this change introduce a new feature -->
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add basic streaming read support for sparksql with latest mode [fluss]
Posted by "Yohahaha (via GitHub)" <gi...@apache.org>.
Yohahaha commented on code in PR #2548:
URL: https://github.com/apache/fluss/pull/2548#discussion_r2776901940
##########
fluss-spark/fluss-spark-common/src/main/scala/org/apache/fluss/spark/read/FlussMicroBatchStream.scala:
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.
+ */
+
+package org.apache.fluss.spark.read
+
+import org.apache.fluss.client.{Connection, ConnectionFactory}
+import org.apache.fluss.client.admin.Admin
+import org.apache.fluss.client.initializer.{BucketOffsetsRetrieverImpl, OffsetsInitializer}
+import org.apache.fluss.config.Configuration
+import org.apache.fluss.metadata.{PartitionInfo, TableBucket, TableInfo, TablePath}
+import org.apache.fluss.utils.json.TableBucketOffsets
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory}
+import org.apache.spark.sql.connector.read.streaming._
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+import java.util
+import java.util.Optional
+
+import scala.collection.JavaConverters._
+
+case class FlussSourceOffset(tableBucketOffsets: TableBucketOffsets) extends Offset {
+ override val json: String = new String(tableBucketOffsets.toJsonBytes, "utf-8")
+}
+
+abstract class FlussMicroBatchStream(
+ val tablePath: TablePath,
+ tableInfo: TableInfo,
+ readSchema: StructType,
+ options: CaseInsensitiveStringMap,
+ flussConfig: Configuration,
+ checkpointLocation: String)
+ extends SupportsTriggerAvailableNow
+ with ReportsSourceMetrics
+ with MicroBatchStream
+ with Logging
+ with AutoCloseable {
+
+ lazy val conn: Connection = ConnectionFactory.createConnection(flussConfig)
+
+ lazy val admin: Admin = conn.getAdmin
+
+ lazy val bucketOffsetsRetriever: BucketOffsetsRetrieverImpl =
+ new BucketOffsetsRetrieverImpl(admin, tableInfo.getTablePath)
+
+ lazy val partitionInfos: util.List[PartitionInfo] = admin.listPartitionInfos(tablePath).get()
+
+ private var allDataForTriggerAvailableNow: Option[TableBucketOffsets] = None
+
+ val startOffsetsInitializer: OffsetsInitializer =
+ FlussOffsetInitializers.startOffsetsInitializer(options, flussConfig)
+
+ val stoppingOffsetsInitializer: OffsetsInitializer =
+ FlussOffsetInitializers.stoppingOffsetsInitializer(false, options, flussConfig)
+
+ protected def projection: Array[Int] = {
+ val columnNameToIndex = tableInfo.getSchema.getColumnNames.asScala.zipWithIndex.toMap
+ readSchema.fields.map {
+ field =>
+ columnNameToIndex.getOrElse(
+ field.name,
+ throw new IllegalArgumentException(s"Invalid field name: ${field.name}"))
+ }
+ }
+
+ override def close(): Unit = {
+ if (admin != null) {
+ admin.close()
+ }
+ if (conn != null) {
+ conn.close()
+ }
+ }
+
+ override def latestOffset(): Offset = {
+ throw new UnsupportedOperationException(
+ "latestOffset(Offset, ReadLimit) should be called instead of this method")
+ }
+
+ override def getDefaultReadLimit: ReadLimit = {
+ ReadLimit.allAvailable()
+ }
+
+ override def initialOffset(): Offset = {
+ val initialTableBucketOffsets = getOrCreateInitialPartitionOffsets()
+ FlussSourceOffset(initialTableBucketOffsets)
+ }
+
+ override def latestOffset(start: Offset, readLimit: ReadLimit): Offset = {
+ if (!readLimit.isInstanceOf[ReadAllAvailable]) {
+ throw new UnsupportedOperationException(s"Only ReadAllAvailable is supported, but $readLimit")
+ }
+
+ val latestTableBucketOffsets = if (allDataForTriggerAvailableNow.isDefined) {
+ allDataForTriggerAvailableNow.get
+ } else {
+ fetchLatestOffsets().get
+ }
+ FlussSourceOffset(latestTableBucketOffsets)
+ }
+
+ override def prepareForTriggerAvailableNow(): Unit = {
+ allDataForTriggerAvailableNow = fetchLatestOffsets()
+ }
+
+ private def fetchLatestOffsets(): Option[TableBucketOffsets] = {
+ val buckets = (0 until tableInfo.getNumBuckets).toSeq
+ val offsetsInitializer = OffsetsInitializer.latest()
+ if (tableInfo.isPartitioned) {
+ val partitionOffsets = partitionInfos.asScala.map(
+ partitionInfo =>
+ FlussMicroBatchStream.getLatestOffsets(
+ tableInfo,
+ offsetsInitializer,
+ bucketOffsetsRetriever,
+ buckets,
+ Some(partitionInfo)))
+ val mergedOffsets = partitionOffsets
+ .map(_.getOffsets)
+ .reduce((l, r) => (l.asScala ++ r.asScala).asJava)
+ Some(new TableBucketOffsets(tableInfo.getTableId, mergedOffsets))
+ } else {
+ Some(
+ FlussMicroBatchStream
+ .getLatestOffsets(tableInfo, offsetsInitializer, bucketOffsetsRetriever, buckets, None))
+ }
+ }
+
+ // No need to notify fluss server
+ override def commit(end: Offset): Unit = {}
+
+ override def stop(): Unit = close()
+
+ override def deserializeOffset(json: String): Offset = {
+ FlussSourceOffset(TableBucketOffsets.fromJsonBytes(json.getBytes("utf-8")))
+ }
+
+ override def metrics(latestConsumedOffset: Optional[Offset]): util.Map[String, String] = {
+ // TODO add metrics
+ Map.empty[String, String].asJava
+ }
+
+ private def getOrCreateInitialPartitionOffsets(): TableBucketOffsets = {
+ // TODO load from checkpoint dir
Review Comment:
done, thank you!
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add basic streaming read support for sparksql with latest mode [fluss]
Posted by "wuchong (via GitHub)" <gi...@apache.org>.
wuchong commented on PR #2548:
URL: https://github.com/apache/fluss/pull/2548#issuecomment-3851000745
@Yohahaha https://github.com/apache/fluss/pull/2532 is merged, could you rebase and resolve the failed CI?
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add basic streaming read support for sparksql with latest mode [fluss]
Posted by "wuchong (via GitHub)" <gi...@apache.org>.
wuchong merged PR #2548:
URL: https://github.com/apache/fluss/pull/2548
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add basic streaming read support for sparksql with latest mode [fluss]
Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on code in PR #2548:
URL: https://github.com/apache/fluss/pull/2548#discussion_r2772741807
##########
fluss-spark/fluss-spark-common/src/main/scala/org/apache/fluss/spark/read/FlussMicroBatchStream.scala:
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.
+ */
+
+package org.apache.fluss.spark.read
+
+import org.apache.fluss.client.{Connection, ConnectionFactory}
+import org.apache.fluss.client.admin.Admin
+import org.apache.fluss.client.initializer.{BucketOffsetsRetrieverImpl, OffsetsInitializer}
+import org.apache.fluss.config.Configuration
+import org.apache.fluss.metadata.{PartitionInfo, TableBucket, TableInfo, TablePath}
+import org.apache.fluss.utils.json.TableBucketOffsets
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory}
+import org.apache.spark.sql.connector.read.streaming._
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+import java.util
+import java.util.Optional
+
+import scala.collection.JavaConverters._
+
+case class FlussSourceOffset(tableBucketOffsets: TableBucketOffsets) extends Offset {
+ override val json: String = new String(tableBucketOffsets.toJsonBytes, "utf-8")
+}
+
+abstract class FlussMicroBatchStream(
+ val tablePath: TablePath,
+ tableInfo: TableInfo,
+ readSchema: StructType,
+ options: CaseInsensitiveStringMap,
+ flussConfig: Configuration,
+ checkpointLocation: String)
+ extends SupportsTriggerAvailableNow
+ with ReportsSourceMetrics
+ with MicroBatchStream
+ with Logging
+ with AutoCloseable {
+
+ lazy val conn: Connection = ConnectionFactory.createConnection(flussConfig)
+
+ lazy val admin: Admin = conn.getAdmin
+
+ lazy val bucketOffsetsRetriever: BucketOffsetsRetrieverImpl =
+ new BucketOffsetsRetrieverImpl(admin, tableInfo.getTablePath)
+
+ lazy val partitionInfos: util.List[PartitionInfo] = admin.listPartitionInfos(tablePath).get()
+
+ private var allDataForTriggerAvailableNow: Option[TableBucketOffsets] = None
+
+ val startOffsetsInitializer: OffsetsInitializer =
+ FlussOffsetInitializers.startOffsetsInitializer(options, flussConfig)
+
+ val stoppingOffsetsInitializer: OffsetsInitializer =
+ FlussOffsetInitializers.stoppingOffsetsInitializer(false, options, flussConfig)
+
+ protected def projection: Array[Int] = {
+ val columnNameToIndex = tableInfo.getSchema.getColumnNames.asScala.zipWithIndex.toMap
+ readSchema.fields.map {
+ field =>
+ columnNameToIndex.getOrElse(
+ field.name,
+ throw new IllegalArgumentException(s"Invalid field name: ${field.name}"))
+ }
+ }
+
+ override def close(): Unit = {
+ if (admin != null) {
+ admin.close()
+ }
+ if (conn != null) {
+ conn.close()
+ }
+ }
+
+ override def latestOffset(): Offset = {
+ throw new UnsupportedOperationException(
+ "latestOffset(Offset, ReadLimit) should be called instead of this method")
+ }
+
+ override def getDefaultReadLimit: ReadLimit = {
+ ReadLimit.allAvailable()
+ }
+
+ override def initialOffset(): Offset = {
+ val initialTableBucketOffsets = getOrCreateInitialPartitionOffsets()
+ FlussSourceOffset(initialTableBucketOffsets)
+ }
+
+ override def latestOffset(start: Offset, readLimit: ReadLimit): Offset = {
+ if (!readLimit.isInstanceOf[ReadAllAvailable]) {
+ throw new UnsupportedOperationException(s"Only ReadAllAvailable is supported, but $readLimit")
+ }
+
+ val latestTableBucketOffsets = if (allDataForTriggerAvailableNow.isDefined) {
+ allDataForTriggerAvailableNow.get
+ } else {
+ fetchLatestOffsets().get
+ }
+ FlussSourceOffset(latestTableBucketOffsets)
+ }
+
+ override def prepareForTriggerAvailableNow(): Unit = {
+ allDataForTriggerAvailableNow = fetchLatestOffsets()
+ }
+
+ private def fetchLatestOffsets(): Option[TableBucketOffsets] = {
+ val buckets = (0 until tableInfo.getNumBuckets).toSeq
+ val offsetsInitializer = OffsetsInitializer.latest()
+ if (tableInfo.isPartitioned) {
+ val partitionOffsets = partitionInfos.asScala.map(
+ partitionInfo =>
+ FlussMicroBatchStream.getLatestOffsets(
+ tableInfo,
+ offsetsInitializer,
+ bucketOffsetsRetriever,
+ buckets,
+ Some(partitionInfo)))
+ val mergedOffsets = partitionOffsets
+ .map(_.getOffsets)
+ .reduce((l, r) => (l.asScala ++ r.asScala).asJava)
+ Some(new TableBucketOffsets(tableInfo.getTableId, mergedOffsets))
+ } else {
+ Some(
+ FlussMicroBatchStream
+ .getLatestOffsets(tableInfo, offsetsInitializer, bucketOffsetsRetriever, buckets, None))
+ }
+ }
+
+ // No need to notify fluss server
+ override def commit(end: Offset): Unit = {}
+
+ override def stop(): Unit = close()
+
+ override def deserializeOffset(json: String): Offset = {
+ FlussSourceOffset(TableBucketOffsets.fromJsonBytes(json.getBytes("utf-8")))
+ }
+
+ override def metrics(latestConsumedOffset: Optional[Offset]): util.Map[String, String] = {
+ // TODO add metrics
+ Map.empty[String, String].asJava
+ }
+
+ private def getOrCreateInitialPartitionOffsets(): TableBucketOffsets = {
+ // TODO load from checkpoint dir
Review Comment:
Fluss doesn't have to load offset from checkpoint path, instead spark does.
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add streaming read for sparksql [fluss]
Posted by "Yohahaha (via GitHub)" <gi...@apache.org>.
Yohahaha commented on PR #2548:
URL: https://github.com/apache/fluss/pull/2548#issuecomment-3839538216
based on https://github.com/apache/fluss/pull/2532 for startup mode.
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Add basic streaming read support for sparksql with latest mode [fluss]
Posted by "YannByron (via GitHub)" <gi...@apache.org>.
YannByron commented on PR #2548:
URL: https://github.com/apache/fluss/pull/2548#issuecomment-3858855216
+1. Let adjust the streaming test cases when we have more discussion offline in the following pr.
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org