Skip to content

Commit 72d1113

Browse files
authored
Change canonical tables to views (#1189)
1 parent 111db67 commit 72d1113

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

pipelines/batch/src/main/java/com/google/fhir/analytics/ConvertResourceFn.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public void processElement(ProcessContext processContext)
206206

207207
private Resource createNewFhirResource(String fhirVersion, String resourceType) {
208208
try {
209+
// TODO create tests for this method and different versions of FHIR; casting to R4 resource
210+
// does not seem right!
209211
return (Resource)
210212
Class.forName(getFhirBasePackageName(fhirVersion) + "." + resourceType)
211213
.getConstructor()

pipelines/controller/src/main/java/com/google/fhir/analytics/HiveTableManager.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,21 @@ private synchronized void createTablesForResource(
9696
Connection connection, String resource, String timestamp, String thriftServerParquetPath)
9797
throws SQLException {
9898

99+
String location =
100+
String.format("%s/%s/%s", THRIFT_CONTAINER_PARQUET_DIR, thriftServerParquetPath, resource);
101+
String tableName = String.format("%s_%s", resource, timestamp);
99102
String sql =
100103
String.format(
101-
"CREATE TABLE IF NOT EXISTS default.%s_%s USING PARQUET LOCATION '%s/%s/%s'",
102-
resource, timestamp, THRIFT_CONTAINER_PARQUET_DIR, thriftServerParquetPath, resource);
104+
"CREATE TABLE IF NOT EXISTS default.%s USING PARQUET LOCATION '%s'",
105+
tableName, location);
103106
executeSql(connection, sql);
104107

105-
// Drop canonical table if exists.
106-
sql = String.format("DROP TABLE IF EXISTS default.%s", resource);
107-
executeSql(connection, sql);
108-
109-
// Create canonical table with latest parquet files.
108+
// Instead of DROP and CREATE we use a VIEW for canonical tables such that the update happens
109+
// in one statement (because of lack of transactions in Hive JDBC driver). ALTER TABLE has its
110+
// own problems too, e.g., it does not seem to trigger parsing/changing schema.
110111
sql =
111112
String.format(
112-
"CREATE TABLE IF NOT EXISTS default.%s USING PARQUET LOCATION '%s/%s/%s'",
113-
resource, THRIFT_CONTAINER_PARQUET_DIR, thriftServerParquetPath, resource);
113+
"CREATE OR REPLACE VIEW default.%s AS SELECT * FROM default.%s", resource, tableName);
114114
executeSql(connection, sql);
115115
}
116116

pipelines/controller/src/main/java/com/google/fhir/analytics/PipelineManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ public void createResourceTables() {
568568

569569
Preconditions.checkState(paths != null, "Make sure DWH prefix is a valid path!");
570570

571-
// Sort snapshots directories.
571+
// Sort snapshots directories such that the canonical view is created for the latest one.
572572
Collections.sort(paths, Comparator.comparing(ResourceId::toString));
573573

574574
for (ResourceId path : paths) {
@@ -579,15 +579,20 @@ public void createResourceTables() {
579579
String fileSeparator = DwhFiles.getFileSeparatorForDwhFiles(rootPrefix);
580580
List<String> existingResources =
581581
dwhFilesManager.findExistingResources(baseDir + fileSeparator + path.getFilename());
582-
hiveTableManager.createResourceAndCanonicalTables(
583-
existingResources, timestamp, path.getFilename());
582+
try {
583+
hiveTableManager.createResourceAndCanonicalTables(
584+
existingResources, timestamp, path.getFilename());
585+
} catch (SQLException e) {
586+
logger.error(
587+
"Exception while creating resource table on thriftserver for path: {}",
588+
path.getFilename(),
589+
e);
590+
}
584591
}
585592
}
586593
} catch (IOException e) {
587594
// In case of exceptions at this stage, we just log the exception.
588595
logger.error("Exception while reading thriftserver parquet output directory: ", e);
589-
} catch (SQLException e) {
590-
logger.error("Exception while creating resource tables on thriftserver: ", e);
591596
}
592597
}
593598

0 commit comments

Comments
 (0)