Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing hive partition getQualifiedName, adding resolver ending point #111

Merged
merged 3 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ servo_version=0.8.3
spock_version=1.0-groovy-2.4
swagger_version=1.3.12
testing_mysql_server_version=0.1

## speed up the build process
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work? cool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works!

org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2G
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.netflix.metacat.common.api.MetacatV1;
import com.netflix.metacat.common.api.MetadataV1;
import com.netflix.metacat.common.api.PartitionV1;
import com.netflix.metacat.common.api.ResolverV1;
import com.netflix.metacat.common.json.MetacatJsonLocator;
import feign.Feign;
import feign.Request;
Expand All @@ -48,6 +49,7 @@ public final class Client {
private final String host;
private final PartitionV1 partitionApi;
private final MetadataV1 metadataApi;
private final ResolverV1 resolverApi;

private Client(
@Nonnull
Expand Down Expand Up @@ -87,6 +89,7 @@ private Client(
api = getApiClient(MetacatV1.class);
partitionApi = getApiClient(PartitionV1.class);
metadataApi = getApiClient(MetadataV1.class);
resolverApi = getApiClient(ResolverV1.class);
}

/**
Expand Down Expand Up @@ -289,4 +292,13 @@ public PartitionV1 getPartitionApi() {
public MetadataV1 getMetadataApi() {
return metadataApi;
}

/**
* Return an API instance that can be used to interact with
* the metacat server for getting the qualified name by uri.
* @return An instance api conforming to ResolverV1 interface
*/
public ResolverV1 getResolverApi() {
return resolverApi;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2017 Netflix, Inc.
*
* 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
*
* https://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.netflix.metacat.common.api;

import com.netflix.metacat.common.dto.ResolveByUriRequestDto;
import com.netflix.metacat.common.dto.ResolveByUriResponseDto;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


/**
* ResolverV1.
*
* @author zhenl
* @since 1.0.0
*/
@Path("mds/v1/resolver")
@Api(value = "ResolverV1",
description = "Metadata resolver operations",
produces = MediaType.APPLICATION_JSON,
consumes = MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface ResolverV1 {
/**
* resolveByUri.
*
* @param prefixSearch use prefix search
* @param resolveByUriRequestDto resolveByUriRequest
* @return response from uri search
*/
@POST
@ApiOperation(
position = 0,
value = "Returns the list of qualified names of tables and partitions containing the given URI path",
notes = "Returns the list of qualified names of tables and partitions containing the given URI path")
ResolveByUriResponseDto resolveByUri(
@ApiParam(value = "do prefix search for URI", required = false)
@DefaultValue("false")
@QueryParam("prefixSearch")
Boolean prefixSearch,
ResolveByUriRequestDto resolveByUriRequestDto);

/**
* isUriUsedMoreThanOnce.
*
* @param prefixSearch use prefix search
* @param resolveByUriRequestDto resolveByUriRequest
* @return response of check if a uri used more than once
*/
@POST
@Path("isUriUsedMoreThanOnce")
@ApiOperation(
position = 1,
value = "Returns status 200 if the given URI is being referred more than once."
+ " Returns status 404 if the given URI is not found or not being referred more than once.",
notes = "Returns status 200 if the given URI is being referred more than once."
+ " Returns status 404 if the given URI is not found or not being referred more than once.")
Response isUriUsedMoreThanOnce(
@ApiParam(value = "do prefix search for URI", required = false)
@DefaultValue("false")
@QueryParam("prefixSearch")
Boolean prefixSearch,
ResolveByUriRequestDto resolveByUriRequestDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.netflix.metacat.common.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* ResolveByUriRequestDto.
*
* @author zhenl
* @since 1.0.0
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class ResolveByUriRequestDto extends BaseDto {
private static final long serialVersionUID = -2649784382533439526L;
private String uri;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.netflix.metacat.common.dto;

import com.netflix.metacat.common.QualifiedName;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
* ResolveByUriResponseDto.
*
* @author zhenl
* @since 1.0.0
*/
@Data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is a DTO please make the variables final. That way lombok won't create setters.

@EqualsAndHashCode(callSuper = false)
public class ResolveByUriResponseDto extends BaseDto {
private static final long serialVersionUID = -4505346090786555046L;
private List<QualifiedName> tables;
private List<QualifiedName> partitions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ public List<String> getPartitionKeys(@Nonnull @NonNull final ConnectorContext re
* @param prefixSearch prefixSearch
* @return partition names
*/
public Map<String, List<QualifiedName>> getPartitionNames(final List<String> uris, final boolean prefixSearch) {
@Override
public Map<String, List<QualifiedName>> getPartitionNames(
@Nonnull final ConnectorContext context,
@Nonnull final List<String> uris,
final boolean prefixSearch) {
final Map<String, List<QualifiedName>> result = Maps.newHashMap();
// Get data source
final DataSource dataSource = DataSourceManager.get().get(catalogName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ String getNameOfPartition(@Nonnull @NonNull final Table table, @Nonnull @NonNull
* @param fields fields
* @return partition keys
*/
public List<String> getPartitionKeys(final List<FieldSchema> fields) {
protected List<String> getPartitionKeys(final List<FieldSchema> fields) {
final List<String> result = Lists.newArrayList();
if (fields != null) {
result.addAll(fields.stream().map(FieldSchema::getName).collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.netflix.metacat.client.Client
import com.netflix.metacat.common.QualifiedName
import com.netflix.metacat.common.api.MetacatV1
import com.netflix.metacat.common.api.PartitionV1
import com.netflix.metacat.common.api.ResolverV1
import com.netflix.metacat.common.dto.*
import com.netflix.metacat.common.exception.MetacatAlreadyExistsException
import com.netflix.metacat.common.exception.MetacatBadRequestException
Expand All @@ -47,14 +48,14 @@ import static TestUtilities.dateCloseEnough
class MetacatFunctionalSpec extends Specification {
public static MetacatV1 api
public static PartitionV1 partitionApi
public static ResolverV1 resolverApi
public static final MetacatJson metacatJson = MetacatJsonLocator.INSTANCE
public static final long BATCH_ID = System.currentTimeSeconds()
public static final int timediff = 24 * 3600

def setupSpec() {
String httpPort = System.properties['metacat_http_port']?.toString()?.trim()
assert httpPort, 'Required system property "metacat_http_port" is not set'

def client = Client.builder()
.withHost("https://localhost:$httpPort")
.withDataTypeContext('pig')
Expand All @@ -64,9 +65,8 @@ class MetacatFunctionalSpec extends Specification {
.build()
api = client.api
partitionApi = client.partitionApi

resolverApi = client.resolverApi
TestCatalogs.resetAll()

}


Expand Down Expand Up @@ -518,6 +518,17 @@ class MetacatFunctionalSpec extends Specification {
then:
database.tables.contains(tableName)

when:
def resovlerRep = resolverApi.resolveByUri(false, new ResolveByUriRequestDto(uri: dataUri))
then:
!resovlerRep.tables.empty

when:
resovlerRep = resolverApi.resolveByUri(true,
new ResolveByUriRequestDto(uri: "file:/tmp/${catalog.name}/${databaseName}/".toString()))
then:
!resovlerRep.tables.empty

when:
def table = api.getTable(catalog.name, databaseName, tableName, true, true, true)
def name = QualifiedName.ofTable(catalog.name, databaseName, tableName)
Expand Down Expand Up @@ -773,6 +784,8 @@ class MetacatFunctionalSpec extends Specification {
]
)

def resolveByUridto = new ResolveByUriRequestDto(uri: dataUri)

when:
def keys = partitionApi.getPartitionKeys(name.catalogName, name.databaseName, name.tableName, null, null, null, null, null)

Expand All @@ -787,6 +800,22 @@ class MetacatFunctionalSpec extends Specification {
response.added.contains(escapedName.partitionName)
response.updated.empty

when:
def resovlerRep = resolverApi.resolveByUri(false, resolveByUridto)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Please add a test case with prefixSearch=true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test.

then:
!resovlerRep.partitions.empty

when:
resovlerRep = resolverApi.resolveByUri(true,
new ResolveByUriRequestDto(uri: "file:/tmp/${name.catalogName}/${name.databaseName}/${name.tableName}".toString()))
then:
!resovlerRep.partitions.empty

when:
def usedMoreThanOnce = resolverApi.isUriUsedMoreThanOnce(false, resolveByUridto)
then:
thrown(MetacatNotFoundException)

when:
keys = partitionApi.getPartitionKeys(name.catalogName, name.databaseName, name.tableName, null, null, null, null, null)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.netflix.metacat.main.api;

import com.google.inject.Inject;
import com.netflix.metacat.common.api.ResolverV1;
import com.netflix.metacat.common.dto.ResolveByUriRequestDto;
import com.netflix.metacat.common.dto.ResolveByUriResponseDto;
import com.netflix.metacat.main.services.PartitionService;
import com.netflix.metacat.main.services.TableService;

import javax.ws.rs.core.Response;

/**
* ResovlerServiceImpl.
*
* @author zhenl
* @since 1.0.0
*/
public class ResovlerV1Resource implements ResolverV1 {
private TableService tableService;
private PartitionService partitionService;

/**
* ResovlerServiceImpl.
* @param tableService table service
* @param partitionService partition service
*/
@Inject
ResovlerV1Resource(final TableService tableService, final PartitionService partitionService) {
this.tableService = tableService;
this.partitionService = partitionService;
}

/**
* Gets the qualified name by uri.
*
* @param resolveByUriRequestDto resolveByUriRequestDto
* @param prefixSearch search by prefix flag
* @return the qualified name of uri
*/
@Override
public ResolveByUriResponseDto resolveByUri(final Boolean prefixSearch,
final ResolveByUriRequestDto resolveByUriRequestDto) {
final ResolveByUriResponseDto result = new ResolveByUriResponseDto();
result.setTables(tableService.getQualifiedNames(resolveByUriRequestDto.getUri(), prefixSearch));
result.setPartitions(partitionService.getQualifiedNames(resolveByUriRequestDto.getUri(), prefixSearch));
return result;
}

/**
* Check if the uri used more than once.
*
* @param prefixSearch search by prefix flag
* @param resolveByUriRequestDto resolveByUriRequestDto
* @return true if the uri used more than once
*/
@Override
public Response isUriUsedMoreThanOnce(final Boolean prefixSearch,
final ResolveByUriRequestDto resolveByUriRequestDto) {
int size = tableService.getQualifiedNames(resolveByUriRequestDto.getUri(), prefixSearch).size();
if (size < 2) {
size += partitionService.getQualifiedNames(resolveByUriRequestDto.getUri(), prefixSearch).size();
}
if (size > 1) {
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import com.netflix.metacat.common.api.MetacatV1;
import com.netflix.metacat.common.api.MetadataV1;
import com.netflix.metacat.common.api.PartitionV1;
import com.netflix.metacat.common.api.ResolverV1;
import com.netflix.metacat.common.api.SearchMetacatV1;
import com.netflix.metacat.common.api.TagV1;
import com.netflix.metacat.common.server.CommonModule;
import com.netflix.metacat.main.api.MetacatV1Resource;
import com.netflix.metacat.main.api.MetadataV1Resource;
import com.netflix.metacat.main.api.PartitionV1Resource;
import com.netflix.metacat.main.api.ResovlerV1Resource;
import com.netflix.metacat.main.api.SearchMetacatV1Resource;
import com.netflix.metacat.main.api.TagV1Resource;
import com.netflix.metacat.main.manager.ManagerModule;
Expand All @@ -46,5 +48,6 @@ protected void configureServlets() {
binder().bind(SearchMetacatV1.class).to(SearchMetacatV1Resource.class).asEagerSingleton();
binder().bind(TagV1.class).to(TagV1Resource.class).asEagerSingleton();
binder().bind(MetacatThriftService.class).asEagerSingleton();
binder().bind(ResolverV1.class).to(ResovlerV1Resource.class).asEagerSingleton();
}
}
Loading