首頁 > 軟體

java操作gaussDB資料庫的實現範例

2022-07-21 18:04:03

本文主要介紹了java操作gaussDB資料庫的實現範例,分享給大家,具體如下:

package com.shiwusuo.ReadHdfsToClickHouse.gauss

import java.sql.{Connection, DriverManager}
import java.util.Properties

import org.apache.spark.sql.DataFrame

import scala.collection.mutable.ArrayBuffer;

object GaussDBUtils {

  val urls = application.gaussURL //資料庫URL
  val username = application.gaussName //使用者名稱
  val password = application.gaussPass //密碼
  val database = application.gaussDatabase
  val gaussCreateDatabase = application.gaussCreateDatabase
  //val driver = "org.postgresql.Driver"
  val driver = application.driver
  var conn: Connection = null;

  def getConnection(): Connection = {
    try {
      Class.forName(driver)
      conn = DriverManager.getConnection(urls, username, password)
      conn
    } catch {
      case e: Throwable => e.printStackTrace()
        println("連線gaussDB失敗===請檢查原因")
        conn
    }
  }

  //建立表
  def createGaussTable(sql: String): Unit = {
    try {
      val connection = getConnection()
      val statement = connection.createStatement()
      statement.executeUpdate(sql)
      statement.close()
    } catch {
      case e: Exception =>
        e.printStackTrace()
    }

  }

  //判斷是否存在該表
  def tableExists(database: String, tablName: String): Boolean = {
    val connection = getConnection()
    val set = connection.getMetaData.getTables(null, database, tablName, null)
    if (set.next()) {
      return true
    } else {
      println("gaussDB中" + tablName + "不存在")
      return false
    }
  }

  //建立資料庫
  def createDatabase(): Unit = {
    val connection = getConnection()
    val statement = connection.createStatement()
    statement.executeUpdate(gaussCreateDatabase)
    statement.close()
  }

  //查詢 、執行sql語句
  def ReadFromGaussDB(sql: String): Unit = {
    try {
      val connection = getConnection()
      val statement = connection.createStatement()
      val set = statement.executeQuery(sql)
      while (set.next()) {
        val str = set.getString(2)
        println(str)
      }
    }catch {
      case e: Exception =>
        e.printStackTrace()
    }
  }

  //獲取gaussDB中所有表名
  def getTablesByGaussDB(db: String) :ArrayBuffer[String]={
    val tablesList = new ArrayBuffer[String]()
    val connection = getConnection()
    val set = connection.getMetaData.getTables(null,db,null,null)
    while (set.next()) {
      val str: String = set.getString("TABLE_NAME")
      tablesList += str
      // println(str)
    }
    tablesList
  }

  //根據resourceId刪除gaussDB資料
  def deleteDataByResourceId(sql:String): Unit ={
      val connection = getConnection()
      val statement = connection.createStatement()
      statement.executeUpdate(sql)
  }



  //插入gaussDB庫中
  def insertgaussDBTable(table: String, df: DataFrame): Unit = {
    val connectionProperties = new Properties()
    connectionProperties.setProperty("user", username)
    connectionProperties.setProperty("password", password)
    println("+++++++開始寫入GaussDB++++++" + table + "共有" + df.count())
    df.write.mode("append").option("batchsize", "50000").option("isolationLevel", "NONE").
      option("numPartitions", "1").jdbc(urls, table, connectionProperties)
    println("=================GaussDB完成寫入========" + table + "====================")
  }


  def main(args: Array[String]): Unit = {
    //val bool = tableExists("gaussdb","test_01087")
    //println(bool)
    //val a = "CREATE TABLE gaussdb.CDPCPn       (n         code bigint NULL,n        ident bigint NULL,n        len bigint NULL,n        lineNum1 bigint NULL,n        lineNum2 bigint NULL,n        lineNum3 bigint NULL,n        lineNum4 bigint NULL,n        lineName varchar(10000000) NULL,n        begTime bigint NULL,n        endTime bigint NULL,n        comDur bigint NULL,n        meanID varchar(10000000) NULL,n        siteID varchar(10000000) NULL,n        unitID varchar(10000000) NULL,n        taskID varchar(10000000) NULL,n        guid bigint NULL,n        storTime bigint NULL,n        mdSecDeg varchar(10000000) NULL,n        fileSecDeg varchar(10000000) NULL,n        secDegPro varchar(10000000) NULL,n        ipVer bigint NULL,n        srcAddr varchar(10000000) NULL,n        dstAddr varchar(10000000) NULL,n        srcPort bigint NULL,n        dstPort bigint NULL,n        protNum bigint NULL,n        srcAddrV6 varchar(10000000) NULL,n        dstAddrV6 varchar(10000000) NULL,n        srcLoc varchar(10000000) NULL,n        dstLoc varchar(10000000) NULL,n        srcISP varchar(10000000) NULL,n        dstISP varchar(10000000) NULL,n        srcAS varchar(10000000) NULL,n        dstAS varchar(10000000) NULL,n        protInfo varchar(10000000) NULL,n        linkInfo varchar(10000000) NULL,n        protType varchar(10000000) NULL,n        protName varchar(10000000) NULL,n        mulRouFlag bigint NULL,n        intFlag bigint NULL,n        strDirec bigint NULL,n        pktNum bigint NULL,n        payLen bigint NULL,n        hashId bigint NULL,n        sessionID varchar(10000000) NULL,n        resourceId varchar(10000000) NULLn       )"
    //createGaussTable(a)
    //createtable
   // ReadFromGaussDB("select * from test_0110.user")
    //val strings = getTablesByGaussDB("test_0301")
    //strings.foreach(println)
    //DELETE FROM test_0110.AOE WHERE resourceId = 518824231216091205
    val b ="DELETE FROM test_0110."ANS" WHERE "resourceId" = '518824231216091205'"
    println(b)
    deleteDataByResourceId(b)
  }
}

到此這篇關於java操作gaussDB資料庫的實現範例的文章就介紹到這了,更多相關java操作gaussDB內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com