Search in sources :

Example 76 with Schema

use of com.google.pubsub.v1.Schema in project java-pubsub by googleapis.

the class SubscribeWithAvroSchemaExample method subscribeWithAvroSchemaExample.

public static void subscribeWithAvroSchemaExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    // Prepare a reader for the encoded Avro records.
    SpecificDatumReader<State> reader = new SpecificDatumReader<>(State.getClassSchema());
    // Instantiate an asynchronous message receiver.
    MessageReceiver receiver = (PubsubMessage message, AckReplyConsumer consumer) -> {
        ByteString data = message.getData();
        // Get the schema encoding type.
        String encoding = message.getAttributesMap().get("googclient_schemaencoding");
        // Send the message data to a byte[] input stream.
        InputStream inputStream = new ByteArrayInputStream(data.toByteArray());
        Decoder decoder = null;
        // based on the schema encoding type.
        block: try {
            switch(encoding) {
                case "BINARY":
                    decoder = DecoderFactory.get().directBinaryDecoder(inputStream, /*reuse=*/
                    null);
                    System.out.println("Receiving a binary-encoded message:");
                    break;
                case "JSON":
                    decoder = DecoderFactory.get().jsonDecoder(State.getClassSchema(), inputStream);
                    System.out.println("Receiving a JSON-encoded message:");
                    break;
                default:
                    break block;
            }
            // Obtain an object of the generated Avro class using the decoder.
            State state = reader.read(null, decoder);
            System.out.println(state.getName() + " is abbreviated as " + state.getPostAbbr());
        } catch (IOException e) {
            System.err.println(e);
        }
        // Ack the message.
        consumer.ack();
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
        subscriber.awaitTerminated(30, TimeUnit.SECONDS);
    } catch (TimeoutException timeoutException) {
        subscriber.stopAsync();
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ByteString(com.google.protobuf.ByteString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) PubsubMessage(com.google.pubsub.v1.PubsubMessage) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) ByteArrayInputStream(java.io.ByteArrayInputStream) Subscriber(com.google.cloud.pubsub.v1.Subscriber) State(utilities.State) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) TimeoutException(java.util.concurrent.TimeoutException)

Example 77 with Schema

use of com.google.pubsub.v1.Schema in project java-pubsub by googleapis.

the class CreateTopicWithSchemaExample method main.

public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String topicId = "your-topic-id";
    // Use an existing schema.
    String schemaId = "your-schema-id";
    // Choose either BINARY or JSON message serialization in this topic.
    Encoding encoding = Encoding.BINARY;
    createTopicWithSchemaExample(projectId, topicId, schemaId, encoding);
}
Also used : Encoding(com.google.pubsub.v1.Encoding)

Example 78 with Schema

use of com.google.pubsub.v1.Schema in project java-pubsub by googleapis.

the class DeleteSchemaExample method deleteSchemaExample.

public static void deleteSchemaExample(String projectId, String schemaId) throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        schemaServiceClient.deleteSchema(schemaName);
        System.out.println("Deleted a schema:" + schemaName);
    } catch (NotFoundException e) {
        System.out.println(schemaName + "not found.");
    }
}
Also used : NotFoundException(com.google.api.gax.rpc.NotFoundException) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 79 with Schema

use of com.google.pubsub.v1.Schema in project java-pubsub by googleapis.

the class GetSchemaExample method getSchemaExample.

public static void getSchemaExample(String projectId, String schemaId) throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        Schema schema = schemaServiceClient.getSchema(schemaName);
        System.out.println("Got a schema:\n" + schema);
    } catch (NotFoundException e) {
        System.out.println(schemaName + "not found.");
    }
}
Also used : Schema(com.google.pubsub.v1.Schema) NotFoundException(com.google.api.gax.rpc.NotFoundException) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 80 with Schema

use of com.google.pubsub.v1.Schema in project java-docs-samples by GoogleCloudPlatform.

the class StreamingBeamSql method main.

