惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 我想我是青蛙

将golang程序注册为windows服务 CentOS上安装spark standalone mode(转载) - 我想我是青蛙 关于听歌这回事 - 我想我是青蛙 c#读取apk 信息 golang 读取mongob数据写入sqlserver golang 通用Contains方法 - 我想我是青蛙 PetaPoco介绍 - 我想我是青蛙 白话MongoDB(三)(转载) - 我想我是青蛙 白话MongoDB(二)(转载) 白话MongoDB(一) (转载) - 我想我是青蛙 给文章加入关键字链接 针对firefox ie6 ie7 ie8的css样式hack (转载) 好久不写日志了,现在开始,好好写了。。 sharepoint 查询calendar recurrence sharepoint之lookup字段 sharepoint获取Audiences 获取exchangeserve的calendar的item sharepoint错误处理 c#上传下载ftp(支持断点续传)
golang读取文件信息插入mongodb
我想我是青蛙 · 2013-04-17 · via 博客园 - 我想我是青蛙
package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io"
	"labix.org/v2/mgo"
	"os"
	"runtime"
)

/*
初始化配置
*/
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 = Config{}

var worker = runtime.NumCPU()

//手机号码
type PhoneArea struct {
	Phone     string "PhoneStart"
	Area      string "Province"
	City      string "City"
	PhoneType string "PhoneType"
	Code      string "Code"
}

//配置
type Config struct {
	MongodbServer string
	PhoneareaFile string
}

/*导入手机地理信息*/
func ImportPhoneInfo() {
	var chanPhoneArea = make(chan PhoneArea)
	// 标记完成
	dones := make(chan struct{}, worker)

	//读取文件信息
	go addPhoneInfo(chanPhoneArea)
	//插入mongodb
	for i := 0; i < worker; i++ {
		go doPhoneInfo(chanPhoneArea, dones)
	}
	//等待完成
	awaitForCloseResult(dones)
	fmt.Println("插入完毕")
}

/*
获取手机地理信息
*/
func addPhoneInfo(chanPhoneArea chan<- PhoneArea) {
	file, err := os.Open(config.PhoneareaFile)

	if err != nil {
		fmt.Println("打开文件失败", err)
		return
	}
	defer file.Close()
	fmt.Println("读取手机地理信息文件")
	reader := csv.NewReader(file)

	for {
		line, err := reader.Read()

		if err == io.EOF {
			close(chanPhoneArea)
			fmt.Println("手机地理信息文件读取完毕")
			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
	}
}

/*
插入信息到mongodb
*/
func doPhoneInfo(chanPhoneArea <-chan PhoneArea, dones chan<- struct{}) {
	//开启loop个协程

	session, err := mgo.Dial(config.MongodbServer)
	if err != nil {
		fmt.Println("错误")
		panic(err)
		return
	}
	defer session.Close()
	c := session.DB("Test").C("PhoneAreaInfo")

	for phonearea := range chanPhoneArea {
		fmt.Println("插入:", phonearea)
		c.Insert(&phonearea)
	}

	dones <- struct{}{}
}

func awaitForCloseResult(dones <-chan struct{}) {
	for {
		<-dones
		worker--
		if worker <= 0 {
			return
		}
	}
}
	{
		"MongodbServer" : "127.0.0.1",
		"PhoneareaFile" : "phonearea.txt"
	}
号码,省份,城市,卡型,区号
1300000,北京,北京,北京联通GSM卡,010
1300001,江苏,常州,江苏联通如意通卡,0519
1300006,江苏,南京,江苏联通GSM卡,025
1300010,北京,北京,北京联通GSM卡,010
1300011,北京,北京,北京联通GSM卡,010
1300012,天津,天津,天津联通GSM卡,022
1300013,天津,天津,天津联通GSM卡,022
1300014,天津,天津,天津联通GSM卡,022
1300015,山东,淄博,山东联通GSM卡,0533
1300016,山东,烟台,山东联通GSM卡,0535
1300017,山东,济南,山东联通GSM卡,0531
1300018,天津,天津,天津联通GSM卡,022
1300019,天津,天津,天津联通GSM卡,022
1300020,上海,上海,上海联通GSM卡,021
1300021,上海,上海,上海联通GSM卡,021
1300022,上海,上海,上海联通GSM卡,021
1300023,上海,上海,上海联通GSM卡,021
1300024,上海,上海,上海联通GSM卡,021
1300025,江苏,南京,江苏联通GSM卡,025
1300026,江苏,南京,江苏联通GSM卡,025