Golang reads file information and inserts it into MongoDB

Source: Internet
Author: User

In the past few days, a function has been implemented in the project to read the home location information of the mobile phone, and then insert the MongoDB

Mobile phone location format

Number Province City Card Type Area code
130001 Jiangsu Changzhou Jiangsu Unicom 0519

 

CodeAs follows:

Package mainimport ("encoding/CSV" "encoding/JSON" "FMT" "Io" "labix.org/v2/mgo" "OS" "RunTime")/* initialize configuration */func Init () {file, _: = OS. open ("config. JSON ") BUF: = make ([] Byte, 2048) n, _: = file. read (BUF) FMT. println (string (BUF) Err: = JSON. unmarshal (BUF [: N], & config) If Err! = Nil {panic (ERR) FMT. println (ERR) }}func main () {runtime. gomaxprocs (runtime. numcpu () importphoneinfo ()} var Config = config {} var worker = runtime. numcpu () // mobile phone number type phonearea struct {phone string "phonestart" area string "Province" city string "city" phonetype string "phonetype" code string "code"} // configure type config struct {mongodbserver stringphoneareafile string}/* import mobile phone geographic information */func importphoneinfo () {VaR chanphonearea = make (Chan phonearea) // mark the completed dones: = make (Chan struct {}, worker) // read the file information go addphoneinfo (chanphonearea) // insert mongodbfor I: = 0; I <worker; I ++ {go dophoneinfo (chanphonearea, dones)} // wait until awaitforcloseresult (dones) FMT is completed. println ("inserted")}/* Get Mobile Phone geographic information */func addphoneinfo (chanphonearea Chan <-phonearea) {file, err: = OS. open (config. phoneareafile) If Err! = Nil {FMT. println ("failed to open the file", err) return} defer file. close () FMT. println ("reading mobile phone Geographic Information Files") Reader: = CSV. newreader (File) for {Line, err: = reader. read () If err = Io. EOF {close (chanphonearea) FMT. println ("Mobile Geographic Information File Read completed") break} else if Err! = Nil {close (chanphonearea) FMT. println ("error:", err) break} phonearea: = phonearea {line [0], line [1], line [2], line [3], line [4]} chanphonearea <-phonearea}/* insert information to MongoDB */func dophoneinfo (chanphonearea <-chan phonearea, dones Chan <-struct {}) {// enable loop session, err: = MgO. dial (config. mongodbserver) If Err! = Nil {FMT. println ("error") panic (ERR) return} defer session. close () C: = session. dB ("test "). C ("phoneareainfo") for phonearea: = range chanphonearea {FMT. println ("insert:", phonearea) C. insert (& phonearea)} dones <-struct {}}} func awaitforcloseresult (dones <-chan struct {}) {for {<-donesworker -- if worker <= 0 {return }}}

Configuration file config. JSON

 
{"Mongodbserver": "127.0.0.1", "phoneareafile": "phonearea.txt "}

Mobile phone Geographic Information phonearea.txt

 
Number, province, city, card type, area code 1300000, Beijing Unicom, gsmcard, 0101300001, Jiangsu, Changzhou, Jiangsu Unicom, ruitong card, 05191300006, Jiangsu, Nanjing, jiangsu Unicom GSM card, 0251300010, Beijing Unicom, 0101300011, Beijing, Beijing Unicom, 0101300012, Tianjin Unicom, 0221300013, Tianjin, Tianjin, tianjin Unicom GSM card, 0221300014, Tianjin Unicom GSM card, 0221300015, Shandong, Zibo, Shandong Unicom GSM card, 05331300016, Shandong, Yantai, Shandong Unicom GSM card, 05351300017, Shandong, Jinan, shandong Unicom GSM card, 05311300018, Tianjin Unicom GSM card, 0221300019, Tianjin Unicom GSM card, 0221300020, Shanghai Unicom GSM card, 0211300021, Shanghai, Shanghai, shanghai Unicom GSM card, 0211300022, Shanghai Unicom GSM card, 0211300023, Shanghai Unicom GSM card, 0211300024, Shanghai Unicom GSM card, 0211300025, Jiangsu, Nanjing, GSM card of Jiangsu Unicom, 0251300026, Jiangsu, Nanjing, Jiangsu Unicom, 025

Related Article

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.