public static void main(final String[] args) {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    options.setStreaming(true);
    var project = options.as(GcpOptions.class).getProject();
    var subscription = ProjectSubscriptionName.of(project, options.getInputSubscription()).toString();
    var schema = Schema.builder().addStringField("url").addDoubleField("page_score").addDateTimeField("processing_time").build();
    var pipeline = Pipeline.create(options);
    pipeline.apply("Read messages from Pub/Sub", PubsubIO.readStrings().fromSubscription(subscription)).apply("Parse JSON into SQL rows", MapElements.into(TypeDescriptor.of(Row.class)).via(message -> {
        // This is a good place to add error handling.
        // The first transform should act as a validation layer to make sure
        // that any data coming to the processing pipeline must be valid.
        // See `MapElements.MapWithFailures` for more details.
        LOG.info("message: {}", message);
        var msg = GSON.fromJson(message, PageReviewMessage.class);
        return Row.withSchema(schema).addValues(// row url
        msg.url, // row page_score
        msg.review.equals("positive") ? 1.0 : 0.0, // row processing_time
        new Instant()).build();
    })).setRowSchema(// make sure to set the row schema for the PCollection
    schema).apply("Add processing time", WithTimestamps.of((row) -> row.getDateTime("processing_time").toInstant())).apply("Fixed-size windows", Window.into(FixedWindows.of(Duration.standardMinutes(1)))).apply("Run Beam SQL query", SqlTransform.query("SELECT " + "  url, " + "  COUNT(page_score) AS num_reviews, " + "  AVG(page_score) AS score, " + "  MIN(processing_time) AS first_date, " + "  MAX(processing_time) AS last_date " + "FROM PCOLLECTION " + "GROUP BY url")).apply("Convert to BigQuery TableRow", MapElements.into(TypeDescriptor.of(TableRow.class)).via(row -> {
        LOG.info("rating summary: {} {} ({} reviews)", row.getDouble("score"), row.getString("url"), row.getInt64("num_reviews"));
        return new TableRow().set("url", row.getString("url")).set("num_reviews", row.getInt64("num_reviews")).set("score", row.getDouble("score")).set("first_date", row.getDateTime("first_date").toInstant().toString()).set("last_date", row.getDateTime("last_date").toInstant().toString());
    })).apply("Write to BigQuery", BigQueryIO.writeTableRows().to(options.getOutputTable()).withSchema(new TableSchema().setFields(Arrays.asList(// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
    new TableFieldSchema().setName("url").setType("STRING"), new TableFieldSchema().setName("num_reviews").setType("INTEGER"), new TableFieldSchema().setName("score").setType("FLOAT64"), new TableFieldSchema().setName("first_date").setType("TIMESTAMP"), new TableFieldSchema().setName("last_date").setType("TIMESTAMP")))).withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED).withWriteDisposition(WriteDisposition.WRITE_APPEND));
    // For a Dataflow Flex Template, do NOT waitUntilFinish().
    pipeline.run();
}
Also used : Arrays(java.util.Arrays) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) Default(org.apache.beam.sdk.options.Default) Duration(org.joda.time.Duration) LoggerFactory(org.slf4j.LoggerFactory) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) SqlTransform(org.apache.beam.sdk.extensions.sql.SqlTransform) Description(org.apache.beam.sdk.options.Description) DefaultCoder(org.apache.beam.sdk.coders.DefaultCoder) Gson(com.google.gson.Gson) TableRow(com.google.api.services.bigquery.model.TableRow) Window(org.apache.beam.sdk.transforms.windowing.Window) TableSchema(com.google.api.services.bigquery.model.TableSchema) Pipeline(org.apache.beam.sdk.Pipeline) Row(org.apache.beam.sdk.values.Row) MapElements(org.apache.beam.sdk.transforms.MapElements) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) WithTimestamps(org.apache.beam.sdk.transforms.WithTimestamps) Logger(org.slf4j.Logger) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) BigQueryIO(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO) StreamingOptions(org.apache.beam.sdk.options.StreamingOptions) FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) CreateDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.CreateDisposition) AvroCoder(org.apache.beam.sdk.coders.AvroCoder) Schema(org.apache.beam.sdk.schemas.Schema) PubsubIO(org.apache.beam.sdk.io.gcp.pubsub.PubsubIO) WriteDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition) Nullable(org.apache.avro.reflect.Nullable) Instant(org.joda.time.Instant) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Validation(org.apache.beam.sdk.options.Validation) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) StreamingOptions(org.apache.beam.sdk.options.StreamingOptions) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) TableSchema(com.google.api.services.bigquery.model.TableSchema) Instant(org.joda.time.Instant) TableRow(com.google.api.services.bigquery.model.TableRow) TableRow(com.google.api.services.bigquery.model.TableRow) Row(org.apache.beam.sdk.values.Row) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema)

Aggregations

Test (org.junit.Test)65 Schema (com.google.pubsub.v1.Schema)38 Schema (org.molgenis.emx2.Schema)38 ByteString (com.google.protobuf.ByteString)19 SchemaServiceClient (com.google.cloud.pubsub.v1.SchemaServiceClient)18 AbstractMessage (com.google.protobuf.AbstractMessage)18 QName (javax.xml.namespace.QName)16 SchemaName (com.google.pubsub.v1.SchemaName)15 File (java.io.File)15 Schema (org.geosdi.geoplatform.xml.xsd.v2001.Schema)15 ProjectName (com.google.pubsub.v1.ProjectName)14 IOException (java.io.IOException)14 URL (java.net.URL)14 LayerSchemaDTO (org.geosdi.geoplatform.connector.wfs.response.LayerSchemaDTO)14 StringWriter (java.io.StringWriter)13 Schema (org.oasisopen.odata.csdl.v4.Schema)13 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)12 StatusRuntimeException (io.grpc.StatusRuntimeException)12 Schema (com.reprezen.kaizen.oasparser.model3.Schema)11 JAXBElement (javax.xml.bind.JAXBElement)10