/** Converts the provided {@link PipelineOptions} into Json{@link String}. */ public static String toJson(PipelineOptions options) { try { return JsonFormat.printer().print(toProto(options)); } catch (InvalidProtocolBufferException e) { throw new RuntimeException("Failed to convert PipelineOptions to JSON", e); } } }
/** Converts the provided Json{@link String} into {@link PipelineOptions}. */ public static PipelineOptions fromJson(String optionsJson) { try { Map<String, Object> probingOptionsMap = MAPPER.readValue(optionsJson, new TypeReference<Map<String, Object>>() {}); if (probingOptionsMap.containsKey("options")) { //Legacy options. return MAPPER.readValue(optionsJson, PipelineOptions.class); } else { // Fn Options with namespace and version. Struct.Builder builder = Struct.newBuilder(); JsonFormat.parser().merge(optionsJson, builder); return fromProto(builder.build()); } } catch (IOException e) { throw new RuntimeException("Failed to read PipelineOptions from JSON", e); } }
FixedWindowsPayload fixedParams = FixedWindowsPayload.parseFrom(windowFnSpec.getSpec().getPayload()); return FixedWindows.of(Duration.millis(Durations.toMillis(fixedParams.getSize()))) .withOffset(Duration.millis(Timestamps.toMillis(fixedParams.getOffset()))); } else if (s.equals(getUrn(SlidingWindowsPayload.Enum.PROPERTIES))) { SlidingWindowsPayload slidingParams = SlidingWindowsPayload.parseFrom(windowFnSpec.getSpec().getPayload()); return SlidingWindows.of(Duration.millis(Durations.toMillis(slidingParams.getSize()))) .every(Duration.millis(Durations.toMillis(slidingParams.getPeriod()))) .withOffset(Duration.millis(Timestamps.toMillis(slidingParams.getOffset()))); } else if (s.equals(getUrn(SessionsPayload.Enum.PROPERTIES))) { SessionsPayload sessionParams = SessionsPayload.parseFrom(windowFnSpec.getSpec().getPayload()); return Sessions.withGapDuration( Duration.millis(Durations.toMillis(sessionParams.getGapSize()))); } else if (s.equals(SERIALIZED_JAVA_WINDOWFN_URN)) { return (WindowFn<?, ?>)
FixedWindowsPayload fixedWindowsPayload = FixedWindowsPayload.newBuilder() .setSize(Durations.fromMillis(((FixedWindows) windowFn).getSize().getMillis())) .setOffset(Timestamps.fromMillis(((FixedWindows) windowFn).getOffset().getMillis())) .build(); return SdkFunctionSpec.newBuilder() SlidingWindowsPayload slidingWindowsPayload = SlidingWindowsPayload.newBuilder() .setSize(Durations.fromMillis(((SlidingWindows) windowFn).getSize().getMillis())) .setOffset(Timestamps.fromMillis(((SlidingWindows) windowFn).getOffset().getMillis())) .setPeriod(Durations.fromMillis(((SlidingWindows) windowFn).getPeriod().getMillis())) .build(); return SdkFunctionSpec.newBuilder() SessionsPayload sessionsPayload = SessionsPayload.newBuilder() .setGapSize(Durations.fromMillis(((Sessions) windowFn).getGapDuration().getMillis())) .build(); return SdkFunctionSpec.newBuilder()
/** Converts the provided {@link Struct} into {@link PipelineOptions}. */ public static PipelineOptions fromProto(Struct protoOptions) { try { Map<String, TreeNode> mapWithoutUrns = new HashMap<>(); TreeNode rootOptions = MAPPER.readTree(JsonFormat.printer().print(protoOptions)); Iterator<String> optionsKeys = rootOptions.fieldNames(); while (optionsKeys.hasNext()) { String optionKey = optionsKeys.next(); TreeNode optionValue = rootOptions.get(optionKey); mapWithoutUrns.put( CaseFormat.LOWER_UNDERSCORE.to( CaseFormat.LOWER_CAMEL, optionKey.substring("beam:option:".length(), optionKey.length() - ":v1".length())), optionValue); } return MAPPER.readValue( MAPPER.writeValueAsString(ImmutableMap.of("options", mapWithoutUrns)), PipelineOptions.class); } catch (IOException e) { throw new RuntimeException("Failed to read PipelineOptions from Protocol", e); } }
/** Converts the provided {@link PipelineOptions} to a {@link Struct}. */ public static Struct toProto(PipelineOptions options) { Struct.Builder builder = Struct.newBuilder(); try { // TODO: Officially define URNs for options and their scheme. TreeNode treeNode = MAPPER.valueToTree(options); TreeNode rootOptions = treeNode.get("options"); Iterator<String> optionsKeys = rootOptions.fieldNames(); Map<String, TreeNode> optionsUsingUrns = new HashMap<>(); while (optionsKeys.hasNext()) { String optionKey = optionsKeys.next(); TreeNode optionValue = rootOptions.get(optionKey); optionsUsingUrns.put( "beam:option:" + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, optionKey) + ":v1", optionValue); } // The JSON format of a Protobuf Struct is the JSON object that is equivalent to that struct // (with values encoded in a standard json-codeable manner). See Beam PR 3719 for more. JsonFormat.parser().merge(MAPPER.writeValueAsString(optionsUsingUrns), builder); return builder.build(); } catch (IOException e) { throw new RuntimeException("Failed to convert PipelineOptions to Protocol", e); } }