@Override public PubsubMessage getCurrent() throws NoSuchElementException { if (current == null) { throw new NoSuchElementException(); } return new PubsubMessage(current.elementBytes, current.attributes); }
@Override public PubsubMessage apply(String input) { return new PubsubMessage(input.getBytes(StandardCharsets.UTF_8), ImmutableMap.of()); } }
@Override public PubsubMessage apply(T input) { try { return new PubsubMessage(CoderUtils.encodeToByteArray(coder, input), ImmutableMap.of()); } catch (CoderException e) { throw new RuntimeException("Could not decode Pubsub message", e); } } }
@Override public PubsubMessage decode(InputStream inStream, Context context) throws IOException { return new PubsubMessage(PAYLOAD_CODER.decode(inStream, context), ImmutableMap.of()); } }
@Override public PubsubMessage apply(IndexedRecord input) { int size = input.getSchema().getFields().size(); for (int i = 0; i < size; i++) { if (sb.length() != 0) sb.append(fieldDelimiter); sb.append(input.get(i)); } byte[] bytes = sb.toString().getBytes(Charset.forName("UTF-8")); sb.setLength(0); return new PubsubMessage(bytes, ImmutableMap.<String, String> of()); } }
@Override public PubsubMessage decode(InputStream inStream, Context context) throws IOException { byte[] payload = PAYLOAD_CODER.decode(inStream); Map<String, String> attributes = ATTRIBUTES_CODER.decode(inStream, context); return new PubsubMessage(payload, attributes); } }
@Override public PubsubMessage apply(IndexedRecord input) { try { DatumWriter<IndexedRecord> datumWriter = new GenericDatumWriter(input.getSchema()); ByteArrayOutputStream out = new ByteArrayOutputStream(); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); datumWriter.write(input, encoder); encoder.flush(); byte[] result = out.toByteArray(); out.close(); return new PubsubMessage(result, ImmutableMap.<String, String> of()); } catch (IOException e) { throw TalendRuntimeException.createUnexpectedException(e); } } }
private PubsubMessage message(Instant timestamp, String jsonPayload) { return new PubsubMessage( jsonPayload.getBytes(UTF_8), ImmutableMap.of("ts", String.valueOf(timestamp.getMillis()))); }
private PubsubMessage message(Instant timestamp, String jsonPayload) { return new PubsubMessage( jsonPayload.getBytes(UTF_8), ImmutableMap.of("ts", String.valueOf(timestamp.getMillis()))); }
private TimestampedValue<PubsubMessage> message( int timestamp, Map<String, String> attributes, String payload) { return TimestampedValue.of( new PubsubMessage(payload.getBytes(UTF_8), attributes), ts(timestamp)); }
@ProcessElement public void processElement(ProcessContext c) { c.outputWithTimestamp( new PubsubMessage(c.element().getBytes(StandardCharsets.UTF_8), attributes), new Instant(TIMESTAMP)); } }
@ProcessElement public void processElement(ProcessContext context) { int num = 0; Instant endTime = Instant.now().plus(maxTime); while (num < maxNum && Instant.now().isBefore(endTime)) { try { List<String> ackIds = new ArrayList<>(); List<ReceivedMessage> receivedMessages = client.pull(spec.getDatasetProperties().subscription.getValue(), maxNum - num); if (receivedMessages == null) { continue; } for (ReceivedMessage receivedMessage : receivedMessages) { context.output(new PubsubMessage(receivedMessage.getMessage().decodeData(), ImmutableMap.<String, String> of())); ackIds.add(receivedMessage.getAckId()); num++; } if (ack && !ackIds.isEmpty()) { // do not call this when getSample, else the message will be removed client.ack(spec.getDatasetProperties().subscription.getValue(), ackIds); } } catch (IOException e) { throw TalendRuntimeException.createUnexpectedException(e); } } } }