jdbc 操作数据库

时间:2022-09-12 15:16:52
    import java.sql.DriverManager
    val driverName = "com.cloudera.impala.jdbc41.Driver"
    val url = "jdbc:impala://host:21050;AuthMech=1;KrbRealm=FEIYUE.COM;KrbHostFQDN=host;KrbServiceName=impala"
    Class.forName(driverName)
    val conn = DriverManager.getConnection(url)
    /*val pst = conn.prepareStatement("select * from impala::mydatabase.kudu_test1")
    pst.executeQuery()*/
    conn.prepareStatement("").setFetchSize()
    val ps = conn.prepareStatement("select * from mydatabase.movieas limit 10")
    ps.setFetchSize(1000)
    val rs = ps.executeQuery()
    while (rs.next()) {
      println(rs.getString(1))
    }
    rs.close()
    ps.close()
    val ps2 = conn.prepareStatement(
      s"""CREATE TABLE mydatabase.kudu_test12
         |(
         |  id BIGINT,
         |  name STRING,
         |  PRIMARY KEY(id)
         |)
         |PARTITION BY HASH PARTITIONS 16
         |STORED AS KUDU;
       """.stripMargin)

    val i = ps2.executeUpdate()

    val ps3 = conn.prepareStatement("insert into mydatabase.kudu_test12 values(?,?)")
    var count = 0
    val batchSize = 100
    for (i <- 0 to 1000) {
      ps3.setInt(1, i)
      ps3.setString(2, s"jj${i}")
      ps3.addBatch()
      count += 1
      if (count % batchSize == 0) {
        ps3.executeBatch()
        count = 0
        ps3.clearBatch()
      }
    }
    if (count > 0) {
      ps3.executeBatch()
    }

    conn.close()