Search in sources :

Example 1 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testInvalidSearchParams.

@Test
public void testInvalidSearchParams() throws Exception {
    NamespaceId namespace = new NamespaceId("invalid");
    Set<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
    try {
        searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY);
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort field
    try {
        searchMetadata(namespace, "*", targets, "name asc");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort order
    try {
        searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " unknown");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with numCursors for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 1, null);
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with cursor for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 0, "cursor");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with invalid query
    try {
        searchMetadata(NamespaceId.DEFAULT, "");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 2 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class AbstractMetadataClient method searchMetadata.

/**
   * Searches entities in the specified namespace whose metadata matches the specified query.
   *
   * @param namespace the namespace to search in
   * @param query the query string with which to search
   * @param targets {@link EntityTypeSimpleName}s to search. If empty, all possible types will be searched
   * @param sort specifies sort field and sort order. If {@code null}, the sort order is by relevance
   * @param offset the index to start with in the search results. To return results from the beginning, pass {@code 0}
   * @param limit the number of results to return, starting from #offset. To return all, pass {@link Integer#MAX_VALUE}
   * @param numCursors the number of cursors to return in the response. A cursor identifies the first index of the
   *                   next page for pagination purposes
   * @param cursor the cursor that acts as the starting index for the requested page. This is only applicable when
   *               #sortInfo is not default. If offset is also specified, it is applied starting at
   *               the cursor. If {@code null}, the first row is used as the cursor
   * @param showHidden boolean which specifies whether to display hidden entities (entity whose name start with "_")
   *                    or not.
   * @return A set of {@link MetadataSearchResultRecord} for the given query.
   */
public MetadataSearchResponse searchMetadata(Id.Namespace namespace, String query, Set<EntityTypeSimpleName> targets, @Nullable String sort, int offset, int limit, int numCursors, @Nullable String cursor, boolean showHidden) throws IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("metadata/search?query=%s", query);
    for (EntityTypeSimpleName t : targets) {
        path += "&target=" + t;
    }
    if (sort != null) {
        path += "&sort=" + URLEncoder.encode(sort, "UTF-8");
    }
    path += "&offset=" + offset;
    path += "&limit=" + limit;
    path += "&numCursors=" + numCursors;
    if (cursor != null) {
        path += "&cursor=" + cursor;
    }
    if (showHidden) {
        path += "&showHidden=" + true;
    }
    URL searchURL = resolve(namespace, path);
    HttpResponse response = execute(HttpRequest.get(searchURL).build(), HttpResponseStatus.BAD_REQUEST.getCode());
    if (HttpResponseStatus.BAD_REQUEST.getCode() == response.getResponseCode()) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
    return GSON.fromJson(response.getResponseBodyAsString(), MetadataSearchResponse.class);
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 3 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testSearchResultPagination.

@Test
public void testSearchResultPagination() throws Exception {
    NamespaceId namespace = new NamespaceId("pagination");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    StreamId stream = namespace.stream("text");
    DatasetId dataset = namespace.dataset("mydataset");
    StreamViewId view = stream.view("view");
    DatasetId trackerDataset = namespace.dataset("_auditLog");
    // create entities so system metadata is annotated
    streamClient.create(stream);
    streamViewClient.createOrUpdate(view, new ViewSpecification(new FormatSpecification("csv", null, null)));
    datasetClient.create(dataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
    datasetClient.create(trackerDataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
    // search with showHidden to true
    EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
    String sort = AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " asc";
    // search to get all the above entities offset 0, limit interger max  and cursors 0
    MetadataSearchResponse searchResponse = searchMetadata(namespace, "*", targets, sort, 0, Integer.MAX_VALUE, 0, null, true);
    List<MetadataSearchResultRecord> expectedResults = ImmutableList.of(new MetadataSearchResultRecord(trackerDataset), new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view));
    List<String> expectedCursors = ImmutableList.of();
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertEquals(expectedCursors, searchResponse.getCursors());
    // no offset, limit 1, no cursors
    searchResponse = searchMetadata(namespace, "*", targets, sort, 0, 1, 0, null);
    expectedResults = ImmutableList.of(new MetadataSearchResultRecord(dataset));
    expectedCursors = ImmutableList.of();
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertEquals(expectedCursors, searchResponse.getCursors());
    // no offset, limit 1, 2 cursors, should return 1st result, with 2 cursors
    searchResponse = searchMetadata(namespace, "*", targets, sort, 0, 1, 2, null);
    expectedResults = ImmutableList.of(new MetadataSearchResultRecord(dataset));
    expectedCursors = ImmutableList.of(stream.getEntityName(), view.getEntityName());
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertEquals(expectedCursors, searchResponse.getCursors());
    // offset 1, limit 1, 2 cursors, should return 2nd result, with only 1 cursor since we don't have enough data
    searchResponse = searchMetadata(namespace, "*", targets, sort, 1, 1, 2, null);
    expectedResults = ImmutableList.of(new MetadataSearchResultRecord(stream));
    expectedCursors = ImmutableList.of(view.getEntityName());
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertEquals(expectedCursors, searchResponse.getCursors());
    // offset 2, limit 1, 2 cursors, should return 3rd result, with 0 cursors since we don't have enough data
    searchResponse = searchMetadata(namespace, "*", targets, sort, 2, 1, 2, null);
    expectedResults = ImmutableList.of(new MetadataSearchResultRecord(view));
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertTrue(searchResponse.getCursors().isEmpty());
    // offset 3, limit 1, 2 cursors, should 0 results, with 0 cursors since we don't have enough data
    searchResponse = searchMetadata(namespace, "*", targets, sort, 3, 1, 2, null);
    Assert.assertTrue(searchResponse.getResults().isEmpty());
    Assert.assertTrue(searchResponse.getCursors().isEmpty());
    // no offset, no limit, should return everything
    searchResponse = searchMetadata(namespace, "*", targets, sort, 0, Integer.MAX_VALUE, 4, null);
    expectedResults = ImmutableList.of(new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view));
    Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
    Assert.assertTrue(searchResponse.getCursors().isEmpty());
    // cleanup
    namespaceClient.delete(namespace);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) ViewSpecification(co.cask.cdap.proto.ViewSpecification) MetadataSearchResponse(co.cask.cdap.proto.metadata.MetadataSearchResponse) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration) DatasetId(co.cask.cdap.proto.id.DatasetId) EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) MetadataSearchResultRecord(co.cask.cdap.proto.metadata.MetadataSearchResultRecord) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) StreamViewId(co.cask.cdap.proto.id.StreamViewId) Test(org.junit.Test)

