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

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 我想我是青蛙

将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