Skip to content

Commit

Permalink
Add support for Glue Hive metastore
Browse files Browse the repository at this point in the history
Allows users to use AWS Glue Data Catalog as metastore in Hive connector
  • Loading branch information
Rentao Wu authored and electrum committed Mar 27, 2018
1 parent 0a0bc69 commit 84be87e
Show file tree
Hide file tree
Showing 16 changed files with 1,840 additions and 16 deletions.
26 changes: 25 additions & 1 deletion pom.xml
Expand Up @@ -47,7 +47,7 @@
<dep.airlift.version>0.164</dep.airlift.version>
<dep.packaging.version>${dep.airlift.version}</dep.packaging.version>
<dep.slice.version>0.33</dep.slice.version>
<dep.aws-sdk.version>1.11.165</dep.aws-sdk.version>
<dep.aws-sdk.version>1.11.293</dep.aws-sdk.version>
<dep.okhttp.version>3.9.0</dep.okhttp.version>
<dep.jdbi3.version>3.0.0</dep.jdbi3.version>
<dep.tempto.version>1.45</dep.tempto.version>
Expand Down Expand Up @@ -846,6 +846,26 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-glue</artifactId>
<version>${dep.aws-sdk.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand All @@ -858,6 +878,10 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
32 changes: 20 additions & 12 deletions presto-hive/pom.xml
Expand Up @@ -147,23 +147,16 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-glue</artifactId>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down Expand Up @@ -290,4 +283,19 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<!-- Exclude tests against Glue from build -->
<exclude>**/TestHiveClientGlueMetastore.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.hive.metastore;

import com.facebook.presto.hive.metastore.file.FileMetastoreModule;
import com.facebook.presto.hive.metastore.glue.GlueMetastoreModule;
import com.facebook.presto.hive.metastore.thrift.ThriftMetastoreModule;
import com.google.inject.Binder;
import com.google.inject.Module;
Expand Down Expand Up @@ -44,6 +45,7 @@ protected void setup(Binder binder)
else {
bindMetastoreModule("thrift", new ThriftMetastoreModule(connectorId));
bindMetastoreModule("file", new FileMetastoreModule(connectorId));
bindMetastoreModule("glue", new GlueMetastoreModule(connectorId));
}
}

Expand Down
@@ -0,0 +1,84 @@
/*
* 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 com.facebook.presto.hive.metastore.glue;

import com.amazonaws.services.glue.model.GetPartitionsRequest;
import com.facebook.presto.hive.HiveType;
import com.facebook.presto.hive.metastore.Column;
import com.facebook.presto.spi.PrestoException;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static com.facebook.presto.hive.HiveErrorCode.HIVE_METASTORE_ERROR;

public class GlueExpressionUtil
{
private static final Joiner JOINER = Joiner.on(" AND ");
private static final Set<String> QUOTED_TYPES = ImmutableSet.of("string", "char", "varchar", "date", "timestamp", "binary", "varbinary");

private GlueExpressionUtil() {}

/**
* <pre>
* Build an expression string used for partition filtering in {@link GetPartitionsRequest}
*
* Ex: partition keys: ['a', 'b']
* partition values: ['1', '2']
* expression: (a='1') AND (b='2')
*
* Partial specification ex:
* partition values: ['', '2']
* expression: (b='2')
* </pre>
* @param partitionKeys
* @param partitionValues Full or partial list of partition values to filter on. Keys without filter should be empty string.
*/
public static String buildExpression(List<Column> partitionKeys, List<String> partitionValues)
{
if (partitionValues == null || partitionValues.isEmpty()) {
return null;
}

if (partitionKeys == null || partitionValues.size() != partitionKeys.size()) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Incorrect number of partition values: " + partitionValues);
}

List<String> predicates = new LinkedList<>();
for (int i = 0; i < partitionValues.size(); i++) {
if (!Strings.isNullOrEmpty(partitionValues.get(i))) {
predicates.add(buildPredicate(partitionKeys.get(i), partitionValues.get(i)));
}
}

return JOINER.join(predicates);
}

private static String buildPredicate(Column partitionKey, String value)
{
if (isQuotedType(partitionKey.getType())) {
return String.format("(%s='%s')", partitionKey.getName(), value);
}
return String.format("(%s=%s)", partitionKey.getName(), value);
}

private static boolean isQuotedType(HiveType type)
{
return QUOTED_TYPES.contains(type.getTypeSignature().getBase());
}
}

0 comments on commit 84be87e

Please sign in to comment.