Search code examples
javascalaserializationgoogle-bigquerydeserialization

How to serialise/deserialise an object in Scala?


I am new to Scala/Java, and I need to see (and then maybe modify as per required) the transport options of default BQ instance

This is the code so far which seems to be printing the object pointer, instead of the value.

transportOptions is of class "TransportOptions" which extends "Serializable"

val bqservice = BigQueryOptions.getDefaultInstance
val transportOptions = bqservice.getTransportOptions
println(transportOptions)

Output :

com.google.cloud.spark.bigquery.repackaged.com.google.cloud.http.HttpTransportOptions@d3e83eb0

How to check the values inside this object? Thanks!


Solution

  • This is the code so far which seems to be printing the object pointer, instead of the value.

    In Scala (like Java) there are no pointers (unless you're using Scala Native), just values of reference types are the references.

    Is Java "pass-by-reference" or "pass-by-value"?

    The println output means that .toString wasn't overridden and produces class name and instance hash code.

    transportOptions has type com.google.cloud.TransportOptions. It is an interface and is implemented by class com.google.cloud.http.HttpTransportOptions.

    For example you can find out field values with reflection

    // val bqservice = BigQueryOptions.getDefaultInstance
    val bqservice = BigQueryOptions.newBuilder()
      .setProjectId("XXX") // (*)
      .build
    val transportOptions = bqservice.getTransportOptions
    
    classOf[TransportOptions].getFields
      .foreach(println)
    
    classOf[TransportOptions].getDeclaredFields
      .foreach(println)
    
    classOf[HttpTransportOptions].getFields
      .map(field => field.getName -> field.get(transportOptions))
      .foreach(println)
    
    classOf[HttpTransportOptions].getDeclaredFields
      .map(field => {
        field.setAccessible(true)
        field.getName -> field.get(transportOptions)
       })
      .foreach(println)
    //(serialVersionUID,7890117765045419810)
    //(connectTimeout,-1)
    //(readTimeout,-1)
    //(httpTransportFactoryClassName,com.google.cloud.http.HttpTransportOptions$DefaultHttpTransportFactory)
    //(httpTransportFactory,com.google.cloud.http.HttpTransportOptions$DefaultHttpTransportFactory@6e0dec4a)
    

    You can repeat with httpTransportFactory.

    getFields returns fields of current class and its parents but only public ones, getDeclaredFields returns fields of only current class but both public and private.

    (*) How to fix error: A project ID is required for this service but could not be determined