Golang reads mongob data and writes it to sqlserver

Source: Internet
Author: User

Today, we have a job to read data from MongoDB and put it in sqlserver. Of course, this program must be completed using go.

First prepare mongdb Third-Party driver package http://labix.org/mgo

Third-party driver package https://bitbucket.org/miquella/mgodbc for ODBC

The conditions for reading MongoDB data are based on the date range and string conditions. On the code.

  

Package mainimport (_ "plugin" "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 serversl Icevar worker = runtime. numcpu () // initialize the configuration 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) // Mark done dones: = make (Chan struct {}, worker) Go readmongodb (chanuser) for I: = 0; I <worker; I ++ {go writesql (chanuser, dones)} awaitforcloseresult (dones) FMT. println ("finished")} // read MongoDB data 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 ("open", server, "failed") panic (ERR)} defer session. close () var RegEx = bson. regEx {} RegEx. pattern = "^139. * "// the query condition is start <= subtime <= end; email = nil; phone does not start with 139 var query = bson. m {"subtime": bson. m {"$ GTE": start, "$ LTE": end}, "email": nil, "phone": bson. m {"$ not": RegEx} // sqlreader is similar to ITER: = session. dB ("DB "). C ("users "). find (query ). ITER () message: = user {} for ITER. next (& message ){ Chanuser <-message} // close channel close (chanuser) }/// write 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 {// assemble 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 {}}/ * Wait for the operation to be completed */func awaitforcloseresult (dones <-chan struct {}) {for {<-dones worker -- if worker <= 0 {return }} var layout = "15:04:05" Var insertsql string = 'insert into [DBO]. [user] ([username], [Password], [email], [PHONE], [subtime]) values ('% s',' % s ', '% s',' % s', '% s ');'

Corresponding configuration file 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"}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.