com.spotify.scio.coders.JavaCoders.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scio-coders_2.11 Show documentation
Show all versions of scio-coders_2.11 Show documentation
Scio add-on for static Coder derivation
The newest version!
/*
* Copyright 2016 Spotify AB.
*
* Licensed 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 com.spotify.scio.coders
import java.math.{BigDecimal, BigInteger}
import com.google.api.services.bigquery.model.TableRow
import org.apache.beam.sdk.{coders => bcoders}
import org.apache.beam.sdk.values.KV
import org.apache.beam.sdk.io.gcp.pubsub.{PubsubMessage, PubsubMessageWithAttributesCoder}
import org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder
import org.apache.beam.sdk.transforms.windowing.{BoundedWindow, IntervalWindow, PaneInfo}
import org.apache.beam.sdk.coders.{Coder => _, _}
//
// Java Coders
//
trait JavaCoders {
implicit def uriCoder: Coder[java.net.URI] =
Coder.xmap(Coder.beam(StringUtf8Coder.of()))(s => new java.net.URI(s), _.toString)
implicit def pathCoder: Coder[java.nio.file.Path] =
Coder.xmap(Coder.beam(StringUtf8Coder.of()))(s => java.nio.file.Paths.get(s), _.toString)
import java.lang.{Iterable => jIterable}
implicit def jIterableCoder[T](implicit c: Coder[T]): Coder[jIterable[T]] =
Coder.transform(c) { bc =>
Coder.beam(bcoders.IterableCoder.of(bc))
}
implicit def jlistCoder[T](implicit c: Coder[T]): Coder[java.util.List[T]] =
Coder.transform(c) { bc =>
Coder.beam(bcoders.ListCoder.of(bc))
}
implicit def jMapCoder[K, V](implicit ck: Coder[K], cv: Coder[V]): Coder[java.util.Map[K, V]] =
Coder.transform(ck) { bk =>
Coder.transform(cv) { bv =>
Coder.beam(bcoders.MapCoder.of(bk, bv))
}
}
implicit def jBitSetCoder: Coder[java.util.BitSet] = Coder.beam(BitSetCoder.of())
private def fromScalaCoder[J <: java.lang.Number, S <: AnyVal](coder: Coder[S]): Coder[J] =
coder.asInstanceOf[Coder[J]]
implicit val jShortCoder: Coder[java.lang.Short] = fromScalaCoder(Coder.shortCoder)
implicit val jByteCoder: Coder[java.lang.Byte] = fromScalaCoder(Coder.byteCoder)
implicit val jIntegerCoder: Coder[java.lang.Integer] = fromScalaCoder(Coder.intCoder)
implicit val jLongCoder: Coder[java.lang.Long] = fromScalaCoder(Coder.longCoder)
implicit val jFloatCoder: Coder[java.lang.Float] = fromScalaCoder(Coder.floatCoder)
implicit val jDoubleCoder: Coder[java.lang.Double] = fromScalaCoder(Coder.doubleCoder)
implicit def jBigIntegerCoder: Coder[BigInteger] = Coder.beam(BigIntegerCoder.of())
implicit def jBigDecimalCoder: Coder[BigDecimal] = Coder.beam(BigDecimalCoder.of())
implicit def intervalWindowCoder: Coder[IntervalWindow] = Coder.beam(IntervalWindow.getCoder)
implicit def boundedWindowCoder: Coder[BoundedWindow] = Coder.kryo[BoundedWindow]
implicit def serializableCoder: Coder[Serializable] = Coder.kryo[Serializable]
implicit def paneInfoCoder: Coder[PaneInfo] = Coder.beam(PaneInfo.PaneInfoCoder.of())
implicit def tableRowCoder: Coder[TableRow] = Coder.beam(TableRowJsonCoder.of())
implicit def messageCoder: Coder[PubsubMessage] =
Coder.beam(PubsubMessageWithAttributesCoder.of())
implicit def beamKVCoder[K: Coder, V: Coder]: Coder[KV[K, V]] = Coder.kv(Coder[K], Coder[V])
}