Search in sources :

Example 6 with ViewSpecification

use of co.cask.cdap.proto.ViewSpecification in project cdap by caskdata.

the class StreamViewClientTest method testAll.

@Test
public void testAll() throws Exception {
    NamespaceId namespace = NamespaceId.DEFAULT;
    StreamId stream = namespace.stream("foo");
    StreamViewId view1 = stream.view("view1");
    LOG.info("Creating stream {}", stream);
    streamClient.create(stream);
    try {
        LOG.info("Sending events to stream {}", stream);
        streamClient.sendEvent(stream, "a,b,c");
        streamClient.sendEvent(stream, "d,e,f");
        streamClient.sendEvent(stream, "g,h,i");
        LOG.info("Verifying that no views exist yet");
        Assert.assertEquals(ImmutableList.of(), streamViewClient.list(stream));
        try {
            streamViewClient.get(view1);
            Assert.fail();
        } catch (NotFoundException e) {
            Assert.assertEquals(view1, e.getObject());
        }
        FormatSpecification format = new FormatSpecification("csv", Schema.recordOf("foo", Schema.Field.of("one", Schema.of(Schema.Type.STRING)), Schema.Field.of("two", Schema.of(Schema.Type.STRING)), Schema.Field.of("three", Schema.of(Schema.Type.STRING))));
        ViewSpecification viewSpecification = new ViewSpecification(format, "firsttable");
        LOG.info("Creating view {} with config {}", view1, GSON.toJson(viewSpecification));
        Assert.assertEquals(true, streamViewClient.createOrUpdate(view1, viewSpecification));
        LOG.info("Verifying that view {} has been created", view1);
        Assert.assertEquals(new ViewDetail(view1.getView(), viewSpecification), streamViewClient.get(view1));
        Assert.assertEquals(ImmutableList.of(view1.getView()), streamViewClient.list(stream));
        FormatSpecification newFormat = new FormatSpecification("csv", Schema.recordOf("foo", Schema.Field.of("one", Schema.of(Schema.Type.STRING)), Schema.Field.of("two", Schema.of(Schema.Type.STRING)), Schema.Field.of("three", Schema.of(Schema.Type.STRING))));
        ViewSpecification newViewSpecification = new ViewSpecification(newFormat, "firsttable");
        LOG.info("Updating view {} with config {}", view1, GSON.toJson(newViewSpecification));
        Assert.assertEquals(false, streamViewClient.createOrUpdate(view1, newViewSpecification));
        LOG.info("Verifying that view {} has been updated", view1);
        Assert.assertEquals(new ViewDetail(view1.getView(), newViewSpecification), streamViewClient.get(view1));
        Assert.assertEquals(ImmutableList.of(view1.getView()), streamViewClient.list(stream));
        ExploreExecutionResult executionResult = queryClient.execute(view1.getParent().getParent(), "select one,two,three from firsttable").get();
        Assert.assertNotNull(executionResult.getResultSchema());
        Assert.assertEquals(3, executionResult.getResultSchema().size());
        Assert.assertEquals("one", executionResult.getResultSchema().get(0).getName());
        Assert.assertEquals("two", executionResult.getResultSchema().get(1).getName());
        Assert.assertEquals("three", executionResult.getResultSchema().get(2).getName());
        List<QueryResult> results = Lists.newArrayList(executionResult);
        Assert.assertNotNull(results);
        Assert.assertEquals(3, results.size());
        Assert.assertEquals("a", results.get(0).getColumns().get(0));
        Assert.assertEquals("b", results.get(0).getColumns().get(1));
        Assert.assertEquals("c", results.get(0).getColumns().get(2));
        Assert.assertEquals("d", results.get(1).getColumns().get(0));
        Assert.assertEquals("e", results.get(1).getColumns().get(1));
        Assert.assertEquals("f", results.get(1).getColumns().get(2));
        Assert.assertEquals("g", results.get(2).getColumns().get(0));
        Assert.assertEquals("h", results.get(2).getColumns().get(1));
        Assert.assertEquals("i", results.get(2).getColumns().get(2));
        LOG.info("Deleting view {}", view1);
        streamViewClient.delete(view1);
        LOG.info("Verifying that view {] has been deleted", view1);
        try {
            streamViewClient.get(view1);
            Assert.fail();
        } catch (NotFoundException e) {
            Assert.assertEquals(view1, e.getObject());
        }
        Assert.assertEquals(ImmutableList.of(), streamViewClient.list(stream));
    } finally {
        streamClient.delete(stream);
    }
    // test deleting stream with a view
    LOG.info("Creating stream {}", stream);
    streamClient.create(stream);
    try {
        FormatSpecification format = new FormatSpecification("csv", Schema.recordOf("foo", Schema.Field.of("one", Schema.of(Schema.Type.STRING)), Schema.Field.of("two", Schema.of(Schema.Type.STRING)), Schema.Field.of("three", Schema.of(Schema.Type.STRING))));
        ViewSpecification viewSpecification = new ViewSpecification(format, "firsttable");
        LOG.info("Creating view {} with config {}", view1, GSON.toJson(viewSpecification));
        Assert.assertEquals(true, streamViewClient.createOrUpdate(view1, viewSpecification));
    } finally {
        streamClient.delete(stream);
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) QueryResult(co.cask.cdap.proto.QueryResult) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) NotFoundException(co.cask.cdap.common.NotFoundException) ViewSpecification(co.cask.cdap.proto.ViewSpecification) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) ViewDetail(co.cask.cdap.proto.ViewDetail) StreamViewId(co.cask.cdap.proto.id.StreamViewId) Test(org.junit.Test)

