Skip to content

Instantly share code, notes, and snippets.

@damondouglas
Created March 5, 2024 23:04
Show Gist options
  • Save damondouglas/5883d1b46b377fc94717094369495859 to your computer and use it in GitHub Desktop.
Save damondouglas/5883d1b46b377fc94717094369495859 to your computer and use it in GitHub Desktop.
Example Java POJO based Beam Coder
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
public class SomePojo {
private final String aString;
private final Integer anInteger;
private final Double aDouble;
public SomePojo(String aString, Integer anInteger, Double aDouble) {
this.aString = aString;
this.anInteger = anInteger;
this.aDouble = aDouble;
}
public String getAString() {
return aString;
}
public Integer getAnInteger() {
return anInteger;
}
public Double getADouble() {
return aDouble;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.apache.beam.sdk.coders.CoderException;
import org.apache.beam.sdk.coders.CustomCoder;
import org.apache.beam.sdk.coders.DoubleCoder;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.coders.VarIntCoder;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SomePojoCoder extends CustomCoder<SomePojo> {
private static final StringUtf8Coder STRING_UTF_8_CODER = StringUtf8Coder.of();
private static final VarIntCoder VAR_INT_CODER = VarIntCoder.of();
private static final DoubleCoder DOUBLE_CODER = DoubleCoder.of();
@Override
public void encode(SomePojo value, OutputStream outStream) throws CoderException, IOException {
STRING_UTF_8_CODER.encode(value.getAString(), outStream);
VAR_INT_CODER.encode(value.getAnInteger(), outStream);
DOUBLE_CODER.encode(value.getADouble(), outStream);
}
@Override
public SomePojo decode(InputStream inStream) throws CoderException, IOException {
String aString = STRING_UTF_8_CODER.decode(inStream);
Integer anInteger = VAR_INT_CODER.decode(inStream);
Double aDouble = DOUBLE_CODER.decode(inStream);
return new SomePojo(aString, anInteger, aDouble);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment