Created
September 26, 2018 12:32
-
-
Save serafdev/18daf5294a14c253634e169a7ea157f7 to your computer and use it in GitHub Desktop.
BigQuery Type Mappers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Timestamp | |
import java.text.SimpleDateFormat | |
import java.util.Date | |
import java.util.concurrent.TimeUnit | |
import com.google.cloud.bigquery.FieldValueList | |
trait BQResultSet { | |
private[this] def getOpt[R](columnName: String, returnValue: String => R)(implicit row: FieldValueList): Option[R] = | |
if (row.get(columnName).isNull) None else Some(returnValue(columnName)) | |
protected[this] def stringOpt(columnName: String)(implicit row: FieldValueList): Option[String] = | |
getOpt(columnName, string) | |
protected[this] def intOpt(columnName: String)(implicit row: FieldValueList): Option[Int] = | |
getOpt(columnName, int) | |
protected[this] def doubleOpt(columnName: String)(implicit row: FieldValueList): Option[Double] = | |
getOpt(columnName, double) | |
protected[this] def timestampOpt(columnName: String)(implicit row: FieldValueList): Option[Timestamp] = | |
getOpt(columnName, timestamp) | |
protected[this] def string(columnName: String)(implicit row: FieldValueList): String = row.get(columnName).getStringValue | |
protected[this] def int(columnName: String)(implicit row: FieldValueList): Int = row.get(columnName).getLongValue.toInt | |
protected[this] def double(columnName: String)(implicit row: FieldValueList): Double = row.get(columnName).getDoubleValue | |
protected[this] def timestamp(columnName: String)(implicit row: FieldValueList): Timestamp = | |
Timestamp.valueOf( | |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | |
.format( | |
new Date(TimeUnit.MICROSECONDS.toMillis( | |
row.get(columnName).getTimestampValue | |
)) | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment