Search in sources :

Example 56 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class StreamBatchSource method prepareRun.

@Override
public void prepareRun(BatchSourceContext context) {
    long duration = ETLUtils.parseDuration(streamBatchConfig.duration);
    long delay = Strings.isNullOrEmpty(streamBatchConfig.delay) ? 0 : ETLUtils.parseDuration(streamBatchConfig.delay);
    long endTime = context.getLogicalStartTime() - delay;
    long startTime = endTime - duration;
    LOG.info("Setting input to Stream : {}", streamBatchConfig.name);
    FormatSpecification formatSpec = streamBatchConfig.getFormatSpec();
    Input stream;
    if (formatSpec == null) {
        stream = Input.ofStream(streamBatchConfig.name, startTime, endTime);
    } else {
        stream = Input.ofStream(streamBatchConfig.name, startTime, endTime, formatSpec);
    }
    context.setInput(stream);
}
Also used : Input(co.cask.cdap.api.data.batch.Input) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification)

Example 57 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class CreateOrUpdateStreamViewCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    StreamId streamId = cliConfig.getCurrentNamespace().stream(arguments.get(ArgumentName.STREAM.toString()));
    StreamViewId viewId = streamId.view(arguments.get(ArgumentName.VIEW.toString()));
    String formatName = arguments.get(ArgumentName.FORMAT.toString());
    Schema schema = getSchema(arguments);
    Map<String, String> settings = Collections.emptyMap();
    if (arguments.hasArgument(ArgumentName.SETTINGS.toString())) {
        settings = ArgumentParser.parseMap(arguments.get(ArgumentName.SETTINGS.toString()), ArgumentName.SETTINGS.toString());
    }
    FormatSpecification formatSpecification = new FormatSpecification(formatName, schema, settings);
    ViewSpecification viewSpecification = new ViewSpecification(formatSpecification);
    boolean created = client.createOrUpdate(viewId, viewSpecification);
    if (created) {
        output.printf("Successfully created stream-view '%s'\n", viewId.getEntityName());
    } else {
        output.printf("Successfully updated stream-view '%s'\n", viewId.getEntityName());
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) Schema(co.cask.cdap.api.data.schema.Schema) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) ViewSpecification(co.cask.cdap.proto.ViewSpecification) StreamViewId(co.cask.cdap.proto.id.StreamViewId)

Example 58 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class StreamViewHttpHandlerTest method testAll.

@Test
public void testAll() throws Exception {
    execute(404, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build());
    execute(404, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views/view1")).build());
    execute(404, HttpRequest.delete(resolve("/v3/namespaces/default/streams/foo/views/view1")).build());
    execute(200, HttpRequest.put(resolve("/v3/namespaces/default/streams/foo")).build());
    execute(404, HttpRequest.delete(resolve("/v3/namespaces/default/streams/foo/views/nonexistent")).build());
    List<String> views = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build(), new TypeToken<List<String>>() {
    }.getType());
    Assert.assertEquals(ImmutableList.of(), views);
    Schema schema = Schema.recordOf("foo", Schema.Field.of("name", Schema.of(Schema.Type.STRING)));
    FormatSpecification formatSpec = new FormatSpecification(Formats.AVRO, schema, Collections.<String, String>emptyMap());
    ViewSpecification config = new ViewSpecification(formatSpec);
    // trying to create without request body should give 400
    execute(400, HttpRequest.put(resolve("/v3/namespaces/default/streams/foo/views/view1")).build());
    execute(201, HttpRequest.put(resolve("/v3/namespaces/default/streams/foo/views/view1")).withBody(GSON.toJson(config)).build());
    ViewDetail actualDetail = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views/view1")).build(), ViewDetail.class);
    Assert.assertEquals(new ViewDetail("view1", new ViewSpecification(config.getFormat(), "stream_foo_view1")), actualDetail);
    views = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build(), new TypeToken<List<String>>() {
    }.getType());
    Assert.assertEquals(ImmutableList.of("view1"), views);
    execute(201, HttpRequest.put(resolve("/v3/namespaces/default/streams/foo/views/view2")).withBody(GSON.toJson(config)).build());
    actualDetail = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views/view2")).build(), ViewDetail.class);
    Assert.assertEquals(new ViewDetail("view2", new ViewSpecification(config.getFormat(), "stream_foo_view2")), actualDetail);
    views = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build(), new TypeToken<List<String>>() {
    }.getType());
    Assert.assertEquals(ImmutableList.of("view1", "view2"), views == null ? null : Ordering.natural().sortedCopy(views));
    execute(200, HttpRequest.delete(resolve("/v3/namespaces/default/streams/foo/views/view1")).build());
    views = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build(), new TypeToken<List<String>>() {
    }.getType());
    Assert.assertEquals(ImmutableList.of("view2"), views);
    execute(200, HttpRequest.delete(resolve("/v3/namespaces/default/streams/foo/views/view2")).build());
    views = execute(200, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views")).build(), new TypeToken<List<String>>() {
    }.getType());
    Assert.assertEquals(ImmutableList.<String>of(), views);
    // Deleting a stream should also delete the associated views
    execute(200, HttpRequest.delete(resolve("/v3/namespaces/default/streams/foo")).build());
    execute(200, HttpRequest.put(resolve("/v3/namespaces/default/streams/foo")).build());
    execute(404, HttpRequest.get(resolve("/v3/namespaces/default/streams/foo/views/view1")).build());
}
Also used : TypeToken(com.google.common.reflect.TypeToken) Schema(co.cask.cdap.api.data.schema.Schema) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) ViewSpecification(co.cask.cdap.proto.ViewSpecification) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ViewDetail(co.cask.cdap.proto.ViewDetail) Test(org.junit.Test)

