Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.11 KB | None | 0 0
  1. func CreateAtcfs(host, port, db_name, username, password, compress, debug string, data []AtcfsItem) {
  2.     connect, err := sql.Open("clickhouse", "tcp://"+host+":"+port+"?database="+db_name+"&username="+username+"&password="+password+"&compress="+compress+"&debug="+debug+"\"")
  3.     checkErr(err)
  4.     if err := connect.Ping(); err != nil {
  5.         if exception, ok := err.(*clickhouse.Exception); ok {
  6.             log.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
  7.         } else {
  8.             log.Println(err)
  9.         }
  10.         return
  11.     }
  12.     _, err = connect.Exec(`
  13.         CREATE TABLE IF NOT EXISTS atcfs (
  14.             uniqueid    String,
  15.             atcfsid     String,
  16.             eventDate   Date
  17.         ) engine=MergeTree(eventDate, (eventDate), 8192)
  18.     `)
  19.  
  20.     checkErr(err)
  21.     tx, err := connect.Begin()
  22.     checkErr(err)
  23.     stmt, err := tx.Prepare("INSERT INTO atcfs (uniqueid, atcfsid, eventDate) VALUES (?,?,?)")
  24.     checkErr(err)
  25.     defer stmt.Close()
  26.     for _, value := range data {
  27.         if _, err := stmt.Exec(
  28.             value.Uniqueid,
  29.             value.Atcfsid,
  30.             value.EventDate,
  31.         ); err != nil {
  32.             log.Println(err)
  33.         }
  34.     }
  35.     checkErr(tx.Commit())
  36.     defer connect.Close()
  37.     return
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement