jihoonson commented on a change in pull request #9449:
URL: https://github.com/apache/druid/pull/9449#discussion_r432899895



##########
File path: server/src/main/java/org/apache/druid/metadata/input/SqlReader.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.IntermediateRowParsingReader;
+import org.apache.druid.data.input.impl.MapInputRowParser;
+import org.apache.druid.data.input.impl.prefetch.JsonIterator;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.java.util.common.parsers.CloseableIterator;
+import org.apache.druid.java.util.common.parsers.ParseException;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Reader exclusively for {@link SqlEntity}
+ */
+public class SqlReader extends IntermediateRowParsingReader<Map<String, 
Object>>
+{
+  private final InputRowSchema inputRowSchema;
+  private final SqlEntity source;
+  private final File temporaryDirectory;
+  private final ObjectMapper objectMapper;
+
+
+  SqlReader(
+      InputRowSchema inputRowSchema,
+      InputEntity source,
+      File temporaryDirectory,
+      ObjectMapper objectMapper
+  )
+  {
+    this.inputRowSchema = inputRowSchema;
+    this.source = (SqlEntity) source;
+    this.temporaryDirectory = temporaryDirectory;
+    this.objectMapper = objectMapper;
+  }
+
+  @Override
+  protected CloseableIterator<Map<String, Object>> intermediateRowIterator() 
throws IOException
+  {
+    final Closer closer = Closer.create();
+    //The results are fetched into local storage as this avoids having to keep 
a persistent database connection for a long time
+    final InputEntity.CleanableFile resultFile = 
closer.register(source.fetch(temporaryDirectory, null));
+    FileInputStream inputStream = new FileInputStream(resultFile.file());
+    JsonIterator<Map<String, Object>> jsonIterator = new JsonIterator<>(new 
TypeReference<Map<String, Object>>()
+    {
+    }, inputStream, closer, objectMapper);
+    return new CloseableIterator<Map<String, Object>>()

Review comment:
       Thanks for making the `JsonIterator` a `CloseableIterator`. Now you can 
return `jsonIterator` directly and remove this.

##########
File path: server/src/main/java/org/apache/druid/metadata/input/SqlEntity.java
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+import org.skife.jdbi.v2.ResultIterator;
+import org.skife.jdbi.v2.exceptions.CallbackFailedException;
+import org.skife.jdbi.v2.exceptions.ResultSetException;
+import org.skife.jdbi.v2.exceptions.StatementException;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents a rdbms based input resource and knows how to read query results 
from the resource using SQL queries.
+ */
+public class SqlEntity implements InputEntity
+{
+  private static final Logger LOG = new Logger(SqlEntity.class);
+
+  private final String sql;
+  private final ObjectMapper objectMapper;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final boolean foldCase;
+
+  public SqlEntity(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      boolean foldCase,
+      ObjectMapper objectMapper
+  )
+  {
+    this.sql = sql;
+    this.sqlFirehoseDatabaseConnector = Preconditions.checkNotNull(
+        sqlFirehoseDatabaseConnector,
+        "SQL Metadata Connector not configured!"
+    );
+    this.foldCase = foldCase;
+    this.objectMapper = objectMapper;
+  }
+
+  public String getSql()
+  {
+    return sql;
+  }
+
+  @Nullable
+  @Override
+  public URI getUri()
+  {
+    return null;
+  }
+
+  @Override
+  public InputStream open()
+  {
+    throw new UnsupportedOperationException("Please use fetch() instead");
+  }
+
+  @Override
+  public CleanableFile fetch(File temporaryDirectory, byte[] fetchBuffer) 
throws IOException
+  {
+    final File tempFile = File.createTempFile("druid-sql-entity", ".tmp", 
temporaryDirectory);
+    return openCleanableFile(sql, sqlFirehoseDatabaseConnector, objectMapper, 
foldCase, tempFile);
+
+  }
+
+  public static CleanableFile openCleanableFile(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      ObjectMapper objectMapper,
+      boolean foldCase,
+      File tempFile
+  )
+      throws IOException
+  {
+    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
+      final JsonGenerator jg = objectMapper.getFactory().createGenerator(fos);
+
+      // Execute the sql query and lazily retrieve the results into the file 
in json format.
+      // foldCase is useful to handle differences in case sensitivity behavior 
across databases.
+      sqlFirehoseDatabaseConnector.retryWithHandle(
+          (handle) -> {
+            ResultIterator<Map<String, Object>> resultIterator = 
handle.createQuery(
+                sql
+            ).map(
+                (index, r, ctx) -> {
+                  Map<String, Object> resultRow = foldCase ? new 
CaseFoldedMap() : new HashMap<>();
+                  ResultSetMetaData resultMetadata;
+                  try {
+                    resultMetadata = r.getMetaData();
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to obtain metadata 
from result set", e, ctx);
+                  }
+                  try {
+                    for (int i = 1; i <= resultMetadata.getColumnCount(); i++) 
{
+                      String key = resultMetadata.getColumnName(i);
+                      String alias = resultMetadata.getColumnLabel(i);
+                      Object value = r.getObject(i);
+                      resultRow.put(alias != null ? alias : key, value);
+                    }
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to access specific 
metadata from " +
+                                                 "result set metadata", e, 
ctx);
+                  }
+                  return resultRow;
+                }
+            ).iterator();
+            jg.writeStartArray();
+            while (resultIterator.hasNext()) {
+              jg.writeObject(resultIterator.next());
+            }
+            jg.writeEndArray();
+            jg.close();
+            return null;
+          },
+          (exception) -> {
+            final boolean isStatementException = exception instanceof 
StatementException ||
+                                                 (exception instanceof 
CallbackFailedException
+                                                  && exception.getCause() 
instanceof StatementException);
+            return 
sqlFirehoseDatabaseConnector.isTransientException(exception) && 
!(isStatementException);
+          }
+      );
+    }
+    return new CleanableFile()

Review comment:
       The `tempFile` will not be deleted if an exception is thrown in any 
lines above. We should catch all exceptions and delete the file properly.

##########
File path: docs/ingestion/native-batch.md
##########
@@ -1310,6 +1311,56 @@ A spec that applies a filter and reads a subset of the 
original datasource's col
 This spec above will only return the `page`, `user` dimensions and `added` 
metric.
 Only rows where `page` = `Druid` will be returned.
 
+### SQL Input Source
+
+The SQL input source is used to read data directly from RDBMS.
+The SQL input source is _splittable_ and can be used by the [Parallel 
task](#parallel-task), where each worker task will read from one SQL query from 
the list of queries.
+Since this input source has a fixed input format for reading events, no 
`inputFormat` field needs to be specified in the ingestion spec when using this 
input source.
+
+|property|description|required?|
+|--------|-----------|---------|
+|type|This should be "sql".|Yes|
+|database|Specifies the database connection details. The database type 
corresponds to the extension that supplies the `connectorConfig` support and 
this extension must be loaded into Druid. For database types `mysql` and 
`postgresql`, the `connectorConfig` support is provided by 
[mysql-metadata-storage](../development/extensions-core/mysql.md) and 
[postgresql-metadata-storage](../development/extensions-core/postgresql.md) 
extensions respectively.|Yes|
+|foldCase|Toggle case folding of database column names. This may be enabled in 
cases where the database returns case insensitive column names in query 
results.|No|
+|sqls|List of SQL queries where each SQL query would retrieve the data to be 
indexed.|Yes|
+
+An example SqlInputSource spec is shown below:
+
+```json
+...
+    "ioConfig": {
+      "type": "index_parallel",
+      "inputSource": {
+        "type": "sql",
+        "database": {
+            "type": "mysql",
+            "connectorConfig": {
+                "connectURI": "jdbc:mysql://host:port/schema",
+                "user": "user",
+                "password": "password"
+            }
+        },
+        "sqls": ["SELECT * FROM table1", "SELECT * FROM table2"]
+    },
+...
+```
+
+The spec above will read all events from two separate SQLs within the interval 
`2013-01-01/2013-01-02`.
+Each of the SQL queries will be run in its own sub-task and thus for the above 
example, there would be two sub-tasks.
+
+Compared to the other native batch InputSources, SQL InputSource behaves 
differently in terms of reading the input data and so it would be helpful to 
consider the following points before using this InputSource in a production 
environment:
+
+* During indexing, each sub-task would execute one of the SQL queries and the 
results are stored locally on disk. The sub-tasks then proceed to read the data 
from these local input files and generate segments. Presently, there isn’t any 
restriction on the size of the generated files and this would require the 
MiddleManagers or Indexers to have sufficient disk capacity based on the volume 
of data being indexed.
+
+* Filtering the SQL queries based on the intervals specified in the 
`granularitySpec` can avoid unwanted data being retrieved and stored locally by 
the indexing sub-tasks.

Review comment:
       I'm not sure what it means by "avoid unwanted data being retrieved and 
stored locally". Does this mean the subtask can modify the sql to filter out 
data out of the interval in the granularitySpec? Would you point me out where 
it is implemented?

##########
File path: docs/ingestion/native-batch.md
##########
@@ -1310,6 +1311,43 @@ A spec that applies a filter and reads a subset of the 
original datasource's col
 This spec above will only return the `page`, `user` dimensions and `added` 
metric.
 Only rows where `page` = `Druid` will be returned.
 
+### Sql Input Source

Review comment:
       I would definitely vote for having such a supervisor! That will be super 
useful.

##########
File path: docs/ingestion/native-batch.md
##########
@@ -1310,6 +1311,56 @@ A spec that applies a filter and reads a subset of the 
original datasource's col
 This spec above will only return the `page`, `user` dimensions and `added` 
metric.
 Only rows where `page` = `Druid` will be returned.
 
+### SQL Input Source
+
+The SQL input source is used to read data directly from RDBMS.
+The SQL input source is _splittable_ and can be used by the [Parallel 
task](#parallel-task), where each worker task will read from one SQL query from 
the list of queries.
+Since this input source has a fixed input format for reading events, no 
`inputFormat` field needs to be specified in the ingestion spec when using this 
input source.
+
+|property|description|required?|
+|--------|-----------|---------|
+|type|This should be "sql".|Yes|
+|database|Specifies the database connection details. The database type 
corresponds to the extension that supplies the `connectorConfig` support and 
this extension must be loaded into Druid. For database types `mysql` and 
`postgresql`, the `connectorConfig` support is provided by 
[mysql-metadata-storage](../development/extensions-core/mysql.md) and 
[postgresql-metadata-storage](../development/extensions-core/postgresql.md) 
extensions respectively.|Yes|
+|foldCase|Toggle case folding of database column names. This may be enabled in 
cases where the database returns case insensitive column names in query 
results.|No|
+|sqls|List of SQL queries where each SQL query would retrieve the data to be 
indexed.|Yes|
+
+An example SqlInputSource spec is shown below:
+
+```json
+...
+    "ioConfig": {
+      "type": "index_parallel",
+      "inputSource": {
+        "type": "sql",
+        "database": {
+            "type": "mysql",
+            "connectorConfig": {
+                "connectURI": "jdbc:mysql://host:port/schema",
+                "user": "user",
+                "password": "password"
+            }
+        },
+        "sqls": ["SELECT * FROM table1", "SELECT * FROM table2"]
+    },
+...
+```
+
+The spec above will read all events from two separate SQLs within the interval 
`2013-01-01/2013-01-02`.

Review comment:
       Where is the interval `2013-01-01/2013-01-02` from?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to