這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
今天有一個從mongodb讀取資料,然後放到sqlserver的工作,當然這等程式必須用go來完成啊。
先準備mongdb的第三方驅動包 http://labix.org/mgo
odbc的第三方驅動包 https://bitbucket.org/miquella/mgodbc
讀取mongodb的資料的條件是根據日期範圍,以及字串條件。上代碼。
package mainimport ( _ "bitbucket.org/miquella/mgodbc" "database/sql" "encoding/json" "fmt" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "os" "runtime" "time")type serverslice struct { Servers []string Sqlconn string Start string End string}type user struct { UserName string "UserName" Password string "Password" Email string "Email" Phone string "Phone" SubTime time.Time "SubTime"}var config serverslicevar worker = runtime.NumCPU()//初始化配置func init() { file, _ := os.Open("config.json") defer file.Close() buf := make([]byte, 2048) n, _ := file.Read(buf) err := json.Unmarshal(buf[:n], &config) if err != nil { panic(err) fmt.Println(err) }}func main() { runtime.GOMAXPROCS(runtime.NumCPU()) var chanUser = make(chan user) // 標記完成 dones := make(chan struct{}, worker) go readmongodb(chanUser) for i := 0; i < worker; i++ { go writesql(chanUser, dones) } awaitForCloseResult(dones) fmt.Println("完成")}//讀取mongodb資料func readmongodb(chanUser chan<- user) { start, _ := time.Parse(layout, config.Start) end, _ := time.Parse(layout, config.End) for _, server := range config.Servers { session, err := mgo.Dial(server) if err != nil { fmt.Println("開啟", server, "失敗") panic(err) } defer session.Close() var regex = bson.RegEx{} regex.Pattern = "^139.*" //查詢條件是 start<=subtime<=end;email=nil;phone 不以139開頭 var query = bson.M{"SubTime": bson.M{"$gte": start, "$lte": end}, "Email": nil, "Phone": bson.M{"$not": regex}} //sqlreader類似 iter := session.DB("db").C("Users").Find(query).Iter() message := user{} for iter.Next(&message) { chanUser <- message } //關閉通道 close(chanUser) }}//寫入sqlserverfunc writesql(chanUser <-chan user, dones chan<- struct{}) { con, err := sql.Open("mgodbc", config.Sqlconn) if err != nil { fmt.Println(err) return } defer con.Close() //con.Exec(prepareSQL) for message := range chanUser { //組裝sql var sql = fmt.Sprintf(insertSql, message.UserName, message.Password, message.Email, message.Phone, message.SubTime.Format(layout)) _, err = con.Exec(sql) if err != nil { fmt.Println(err) } } dones <- struct{}{}}/*等待操作完成*/func awaitForCloseResult(dones <-chan struct{}) { for { <-dones worker-- if worker <= 0 { return } }}var layout = "2006-01-02 15:04:05"var insertSql string = `INSERT INTO [dbo].[User] ([UserName] ,[Password] ,[Email] ,[Phone] ,[SubTime] ) VALUES ('%s','%s','%s','%s','%s'); `
對應的設定檔config.json
{ "servers": [ "127.0.0.1:27017" ], "sqlconn":"driver={SQL Server};SERVER=localhost;UID=aa;PWD=bbb;DATABASE=auth", "start":"2013-05-17 00:00:00", "end":"2013-06-01 00:00:00"}