Example 59 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class SetStreamFormatCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    StreamId streamId = cliConfig.getCurrentNamespace().stream(arguments.get(ArgumentName.STREAM.toString()));
    StreamProperties currentProperties = streamClient.getConfig(streamId);
    String formatName = arguments.get(ArgumentName.FORMAT.toString());
    Schema schema = getSchema(arguments);
    Map<String, String> settings = Collections.emptyMap();
    if (arguments.hasArgument(ArgumentName.SETTINGS.toString())) {
        settings = ArgumentParser.parseMap(arguments.get(ArgumentName.SETTINGS.toString()), ArgumentName.SETTINGS.toString());
    }
    FormatSpecification formatSpecification = new FormatSpecification(formatName, schema, settings);
    StreamProperties streamProperties = new StreamProperties(currentProperties.getTTL(), formatSpecification, currentProperties.getNotificationThresholdMB(), currentProperties.getDescription());
    streamClient.setStreamProperties(streamId, streamProperties);
    output.printf("Successfully set format of stream '%s'\n", streamId.getEntityName());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) Schema(co.cask.cdap.api.data.schema.Schema) StreamProperties(co.cask.cdap.proto.StreamProperties) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification)

Example 60 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class StreamHandler method getAndValidateConfig.

/**
   * Gets stream properties from the request. If there is request is invalid, a BadRequestException will be thrown.
   */
private StreamProperties getAndValidateConfig(HttpRequest request) throws BadRequestException {
    Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()));
    StreamProperties properties;
    try {
        properties = GSON.fromJson(reader, StreamProperties.class);
    } catch (Exception e) {
        throw new BadRequestException("Invalid stream configuration. Please check that the " + "configuration is a valid JSON Object with a valid schema. Cause: " + e.getMessage());
    }
    // Validate ttl
    Long ttl = properties.getTTL();
    if (ttl != null && ttl < 0) {
        throw new BadRequestException("Invalid TTL " + ttl + ". TTL value should be positive.");
    }
    // Validate format
    FormatSpecification formatSpec = properties.getFormat();
    if (formatSpec != null) {
        String formatName = formatSpec.getName();
        if (formatName == null) {
            throw new BadRequestException("A format name must be specified.");
        }
        try {
            // if a format is given, make sure it is a valid format,
            // check that we can instantiate the format class
            RecordFormat<?, ?> format = RecordFormats.createInitializedFormat(formatSpec);
            // the request may contain a null schema, in which case the default schema of the format should be used.
            // create a new specification object that is guaranteed to have a non-null schema.
            formatSpec = new FormatSpecification(formatSpec.getName(), format.getSchema(), formatSpec.getSettings());
        } catch (UnsupportedTypeException e) {
            throw new BadRequestException("Format " + formatName + " does not support the requested schema.");
        } catch (Exception e) {
            throw new BadRequestException("Invalid format, unable to instantiate format " + formatName);
        }
    }
    // Validate notification threshold
    Integer threshold = properties.getNotificationThresholdMB();
    if (threshold != null && threshold <= 0) {
        throw new BadRequestException("Invalid threshold " + threshold + ". Threshold value should be greater than zero.");
    }
    // validate owner principal if one is provided
    if (properties.getOwnerPrincipal() != null) {
        SecurityUtil.validateKerberosPrincipal(properties.getOwnerPrincipal());
    }
    return new StreamProperties(ttl, formatSpec, threshold, properties.getDescription(), properties.getOwnerPrincipal());
}
Also used : InputStreamReader(java.io.InputStreamReader) StreamProperties(co.cask.cdap.proto.StreamProperties) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) BadRequestException(co.cask.cdap.common.BadRequestException) JsonParseException(com.google.gson.JsonParseException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException)

Aggregations

FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)61 Test (org.junit.Test)43 Schema (co.cask.cdap.api.data.schema.Schema)32 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)19 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)17 StreamId (co.cask.cdap.proto.id.StreamId)16 ViewSpecification (co.cask.cdap.proto.ViewSpecification)14 StreamProperties (co.cask.cdap.proto.StreamProperties)11 StreamViewId (co.cask.cdap.proto.id.StreamViewId)11 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 IOException (java.io.IOException)5 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)4 NotFoundException (co.cask.cdap.common.NotFoundException)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ArtifactId (co.cask.cdap.proto.id.ArtifactId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3 HttpURLConnection (java.net.HttpURLConnection)3