-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1704 from byzer-org/william-dev
Add hasNextPage to Rest Datasource Paging Strategy
- Loading branch information
Showing
9 changed files
with
392 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 51 additions & 2 deletions
53
streamingpro-mlsql/src/main/java/tech/mlsql/datasource/helper/rest/PageStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,61 @@ | ||
package tech.mlsql.datasource.helper.rest | ||
|
||
import com.jayway.jsonpath.JsonPath | ||
import org.apache.spark.sql.mlsql.session.MLSQLException | ||
|
||
/** | ||
* 3/12/2021 WilliamZhu(allwefantasy@gmail.com) | ||
*/ | ||
trait PageStrategy { | ||
def pageValues(_content: Option[Any]): Array[String] | ||
|
||
def nexPage: PageStrategy | ||
def nexPage(_content: Option[Any]): PageStrategy | ||
|
||
def hasNextPage(_content: Option[Any]): Boolean | ||
|
||
def pageUrl(_content: Option[Any]): String | ||
} | ||
|
||
object PageStrategy { | ||
val PAGE_CONFIG_VALUES = "config.page.values" | ||
|
||
def defaultHasNextPage(params: Map[String, String], _content: Option[Any]): Boolean = { | ||
val stopPagingCondition = params.get("config.page.stop") | ||
|
||
if (stopPagingCondition.isEmpty) { | ||
//todo: Choose a better default value | ||
return true | ||
} | ||
val content = _content.get.toString | ||
val Array(func, jsonPath) = stopPagingCondition.get.split(":") | ||
|
||
func match { | ||
case "size-zero" | "sizeZero" => | ||
try { | ||
val targetValue = JsonPath.read[net.minidev.json.JSONArray](content, jsonPath) | ||
targetValue.size() > 0 | ||
} catch { | ||
case _: com.jayway.jsonpath.PathNotFoundException => | ||
false | ||
} | ||
|
||
case "not-exists" | "notExists" => try { | ||
JsonPath.read[Object](content, jsonPath) | ||
true | ||
} catch { | ||
case _: com.jayway.jsonpath.PathNotFoundException => | ||
false | ||
} | ||
case "equals" => | ||
try { | ||
val Array(realJsonPath, equalValue) = jsonPath.split(",") | ||
val targetValue = JsonPath.read[Object](content, realJsonPath).toString | ||
targetValue != equalValue | ||
} catch { | ||
case _: com.jayway.jsonpath.PathNotFoundException => | ||
true | ||
} | ||
|
||
case _ => throw new MLSQLException(s"config.page.stop with ${func} is not supported") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.