Search in sources :

Example 81 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.

the class MetadataDatasetTest method testSearchOnTypes.

@Test
public void testSearchOnTypes() throws Exception {
    MetadataEntity myField1 = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField1").build();
    MetadataEntity myField2 = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField2").build();
    final MetadataEntry myFieldEntry1 = new MetadataEntry(myField1, "testKey1", "testValue1");
    final MetadataEntry myFieldEntry2 = new MetadataEntry(myField2, "testKey2", "testValue2");
    txnl.execute(() -> {
        dataset.addProperty(myField1, "testKey1", "testValue1");
        dataset.addProperty(myField2, "testKey2", "testValue2");
    });
    // Search for it based on value
    txnl.execute(() -> {
        List<MetadataEntry> results = searchByDefaultIndex("default", "field:myField1", ALL_TYPES);
        Assert.assertEquals(ImmutableList.of(myFieldEntry1), results);
        // should return both fields
        results = searchByDefaultIndex("default", "field:myFie*", ALL_TYPES);
        Assert.assertEquals(ImmutableList.of(myFieldEntry1, myFieldEntry2), results);
        results = searchByDefaultIndex("default", "field*", ALL_TYPES);
        Assert.assertEquals(ImmutableList.of(myFieldEntry1, myFieldEntry2), results);
    });
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Test(org.junit.Test)

Example 82 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.

the class MetadataDatasetTest method testCrossNamespaceDefaultSearch.

@Test
public void testCrossNamespaceDefaultSearch() throws Exception {
    MetadataEntity ns1App = new NamespaceId("ns1").app("a").toMetadataEntity();
    MetadataEntity ns2App = new NamespaceId("ns2").app("a").toMetadataEntity();
    txnl.execute(() -> {
        dataset.addProperty(ns1App, "k1", "v1");
        dataset.addProperty(ns1App, "k2", "v2");
        dataset.addProperty(ns2App, "k1", "v1");
    });
    SearchRequest request1 = new SearchRequest(null, "v1", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
    SearchResults results = txnl.execute(() -> dataset.search(request1));
    Set<MetadataEntry> actual = new HashSet<>(results.getResults());
    Set<MetadataEntry> expected = new HashSet<>();
    expected.add(new MetadataEntry(ns1App, "k1", "v1"));
    expected.add(new MetadataEntry(ns2App, "k1", "v1"));
    Assert.assertEquals(expected, actual);
    SearchRequest request2 = new SearchRequest(null, "v2", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
    results = txnl.execute(() -> dataset.search(request2));
    Assert.assertEquals(Collections.singletonList(new MetadataEntry(ns1App, "k2", "v2")), results.getResults());
    SearchRequest star = new SearchRequest(null, "*", ALL_TYPES, SortInfo.DEFAULT, 0, 10, 0, null, false, EnumSet.allOf(EntityScope.class));
    results = txnl.execute(() -> dataset.search(star));
    expected.add(new MetadataEntry(ns1App, "k2", "v2"));
    Assert.assertEquals(expected, new HashSet<>(results.getResults()));
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) EntityScope(io.cdap.cdap.proto.EntityScope) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.

the class MetadataKeyTest method testVersionedEntitiesKey.

@Test
public void testVersionedEntitiesKey() {
    // CDAP-13597 Metadata for versioned entity is version independent i.e if there are two application version v1
    // and v2 and a tag 'tag1' is added to either one it will be be reflected to both as we don't store the
    // application/schedule/programs with it's version. Following tests test that for such versioned entity the keys
    // are the same i.e default version
    // Key for versioned application/schedule/program should be the same
    // application
    // default version
    ApplicationId applicationId1 = new ApplicationId("ns", "app");
    // custom version
    ApplicationId applicationId2 = new ApplicationId("ns", "app", "2");
    // non-version Application metadata entity
    MDSKey mdsValueKey = MetadataKey.createValueRowKey(applicationId1.toMetadataEntity(), "key1");
    MetadataEntity actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(applicationId1.toMetadataEntity(), actual);
    mdsValueKey = MetadataKey.createValueRowKey(applicationId2.toMetadataEntity(), "key1");
    actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(applicationId1.toMetadataEntity(), actual);
    // program
    // default version
    ProgramId programId1 = new ApplicationId("ns", "app").program(ProgramType.SERVICE, "s");
    // custom version
    ProgramId programId2 = new ApplicationId("ns", "app", "2").program(ProgramType.SERVICE, "s");
    mdsValueKey = MetadataKey.createValueRowKey(programId1.toMetadataEntity(), "key1");
    actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(programId1.toMetadataEntity(), actual);
    mdsValueKey = MetadataKey.createValueRowKey(programId2.toMetadataEntity(), "key1");
    actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(programId1.toMetadataEntity(), actual);
    // schedule
    // default version
    ScheduleId scheduleId1 = new ApplicationId("ns", "app").schedule("s");
    // custom version
    ScheduleId scheduleId2 = new ApplicationId("ns", "app", "2").schedule("s");
    mdsValueKey = MetadataKey.createValueRowKey(scheduleId1.toMetadataEntity(), "key1");
    actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(scheduleId1.toMetadataEntity(), actual);
    mdsValueKey = MetadataKey.createValueRowKey(scheduleId2.toMetadataEntity(), "key1");
    actual = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(scheduleId1.toMetadataEntity(), actual);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Test(org.junit.Test)

Example 84 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.

the class MetadataKeyTest method testGetMetadataEntityFromKey.

@Test
public void testGetMetadataEntityFromKey() {
    ApplicationId expectedAppId = new ApplicationId("ns1", "app1");
    MDSKey mdsValueKey = MetadataKey.createValueRowKey(expectedAppId.toMetadataEntity(), "key1");
    MDSKey mdsIndexKey = MetadataKey.createIndexRowKey(expectedAppId.toMetadataEntity(), "key1", "value1");
    // check that we can get MetadataEntity from value and index key
    MetadataEntity actualAppId = MetadataKey.extractMetadataEntityFromKey(mdsValueKey.getKey());
    Assert.assertEquals(expectedAppId.toMetadataEntity(), actualAppId);
    actualAppId = MetadataKey.extractMetadataEntityFromKey(mdsIndexKey.getKey());
    Assert.assertEquals(expectedAppId.toMetadataEntity(), actualAppId);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 85 with MetadataEntity

use of io.cdap.cdap.api.metadata.MetadataEntity in project cdap by cdapio.

the class MetadataTest method testToEntityId.

@Test
public void testToEntityId() {
    // should be able to get get an EntityId if Metadata belong to a cdap entity id
    DatasetId myDs = NamespaceId.DEFAULT.dataset("myDs");
    MetadataDataset.Record metadata1 = new MetadataDataset.Record(myDs);
    Assert.assertEquals(myDs, metadata1.getEntityId());
    MetadataEntity metadataEntity = MetadataEntity.builder(MetadataEntity.ofDataset(NamespaceId.DEFAULT.getEntityName(), "myDs")).appendAsType("field", "myField").build();
    MetadataDataset.Record metadata2 = new MetadataDataset.Record(metadataEntity);
    try {
        metadata2.getEntityId();
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Aggregations

MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)192 Test (org.junit.Test)114 Drop (io.cdap.cdap.spi.metadata.MetadataMutation.Drop)58 Update (io.cdap.cdap.spi.metadata.MetadataMutation.Update)56 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)34 HashMap (java.util.HashMap)30 HashSet (java.util.HashSet)30 Map (java.util.Map)28 ImmutableMap (com.google.common.collect.ImmutableMap)26 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 Remove (io.cdap.cdap.spi.metadata.MetadataMutation.Remove)24 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)22 List (java.util.List)22 Metadata (io.cdap.cdap.spi.metadata.Metadata)20 Create (io.cdap.cdap.spi.metadata.MetadataMutation.Create)20 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)20 Nullable (javax.annotation.Nullable)20 Path (javax.ws.rs.Path)20