Example 7 with ViewSpecification

use of co.cask.cdap.proto.ViewSpecification in project cdap by caskdata.

the class ViewAdmin method delete.

public void delete(StreamViewId viewId) throws Exception {
    ViewSpecification spec = store.get(viewId);
    explore.disableExploreStream(viewId.getParent(), spec.getTableName());
    store.delete(viewId);
    metadataStore.removeMetadata(viewId);
}
Also used : ViewSpecification(co.cask.cdap.proto.ViewSpecification)

Example 8 with ViewSpecification

use of co.cask.cdap.proto.ViewSpecification in project cdap by caskdata.

the class StreamViewHttpHandler method createOrUpdate.

@PUT
@Path("/streams/{stream}/views/{view}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void createOrUpdate(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("stream") String stream, @PathParam("view") String view) throws Exception {
    StreamViewId viewId;
    try {
        viewId = new StreamViewId(namespace, stream, view);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()))) {
        ViewSpecification spec = GSON.fromJson(reader, ViewSpecification.class);
        if (spec == null) {
            throw new BadRequestException("Missing ViewSpecification in request body");
        }
        boolean created = admin.createOrUpdateView(viewId, spec);
        responder.sendStatus(created ? HttpResponseStatus.CREATED : HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Couldn't decode body as view config JSON");
    } catch (IOException e) {
        LOG.warn("Error closing InputStreamReader", e);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ViewSpecification(co.cask.cdap.proto.ViewSpecification) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) IOException(java.io.IOException) StreamViewId(co.cask.cdap.proto.id.StreamViewId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 9 with ViewSpecification

use of co.cask.cdap.proto.ViewSpecification in project cdap by caskdata.

the class InMemoryViewStore method delete.

@Override
public void delete(StreamViewId viewId) throws NotFoundException {
    ViewSpecification removed;
    Lock lock = viewsLock.writeLock();
    lock.lock();
    try {
        removed = views.remove(viewId, viewId.getParent());
    } finally {
        lock.unlock();
    }
    if (removed == null) {
        throw new NotFoundException(viewId);
    }
}
Also used : ViewSpecification(co.cask.cdap.proto.ViewSpecification) NotFoundException(co.cask.cdap.common.NotFoundException) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 10 with ViewSpecification

use of co.cask.cdap.proto.ViewSpecification in project cdap by caskdata.

the class EntityExistenceTest method setup.

@BeforeClass
public static void setup() throws Exception {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.INSTANCE_NAME, EXISTS);
    Injector injector = AppFabricTestHelper.getInjector(cConf);
    NamespaceStore nsStore = injector.getInstance(NamespaceStore.class);
    ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
    cConf = injector.getInstance(CConfiguration.class);
    nsStore.create(new NamespaceMeta.Builder().setName(EXISTS).build());
    existenceVerifier = injector.getInstance(EntityExistenceVerifier.class);
    LocalLocationFactory lf = new LocalLocationFactory(TEMPORARY_FOLDER.newFolder());
    File artifactFile = new File(AppJarHelper.createDeploymentJar(lf, AllProgramsApp.class).toURI());
    artifactRepository.addArtifact(ARTIFACT.toId(), artifactFile);
    AppFabricTestHelper.deployApplication(NAMESPACE.toId(), AllProgramsApp.class, null, cConf);
    StreamAdmin streamAdmin = injector.getInstance(StreamAdmin.class);
    streamAdmin.createOrUpdateView(VIEW, new ViewSpecification(new FormatSpecification("csv", null)));
}
Also used : StreamAdmin(co.cask.cdap.data2.transaction.stream.StreamAdmin) EntityExistenceVerifier(co.cask.cdap.common.entity.EntityExistenceVerifier) Injector(com.google.inject.Injector) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceStore(co.cask.cdap.store.NamespaceStore) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) ViewSpecification(co.cask.cdap.proto.ViewSpecification) ArtifactRepository(co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository) CConfiguration(co.cask.cdap.common.conf.CConfiguration) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Aggregations

ViewSpecification (co.cask.cdap.proto.ViewSpecification)18 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)14 StreamViewId (co.cask.cdap.proto.id.StreamViewId)12 StreamId (co.cask.cdap.proto.id.StreamId)10 Test (org.junit.Test)9 DatasetId (co.cask.cdap.proto.id.DatasetId)6 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)5 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 MetadataSearchResultRecord (co.cask.cdap.proto.metadata.MetadataSearchResultRecord)5 Schema (co.cask.cdap.api.data.schema.Schema)4 NotFoundException (co.cask.cdap.common.NotFoundException)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ArtifactId (co.cask.cdap.proto.id.ArtifactId)3 WordCountApp (co.cask.cdap.WordCountApp)2 DatasetInstanceConfiguration (co.cask.cdap.proto.DatasetInstanceConfiguration)2 ViewDetail (co.cask.cdap.proto.ViewDetail)2 EntityTypeSimpleName (co.cask.cdap.proto.element.EntityTypeSimpleName)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 Config (co.cask.cdap.api.Config)1 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1