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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
美团技术团队
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
博客园_首页
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
B
Blog
The Register - Security
The Register - Security
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 我想我是青蛙

将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