Skip to content

Instantly share code, notes, and snippets.

@border
Last active December 29, 2015 18:09
Show Gist options
  • Save border/7708519 to your computer and use it in GitHub Desktop.
Save border/7708519 to your computer and use it in GitHub Desktop.
homeinns 2000w by golang + mongo
package main
import (
"bufio"
"bytes"
"io"
"os"
"strings"
"time"
"log"
"labix.org/v2/mgo"
)
type Person struct {
Name string
CardNo string
Descriot string
CtfTp string
CtfId string
Gender string
Birthday string
Address string
Zip string
Dirty string
District1 string
District2 string
District3 string
District4 string
District5 string
District6 string
FirstNm string
LastNm string
Duty string
Mobile string
Tel string
Fax string
Email string
Nation string
Taste string
Education string
Company string
Ctel string
CAddress string
Czip string
Family string
Version string
UID string
}
// Read a whole file into the memory
func readLines(path string, c *mgo.Collection) (err error) {
var (
file *os.File
part []byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
defer file.Close()
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 1024))
isFirst := true
count := 0
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
// lines = append(lines, buffer.String())
if !isFirst {
//log.Println(buffer.String())
items := strings.Split(buffer.String(), ",")
if len(items) == 33 {
err = c.Insert(&Person{items[0], items[1], items[2], items[3], items[4], items[5],
items[6], items[7], items[8], items[9], items[10], items[11], items[12], items[13], items[14],
items[15], items[16], items[17], items[18], items[19], items[20], items[21], items[22], items[23], items[24],
items[25], items[26], items[27], items[28], items[29], items[30], items[31], items[32]})
}
if err != nil {
panic(err)
}
log.Println("++++++++++++++++++", count)
count++
}
buffer.Reset()
}
isFirst = false
}
if err == io.EOF {
err = nil
}
return
}
func main() {
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Strong, true)
c := session.DB("homeinns").C("user")
readLines("./homeinns.txt", c)
time.Sleep(5 * 1e9)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment