/
HiveMetaStore.java
370 lines (332 loc) · 12.1 KB
/
HiveMetaStore.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2018 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.confluent.connect.storage.hive;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.UnknownDBException;
import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hive.hcatalog.common.HCatUtil;
import org.apache.kafka.common.config.AbstractConfig;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.confluent.connect.storage.errors.HiveMetaStoreException;
public class HiveMetaStore {
private static final Logger log = LoggerFactory.getLogger(HiveMetaStore.class);
protected final IMetaStoreClient client;
public HiveMetaStore(AbstractConfig connectorConfig) {
this(new Configuration(), connectorConfig);
}
public HiveMetaStore(Configuration conf, AbstractConfig connectorConfig)
throws HiveMetaStoreException {
HiveConf hiveConf = new HiveConf(conf, HiveConf.class);
String hiveConfDir = connectorConfig.getString(HiveConfig.HIVE_CONF_DIR_CONFIG);
String hiveMetaStoreUris = connectorConfig.getString(HiveConfig.HIVE_METASTORE_URIS_CONFIG);
if (hiveMetaStoreUris.isEmpty()) {
log.warn(
"hive.metastore.uris empty, an embedded Hive metastore will be created in the directory"
+ " the connector is started. You need to start Hive in that specific directory to "
+ "query the data."
);
}
if (!hiveConfDir.equals("")) {
String hiveSitePath = hiveConfDir + "/hive-site.xml";
File hiveSite = new File(hiveSitePath);
if (!hiveSite.exists()) {
log.warn(
"hive-site.xml does not exist in provided Hive configuration directory {}.",
hiveConf
);
}
hiveConf.addResource(new Path(hiveSitePath));
}
hiveConf.set("hive.metastore.uris", hiveMetaStoreUris);
try {
client = HCatUtil.getHiveMetastoreClient(hiveConf);
} catch (IOException | MetaException e) {
throw new HiveMetaStoreException(e);
}
}
private interface ClientAction<R> {
R call() throws TException;
}
private <R> R doAction(ClientAction<R> action) throws HiveMetaStoreException {
// No need to implement retries here. We use RetryingMetaStoreClient which creates a proxy
// for a IMetaStoreClient implementation and retries calls to it on failure. The retrying
// client is conscious of the socket timeout and does not call reconnect on an open
// connection. Since HiveMetaStoreClient's reconnect method does not check the status of the
// connection, blind retries may cause a huge spike in the number of connections to the Hive
// MetaStore.
try {
return action.call();
} catch (MetaException e) {
throw new HiveMetaStoreException("Hive MetaStore exception", e);
} catch (TException e) {
throw new HiveMetaStoreException("Exception communicating with the Hive MetaStore", e);
}
}
public void addPartition(final String database, final String tableName, final String path)
throws HiveMetaStoreException {
ClientAction<Void> addPartition = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
// purposely don't check if the partition already exists because
// getPartition(db, table, path) will throw an exception to indicate the
// partition doesn't exist also. this way, it's only one call.
client.appendPartition(database, tableNameConverter(tableName), path);
} catch (AlreadyExistsException e) {
// this is okay
} catch (InvalidObjectException e) {
throw new HiveMetaStoreException(
"Invalid partition for "
+ database
+ "."
+ tableNameConverter(tableName)
+ ": "
+ path,
e
);
}
return null;
}
};
doAction(addPartition);
}
public void dropPartition(final String database, final String tableName, final String path)
throws HiveMetaStoreException {
ClientAction<Void> dropPartition = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.dropPartition(database, tableNameConverter(tableName), path, false);
} catch (NoSuchObjectException e) {
// this is okay
} catch (InvalidObjectException e) {
throw new HiveMetaStoreException(
"Invalid partition for "
+ database
+ "."
+ tableNameConverter(tableName)
+ ": "
+ path,
e
);
}
return null;
}
};
doAction(dropPartition);
}
public void createDatabase(final String database) throws HiveMetaStoreException {
ClientAction<Void> create = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.createDatabase(
new Database(database, "Database created by Kafka Connect", null, null)
);
} catch (AlreadyExistsException e) {
log.warn("Hive database already exists: {}", database);
} catch (InvalidObjectException e) {
throw new HiveMetaStoreException("Invalid database: " + database, e);
}
return null;
}
};
doAction(create);
}
public void dropDatabase(
final String name,
final boolean deleteData
) throws HiveMetaStoreException {
ClientAction<Void> drop = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.dropDatabase(name, deleteData, true);
} catch (NoSuchObjectException e) {
// this is ok
}
return null;
}
};
doAction(drop);
}
public void createTable(final Table table) throws HiveMetaStoreException {
ClientAction<Void> create = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.createTable(table.getTTable());
} catch (NoSuchObjectException e) {
throw new HiveMetaStoreException(
"Hive table not found: " + table.getDbName() + "." + table.getTableName()
);
} catch (AlreadyExistsException e) {
// this is ok
log.warn("Hive table already exists: {}.{}", table.getDbName(), table.getTableName());
} catch (InvalidObjectException e) {
throw new HiveMetaStoreException("Invalid table", e);
}
return null;
}
};
createDatabase(table.getDbName());
doAction(create);
}
public void alterTable(final Table table, final boolean cascade) throws HiveMetaStoreException {
ClientAction<Void> alter = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.alter_table(table.getDbName(), table.getTableName(), table.getTTable(), cascade);
} catch (NoSuchObjectException e) {
throw new HiveMetaStoreException(
"Hive table not found: " + table.getDbName() + "." + table.getTableName()
);
} catch (InvalidObjectException e) {
throw new HiveMetaStoreException("Invalid table", e);
} catch (InvalidOperationException e) {
throw new HiveMetaStoreException("Invalid table change", e);
}
return null;
}
};
doAction(alter);
}
public void alterTable(final Table table) throws HiveMetaStoreException {
alterTable(table, false);
}
public void dropTable(final String database, final String tableName) {
ClientAction<Void> drop = new ClientAction<Void>() {
@Override
public Void call() throws TException {
try {
client.dropTable(database, tableNameConverter(tableName), false, true);
} catch (NoSuchObjectException e) {
// this is okay
}
return null;
}
};
doAction(drop);
}
public boolean tableExists(
final String database,
final String tableName
) throws HiveMetaStoreException {
ClientAction<Boolean> exists = new ClientAction<Boolean>() {
@Override
public Boolean call() throws TException {
try {
return client.tableExists(database, tableNameConverter(tableName));
} catch (UnknownDBException e) {
return false;
}
}
};
return doAction(exists);
}
public Table getTable(
final String database,
final String tableName
) throws HiveMetaStoreException {
ClientAction<Table> getTable = new ClientAction<Table>() {
@Override
public Table call() throws TException {
try {
return new Table(client.getTable(database, tableNameConverter(tableName)));
} catch (NoSuchObjectException e) {
throw new HiveMetaStoreException(
"Hive table not found: " + database + "." + tableNameConverter(tableName)
);
}
}
};
Table table = doAction(getTable);
if (table == null) {
throw new HiveMetaStoreException(
"Could not find info for table: " + tableNameConverter(tableName)
);
}
return table;
}
public List<String> listPartitions(final String database, final String tableName, final short max)
throws HiveMetaStoreException {
ClientAction<List<String>> listPartitions = new ClientAction<List<String>>() {
@Override
public List<String> call() throws TException {
try {
List<Partition> partitions = client.listPartitions(
database,
tableNameConverter(tableName),
max
);
List<String> paths = new ArrayList<>();
for (Partition partition : partitions) {
paths.add(partition.getSd().getLocation());
}
return paths;
} catch (NoSuchObjectException e) {
return new ArrayList<>();
}
}
};
return doAction(listPartitions);
}
public List<String> getAllTables(final String database) throws HiveMetaStoreException {
ClientAction<List<String>> getAllTables = new ClientAction<List<String>>() {
@Override
public List<String> call() throws TException {
try {
return client.getAllTables(database);
} catch (NoSuchObjectException e) {
return new ArrayList<>();
}
}
};
return doAction(getAllTables);
}
public List<String> getAllDatabases() throws HiveMetaStoreException {
ClientAction<List<String>> create = new ClientAction<List<String>>() {
@Override
public List<String> call() throws TException {
try {
return client.getAllDatabases();
} catch (NoSuchObjectException e) {
return new ArrayList<>();
}
}
};
return doAction(create);
}
public String tableNameConverter(String table) {
return table == null ? table : table.replaceAll("[.-]", "_");
}
}