Skip to content

Commit 0bc2618

Browse files
committed
Use the runner version that supports the cli
1 parent 9060938 commit 0bc2618

File tree

1 file changed

+63
-24
lines changed

1 file changed

+63
-24
lines changed

src/main/java/io/ecraft/SqlRunner.java

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.ecraft;
22

3-
import jdk.jpackage.internal.Log;
3+
import org.apache.hadoop.fs.FileUtil;
4+
import java.util.zip.*;
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
import java.io.*;
8+
import java.nio.file.*;
9+
import org.apache.commons.io.FilenameUtils;
410
import org.apache.flink.configuration.Configuration;
511
import org.apache.flink.core.fs.FSDataInputStream;
612
import org.apache.flink.core.fs.FileSystem;
@@ -19,16 +25,6 @@
1925
import org.slf4j.Logger;
2026
import org.slf4j.LoggerFactory;
2127

22-
import java.io.BufferedReader;
23-
import java.io.IOException;
24-
import java.io.InputStreamReader;
25-
import java.io.StringWriter;
26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import java.util.Map;
29-
import java.util.stream.Collectors;
30-
31-
3228
public class SqlRunner {
3329
private static final Logger LOG = LoggerFactory.getLogger(SqlRunner.class);
3430

@@ -44,21 +40,22 @@ public static void main(String[] args) throws Exception {
4440
throw new Exception("Exactly one argument is expected.");
4541
}
4642

47-
// StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
48-
// LOG.info("Checkpoint storage: {}",env.getCheckpointConfig().getCheckpointStorage());
49-
// LOG.info("Checkpointing mode: {}", env.getCheckpointConfig().getCheckpointingMode());
50-
// LOG.info("Checkpointing interval: {}", env.getCheckpointInterval());
51-
// StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
52-
53-
CatalogStore catalogStore = new FileCatalogStore(System.getenv("TABLE_CATALOG_STORE_FILE_PATH"));
5443
EnvironmentSettings settings = EnvironmentSettings
5544
.newInstance()
5645
.inStreamingMode()
57-
.withCatalogStore(catalogStore)
5846
.build();
5947
TableEnvironment tableEnv = TableEnvironment.create(settings);
6048

61-
tableEnv.useCatalog("hive");
49+
String name = "hive";
50+
String defaultDatabase = "default";
51+
String hiveConfDir = "/conf/hive-conf";
52+
53+
HiveCatalog hive = new HiveCatalog(name, defaultDatabase, hiveConfDir);
54+
tableEnv.registerCatalog(name, hive);
55+
56+
// set the HiveCatalog as the current catalog of the session
57+
tableEnv.useCatalog(name);
58+
6259
tableEnv.getConfig().setSqlDialect(SqlDialect.DEFAULT);
6360

6461
LOG.debug("Current catalog: {}", tableEnv.getCurrentCatalog());
@@ -69,10 +66,37 @@ public static void main(String[] args) throws Exception {
6966
LOG.debug(" - {}", t);
7067
}
7168

72-
Path sqlPath = new Path(args[0]);
73-
FileSystem fs = sqlPath.getFileSystem();
74-
FSDataInputStream inputStream = fs.open(sqlPath);
75-
InputStreamReader reader = new InputStreamReader(inputStream);
69+
// Read the tar file from azure blob store to a local file
70+
Path remoteArchivePath = new Path(args[0]);
71+
FileSystem remoteArchiveFs = remoteArchivePath.getFileSystem();
72+
FSDataInputStream remoteArchiveStream = remoteArchiveFs.open(remoteArchivePath);
73+
// We name everything after the full name of the archive without extension (including hashes)
74+
String jobName = FilenameUtils.getBaseName(remoteArchivePath.getName());
75+
76+
// Make sure we have the directory for the job files
77+
Files.createDirectories(Paths.get("/tmp/"+ jobName));
78+
79+
// Download the file into the directory
80+
String archiveDownloadPath = "/tmp/"+ jobName + "/" + remoteArchivePath.getName();
81+
FileOutputStream fos = new FileOutputStream(archiveDownloadPath);
82+
transferTo(remoteArchiveStream, fos);
83+
84+
// Uncompress the contents of the zip file to a local directory
85+
ZipFile zipFile = new ZipFile(archiveDownloadPath);
86+
Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
87+
while(zipFileEntries.hasMoreElements()) {
88+
ZipEntry entry = zipFileEntries.nextElement();
89+
FileOutputStream zipEntryOutputStream = new FileOutputStream("/tmp/" + jobName + "/" + entry.getName());
90+
InputStream zipInputStream = zipFile.getInputStream(entry);
91+
transferTo(zipInputStream, zipEntryOutputStream);
92+
}
93+
94+
// Read the sql file
95+
String sqlName = remoteArchivePath.getName().substring(0, remoteArchivePath.getName().lastIndexOf("-")) + ".sql";
96+
Path sqlPath = new Path("/tmp/" + jobName + "/" + sqlName);
97+
FileSystem sqlFs = sqlPath.getFileSystem();
98+
FSDataInputStream sqlInputStream = sqlFs.open(sqlPath);
99+
InputStreamReader reader = new InputStreamReader(sqlInputStream);
76100
String script = new BufferedReader(reader).lines().parallel().collect(Collectors.joining("\n"));
77101

78102
List<String> statements = parseStatements(script, SqlRunner.loadEnvironment());
@@ -83,6 +107,21 @@ public static void main(String[] args) throws Exception {
83107
}
84108
}
85109

110+
public static void transferTo(InputStream input, OutputStream output) throws IOException {
111+
try {
112+
byte[] buffer = new byte[1024];
113+
int len = input.read(buffer);
114+
while (len != -1) {
115+
output.write(buffer, 0, len);
116+
len = input.read(buffer);
117+
}
118+
input.close();
119+
output.close();
120+
} catch (IOException e) {
121+
LOG.debug("Failed transferTo:\n{}", e.getMessage());
122+
throw e;
123+
}
124+
}
86125

87126

88127
public static Map<String, String> loadEnvironment() {

0 commit comments

Comments
 (0)