-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
46 lines (38 loc) · 1.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"database/sql"
"log"
"os"
"github.com/aliyun/aliyun-odps-go-sdk/odps/data"
"github.com/aliyun/aliyun-odps-go-sdk/sqldriver"
)
func main() {
config, err := sqldriver.NewConfigFromIni(os.Args[1])
if err != nil {
log.Fatalf("%+v", err)
}
dsn := config.FormatDsn()
// or dsn := "http://<accessId>:<accessKey>@<endpoint>?project=<project>"
db, err := sql.Open("odps", dsn)
if err != nil {
log.Fatalf("%+v", err)
}
insertSql := "insert into user_test partition (age=20, hometown='hangzhou') values (" +
"@name, @score, @birthday, @extra);"
birthday, _ := data.NewDateTime("2010-11-11 15:20:00")
addresses := data.NewArray()
_ = addresses.Append("apsaras", "efc")
extra := data.NewStruct()
_ = extra.SetField("address", addresses)
_ = extra.SetField("hobby", "swimming")
_, err = db.Exec(
insertSql,
sql.Named("name", "'xiaoming'"),
sql.Named("score", 99),
sql.Named("birthday", birthday.Sql()), // datetime'2010-11-11 15:20:00'
sql.Named("extra", extra.Sql()), // named_struct('address', array('apsaras', 'efc'), 'hobby', 'swimming')
)
if err != nil {
log.Fatalf("%+v", err)
}
}