.putAllAttributes(getDynamicAttributesMap(context, flowFile)) .build();
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { try { PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(ByteString.copyFrom(this.objectMapper.writeValueAsBytes(payload))); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); } catch (JsonProcessingException ex) { throw new PubSubMessageConversionException("JSON serialization of an object of type " + payload.getClass().getName() + " failed.", ex); } }
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { try { PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(ByteString.copyFrom(this.objectMapper.writeValueAsBytes(payload))); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); } catch (JsonProcessingException ex) { throw new PubSubMessageConversionException("JSON serialization of an object of type " + payload.getClass().getName() + " failed.", ex); } }
.setData(ByteString.copyFrom(DATA.getBytes(StandardCharsets.UTF_8))) .setPublishTime(timestamp) .putAllAttributes(ATTRIBUTES) .putAllAttributes( ImmutableMap.of( TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID))
PubsubMessage.newBuilder() .setData(ByteString.copyFrom(DATA.getBytes(StandardCharsets.UTF_8))) .putAllAttributes(ATTRIBUTES) .putAllAttributes( ImmutableMap.of( TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID))
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { ByteString convertedPayload; if (payload instanceof ByteString) { convertedPayload = (ByteString) payload; } else if (payload instanceof String) { convertedPayload = ByteString.copyFrom(((String) payload).getBytes(this.charset)); } else if (payload instanceof ByteBuffer) { convertedPayload = ByteString.copyFrom((ByteBuffer) payload); } else if (payload instanceof byte[]) { convertedPayload = ByteString.copyFrom((byte[]) payload); } else { throw new PubSubMessageConversionException("Unable to convert payload of type " + payload.getClass().getName() + " to byte[] for sending to Pub/Sub."); } PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(convertedPayload); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); }
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { ByteString convertedPayload; if (payload instanceof ByteString) { convertedPayload = (ByteString) payload; } else if (payload instanceof String) { convertedPayload = ByteString.copyFrom(((String) payload).getBytes(this.charset)); } else if (payload instanceof ByteBuffer) { convertedPayload = ByteString.copyFrom((ByteBuffer) payload); } else if (payload instanceof byte[]) { convertedPayload = ByteString.copyFrom((byte[]) payload); } else { throw new PubSubMessageConversionException("Unable to convert payload of type " + payload.getClass().getName() + " to byte[] for sending to Pub/Sub."); } PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(convertedPayload); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); }
@Override public void put(Collection<SinkRecord> sinkRecords) { log.debug("Received " + sinkRecords.size() + " messages to send to CPS."); PubsubMessage.Builder builder = PubsubMessage.newBuilder(); for (SinkRecord record : sinkRecords) { log.trace("Received record: " + record.toString()); Map<String, String> attributes = new HashMap<>(); ByteString value = handleValue(record.valueSchema(), record.value(), attributes); if (record.key() != null) { String key = record.key().toString(); attributes.put(ConnectorUtils.CPS_MESSAGE_KEY_ATTRIBUTE, key); } if (includeMetadata) { attributes.put(ConnectorUtils.KAFKA_TOPIC_ATTRIBUTE, record.topic()); attributes.put( ConnectorUtils.KAFKA_PARTITION_ATTRIBUTE, record.kafkaPartition().toString()); attributes.put(ConnectorUtils.KAFKA_OFFSET_ATTRIBUTE, Long.toString(record.kafkaOffset())); attributes.put(ConnectorUtils.KAFKA_TIMESTAMP_ATTRIBUTE, record.timestamp().toString()); } PubsubMessage message = builder.setData(value).putAllAttributes(attributes).build(); publishMessage(record.topic(), record.kafkaPartition(), message); } }
@Override public int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages) throws IOException { PublishRequest.Builder request = PublishRequest.newBuilder().setTopic(topic.getPath()); for (OutgoingMessage outgoingMessage : outgoingMessages) { PubsubMessage.Builder message = PubsubMessage.newBuilder().setData(ByteString.copyFrom(outgoingMessage.elementBytes)); if (outgoingMessage.attributes != null) { message.putAllAttributes(outgoingMessage.attributes); } if (timestampAttribute != null) { message .getMutableAttributes() .put(timestampAttribute, String.valueOf(outgoingMessage.timestampMsSinceEpoch)); } if (idAttribute != null && !Strings.isNullOrEmpty(outgoingMessage.recordId)) { message.getMutableAttributes().put(idAttribute, outgoingMessage.recordId); } request.addMessages(message); } PublishResponse response = publisherStub().publish(request.build()); return response.getMessageIdsCount(); }
public void publish(String jsonPayload, Map<String, String> attributes) { PubsubMessage message = PubsubMessage.newBuilder() .setData(ByteString.copyFromUtf8(jsonPayload)) .putAllAttributes(attributes) .build(); log.debug("Publishing message on Google Pubsub topic {}", this.getFullTopicName()); ApiFuture<String> future = publisher.publish(message); ApiFutures.addCallback(future, new PublishCallback(this.getFullTopicName())); }