-
Notifications
You must be signed in to change notification settings - Fork 83
/
HiveTableManager.java
148 lines (132 loc) · 5.34 KB
/
HiveTableManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2020-2023 Google LLC
*
* Licensed 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.openmrs.analytics;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** This class manages to create resources on Thrift Server post each pipeline run. */
public class HiveTableManager {
private static final Logger logger = LoggerFactory.getLogger(HiveTableManager.class.getName());
private final String jdbcUrl;
private final String user;
private final String password;
private static final String THRIFT_CONTAINER_PARQUET_PATH_PREFIX = "/dwh";
public HiveTableManager(String jdbcUrl, String user, String password) {
this.jdbcUrl = jdbcUrl;
this.user = user;
this.password = password;
}
/**
* Method to create resources on Thrift Server Hive.
*
* @param resourceList Comma separated list of resources such as Patient, Observation, Encounter
* @param timestamp Timestamp suffix to be used in table name.
* @param thriftServerParquetPath location of parquet files in Thrift Server
* @throws SQLException
*/
public void createResourceTables(
String resourceList, String timestamp, String thriftServerParquetPath) throws SQLException {
if (resourceList == null || resourceList.isEmpty()) {
return;
}
String[] resources = resourceList.split(",");
if (resources == null || resources.length == 0) {
return;
}
// TODO: Make use of JdbcConnectionUtil to create jdbc connection
// (https://github.com/google/fhir-data-pipes/issues/483)
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
for (String resource : resources) {
createResourceAndCanonicalTables(connection, resource, timestamp, thriftServerParquetPath);
}
}
}
/**
* This method will create table 'encounter_2023_01_24t18_42_54_302111z' if the given resource is
* Encounter and the timestamp suffix is 2023_01_24t18_42_54_302111z
*
* <p>wrt PARQUET LOCATION, THRIFT_CONTAINER_PARQUET_PATH_PREFIX is the directory hosting parquet
* files, thriftServerParquetPath is the exact path for parquet files and resource shall be the
* respective resource name e.g. Patient
*/
private void createResourceAndCanonicalTables(
Connection connection, String resource, String timestamp, String thriftServerParquetPath)
throws SQLException {
String sql =
String.format(
"CREATE TABLE IF NOT EXISTS default.%s_%s USING PARQUET LOCATION '%s/%s/%s'",
resource,
timestamp,
THRIFT_CONTAINER_PARQUET_PATH_PREFIX,
thriftServerParquetPath,
resource);
executeSql(connection, sql);
// Drop canonical table if exists.
sql = String.format("DROP TABLE IF EXISTS default.%s", resource);
executeSql(connection, sql);
// Create canonical table with latest parquet files.
sql =
String.format(
"CREATE TABLE IF NOT EXISTS default.%s USING PARQUET LOCATION '%s/%s/%s'",
resource, THRIFT_CONTAINER_PARQUET_PATH_PREFIX, thriftServerParquetPath, resource);
executeSql(connection, sql);
}
/**
* The method creates resource tables with names suffixed with given timestamp if not present. It
* excepts complete parquet files path.
*
* @param resource FHIR resource type.
* @param timestamp timestamp string to be used for resource table name.
* @param thriftServerParquetPath directory path having output parquet files.
* @throws SQLException
*/
public void createResourceTable(String resource, String timestamp, String thriftServerParquetPath)
throws SQLException {
String sql =
String.format(
"CREATE TABLE IF NOT EXISTS default.%s_%s USING PARQUET LOCATION '%s'",
resource, timestamp, thriftServerParquetPath);
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
executeSql(connection, sql);
}
}
/**
* This method creates canonical resource tables if not present. It excepts complete parquet files
* path.
*
* @param resource FHIR resource type.
* @param thriftServerParquetPath directory path having output parquet files.
* @throws SQLException
*/
public void createResourceCanonicalTable(String resource, String thriftServerParquetPath)
throws SQLException {
String sql =
String.format(
"CREATE TABLE IF NOT EXISTS default.%s USING PARQUET LOCATION '%s'",
resource, thriftServerParquetPath);
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
executeSql(connection, sql);
}
}
private void executeSql(Connection connection, String sql) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.execute(sql);
}
}
}