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



##########
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);

Review comment:
       Please move `jg` to the above `try-resource` clause so that i can be 
closed safely.




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