You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by xi...@apache.org on 2020/12/11 17:18:00 UTC

[incubator-pinot] 01/01: Adding json path functions to extract values from json object

This is an automated email from the ASF dual-hosted git repository.

xiangfu pushed a commit to branch adding_json_extract_function_in_scalar
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git

commit c9ead60ebf10a3108ae4719ee4d1f8206dc244f6
Author: Xiang Fu <fx...@gmail.com>
AuthorDate: Fri Dec 11 09:16:02 2020 -0800

    Adding json path functions to extract values from json object
---
 pinot-common/pom.xml                               |  4 +++
 .../common/function/scalar/JsonFunctions.java      | 36 ++++++++++++++++++++++
 .../apache/pinot/core/util/TableConfigUtils.java   |  3 +-
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/pinot-common/pom.xml b/pinot-common/pom.xml
index 1d687d5..b23ac14 100644
--- a/pinot-common/pom.xml
+++ b/pinot-common/pom.xml
@@ -222,6 +222,10 @@
       <artifactId>jackson-databind</artifactId>
     </dependency>
     <dependency>
+      <groupId>com.jayway.jsonpath</groupId>
+      <artifactId>json-path</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
       <scope>test</scope>
diff --git a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
index c67e9b9..e0a7ff2 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
@@ -19,6 +19,7 @@
 package org.apache.pinot.common.function.scalar;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.jayway.jsonpath.JsonPath;
 import java.util.Map;
 import org.apache.pinot.spi.annotations.ScalarFunction;
 import org.apache.pinot.spi.utils.JsonUtils;
@@ -56,4 +57,39 @@ public class JsonFunctions {
       throws JsonProcessingException {
     return JsonUtils.objectToString(object);
   }
+
+  /**
+   * Extract object based on Json path
+   */
+  @ScalarFunction
+  public static Object jsonPath(Object object, String jsonPath) {
+    return JsonPath.read(object, jsonPath);
+  }
+
+  /**
+   * Extract from Json with path to String
+   */
+  @ScalarFunction
+  public static String jsonPathString(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return JsonUtils.objectToString(jsonPath(object, jsonPath));
+  }
+
+  /**
+   * Extract from Json with path to Long
+   */
+  @ScalarFunction
+  public static Long jsonPathLong(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return Long.parseLong(jsonPathString(object, jsonPath));
+  }
+
+  /**
+   * Extract from Json with path to Double
+   */
+  @ScalarFunction
+  public static Double jsonPathDouble(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return Double.parseDouble(jsonPathString(object, jsonPath));
+  }
 }
diff --git a/pinot-core/src/main/java/org/apache/pinot/core/util/TableConfigUtils.java b/pinot-core/src/main/java/org/apache/pinot/core/util/TableConfigUtils.java
index 1074cde..d400769 100644
--- a/pinot-core/src/main/java/org/apache/pinot/core/util/TableConfigUtils.java
+++ b/pinot-core/src/main/java/org/apache/pinot/core/util/TableConfigUtils.java
@@ -238,7 +238,8 @@ public final class TableConfigUtils {
           String columnName = transformConfig.getColumnName();
           if (schema != null) {
             Preconditions.checkState(schema.getFieldSpecFor(columnName) != null,
-                "The destination column of the transform function must be present in the schema");
+                "The destination column '" + columnName
+                    + "'of the transform function must be present in the schema");
           }
           String transformFunction = transformConfig.getTransformFunction();
           if (columnName == null || transformFunction == null) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org