如何在复制postgresql中的列上使用postgres函数

如何在复制postgresql中的列上使用postgres函数,postgresql,scala,apache-spark,cartodb,Postgresql,Scala,Apache Spark,Cartodb,我有一份spark(1.2.1v)的工作,它使用将rdd的内容插入postgres。在postgres中,我有一些函数,例如“CDB_LatLng”,需要由插入触发(该特定函数计算CartoDB表的“the_geom”列) 我想使用COPY将数据插入postgres: def copyIn(url: String, username: String, password: String, tableName: String, reader: java.io.Reader, columnStmt:

我有一份spark(1.2.1v)的工作,它使用将rdd的内容插入postgres。在postgres中,我有一些函数,例如“CDB_LatLng”,需要由插入触发(该特定函数计算CartoDB表的“the_geom”列)

我想使用COPY将数据插入postgres:

def copyIn(url: String, username: String, password: String, tableName: String, reader: java.io.Reader, columnStmt: String = "") = {
        //connect to postgres database on the localhost
        val driver = "org.postgresql.Driver"
        var connection:Connection = null
        Class.forName(driver)
        connection = DriverManager.getConnection(url, username, password)

        try {
            connection.unwrap(classOf[PGConnection]).getCopyAPI.copyIn(s"COPY $tableName ($columnStmt) FROM STDIN (FORMAT CSV, DELIMITER ';'))", reader)
        } catch {
            case se: SQLException => println(se.getMessage)
            case t: Throwable => println(t.getMessage)
        } finally {
            connection.close()
        }
}

squares_rdd.foreachPartition(iter => {
        val sb = new StringBuilder()
        var keys : String = ""

        iter.foreach(row => {
            val mapRequest = Utils.getSquaresInsertMap(geoSelectMap, row)
            val mapValues = mapRequest.values.mkString("; ")
            sb.append(mapValues).append("\n")

            if(keys == ""){
                keys = mapRequest.keySet.mkString(", ")
            }

        })

        copyIn(url, username, password, squares_table,  new StringReader(sb.toString), keys)
        sb.clear
  })

当我尝试使用它时,我得到一个错误,列“theu geom”无法接收字符串数据“CDB_LatLng(x,y)”。。。COPY命令应该像中所述的INSERT命令一样触发触发器,因此是否可以在副本中的列上使用函数以及如何使用?

@zero323-但是如果使用触发器,我将无法从COPY中获得任何好处。。。我可以将触发器与INSERT to一起使用,但作为一个函数它会更快,不是吗?你能复制到一个临时表,然后插入临时表的内容,同时使用函数进行转换吗?你说得对。划掉它。@Bill-你的意思是我应该使用COPY将“CDB_LatLng(x,y)”列作为字符串插入临时表,然后再从临时表插入目标表?我想这会解决我的问题,但它是否比使用函数逐行的常规插入更快?@Bill她可以这样做,但同样,这个过程很耗时,而且没有帮助