I have to create the pipeline to transfer the data from BigQuery and save it as json file. But I got this error. The result from sql query is 30 million records. How to improve this code?
Error:
[error] (run-main-0) java.lang.OutOfMemoryError: Java heap space [error]
object tmp {
private val logger = LoggerFactory.getLogger(this.getClass)
var date = "2023-05-22"
def main(cmdlineArgs: Array[String]){
val (sc, args) = ContextAndArgs(cmdlineArgs)
val file_path = "src/main/scala/thunder/tmp.sql"
val sql_content = Source.fromFile(file_path).mkString
val queryConfig = QueryJobConfiguration.newBuilder(sql_content).build()
val client = BigQueryOptions.getDefaultInstance().getService()
val queryResult = client.query(queryConfig)
var result = queryResult.iterateAll().iterator().asScala.map(_.asScala.map(_.getValue).toArray).toSeq
val json_result = result.map { row =>
val pin_username = row(0).toString
val feature_name = row(1).toString
implicit val formats = DefaultFormats
write(Map(("pin_username"->pin_username),("feature_name" -> feature_name)))
}
sc.parallelize(json_result)
.saveAsTextFile("output", ".json")
sc.close().waitUntilFinish()
}
}