|
| 1 | +import scalaj.http.{Http, HttpOptions} |
| 2 | +import org.apache.spark.sql.{SparkSession, Row} |
| 3 | +import org.apache.spark.sql.types._ |
| 4 | +import org.json4s.jackson.Serialization |
| 5 | +import java.time.LocalDateTime |
| 6 | +import java.time.format.DateTimeFormatter |
| 7 | +import java.sql.Timestamp |
| 8 | + |
| 9 | +object SparkCrateDBhttpExample { |
| 10 | + def main(args: Array[String]): Unit = { |
| 11 | + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") |
| 12 | + |
| 13 | + val data = Seq( |
| 14 | + Row(Timestamp.valueOf(LocalDateTime.parse("2024-07-10 10:00", formatter)), Array(9.744417, 47.413417), """{"example_quantity_field": 30, "example_string_field": "abc"}"""), |
| 15 | + Row(Timestamp.valueOf(LocalDateTime.parse("2024-07-10 11:00", formatter)), Array(13.46738, 52.50463), """{"example_quantity_field": 40, "example_string_field": "def"}""") |
| 16 | + ) |
| 17 | + |
| 18 | + val spark = SparkSession.builder |
| 19 | + .appName("test") |
| 20 | + .master("local[*]") |
| 21 | + .getOrCreate() |
| 22 | + val df = spark.createDataFrame( |
| 23 | + spark.sparkContext.parallelize(data), |
| 24 | + StructType( |
| 25 | + List( |
| 26 | + StructField("examplefield1", TimestampType, true), |
| 27 | + StructField("anotherfield", ArrayType(DoubleType), true), |
| 28 | + StructField("jsonpayload", StringType, true) |
| 29 | + ) |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + val url = "http://localhost:4200/_sql" |
| 34 | + |
| 35 | + val columns = df.columns.mkString(", ") |
| 36 | + val placeholders = df.columns.map(_ => "?").mkString(", ") |
| 37 | + val stmt = s"INSERT INTO myschema.mytable ($columns) VALUES ($placeholders)" |
| 38 | + |
| 39 | + val columnNames = df.columns |
| 40 | + df.foreachPartition { partition => |
| 41 | + val bulkArgs: List[List[Any]] = partition.map { row => |
| 42 | + columnNames.indices.map(i => row.get(i)).toList |
| 43 | + }.toList |
| 44 | + |
| 45 | + if (bulkArgs.nonEmpty) { |
| 46 | + val data = Map( |
| 47 | + "stmt" -> stmt, |
| 48 | + "bulk_args" -> bulkArgs |
| 49 | + ) |
| 50 | + |
| 51 | + implicit val formats = org.json4s.DefaultFormats |
| 52 | + val jsonString = Serialization.write(data) |
| 53 | + |
| 54 | + val response = Http(url) |
| 55 | + .postData(jsonString) |
| 56 | + .header("Content-Type", "application/json") |
| 57 | + .asString |
| 58 | + |
| 59 | + println(response) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
0 commit comments