Example 4 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testSearchResultSorting.

@Test
public void testSearchResultSorting() throws Exception {
    NamespaceId namespace = new NamespaceId("sorting");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    StreamId stream = namespace.stream("text");
    DatasetId dataset = namespace.dataset("mydataset");
    StreamViewId view = stream.view("view");
    // create entities so system metadata is annotated
    // also ensure that they are created at least 1 ms apart
    streamClient.create(stream);
    TimeUnit.MILLISECONDS.sleep(1);
    streamViewClient.createOrUpdate(view, new ViewSpecification(new FormatSpecification("csv", null, null)));
    TimeUnit.MILLISECONDS.sleep(1);
    datasetClient.create(dataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
    // search with bad sort param
    EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
    // test ascending order of entity name
    Set<MetadataSearchResultRecord> searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " asc");
    List<MetadataSearchResultRecord> expected = ImmutableList.of(new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view));
    Assert.assertEquals(expected, new ArrayList<>(searchResults));
    // test descending order of entity name
    searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " desc");
    expected = ImmutableList.of(new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(dataset));
    Assert.assertEquals(expected, new ArrayList<>(searchResults));
    // test ascending order of creation time
    searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " asc");
    expected = ImmutableList.of(new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(dataset));
    Assert.assertEquals(expected, new ArrayList<>(searchResults));
    // test descending order of creation time
    searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " desc");
    expected = ImmutableList.of(new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(stream));
    Assert.assertEquals(expected, new ArrayList<>(searchResults));
    // cleanup
    namespaceClient.delete(namespace);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) ViewSpecification(co.cask.cdap.proto.ViewSpecification) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration) DatasetId(co.cask.cdap.proto.id.DatasetId) EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) MetadataSearchResultRecord(co.cask.cdap.proto.metadata.MetadataSearchResultRecord) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) StreamViewId(co.cask.cdap.proto.id.StreamViewId) Test(org.junit.Test)

Example 5 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testInvalidParams.

@Test
public void testInvalidParams() throws Exception {
    NamespaceId namespace = new NamespaceId("testInvalidParams");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    try {
        EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
        searchMetadata(namespace, "text", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " desc");
        Assert.fail("Expected not to be able to specify 'query' and 'sort' parameters.");
    } catch (BadRequestException expected) {
    // expected
    }
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Aggregations

EntityTypeSimpleName (co.cask.cdap.proto.element.EntityTypeSimpleName)9 BadRequestException (co.cask.cdap.common.BadRequestException)7 Test (org.junit.Test)7 NamespaceId (co.cask.cdap.proto.id.NamespaceId)4 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)3 TransactionExecutor (org.apache.tephra.TransactionExecutor)3 TransactionFailureException (org.apache.tephra.TransactionFailureException)3 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)2 DatasetInstanceConfiguration (co.cask.cdap.proto.DatasetInstanceConfiguration)2 ViewSpecification (co.cask.cdap.proto.ViewSpecification)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 StreamId (co.cask.cdap.proto.id.StreamId)2 StreamViewId (co.cask.cdap.proto.id.StreamViewId)2 MetadataSearchResponse (co.cask.cdap.proto.metadata.MetadataSearchResponse)2 MetadataSearchResultRecord (co.cask.cdap.proto.metadata.MetadataSearchResultRecord)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 NotFoundException (co.cask.cdap.common.NotFoundException)1 SortInfo (co.cask.cdap.data2.metadata.dataset.SortInfo)1 Indexer (co.cask.cdap.data2.metadata.indexer.Indexer)1