golang 讀取mongob資料寫入sqlserver

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

  今天有一個從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"}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.