美文网首页程序员
Go语言协程池模型--图数据库(Neo4j)写入

Go语言协程池模型--图数据库(Neo4j)写入

作者: 汤汤的汤 | 来源:发表于2019-10-20 17:45 被阅读0次

    Go语言协程池

    package main
    import (
        "fmt"
        "time"
        driver "github.com/johnnadratowski/golang-neo4j-bolt-driver"
    ) 
    var DriverNeo4j driver.Driver
    //写入数据库
    func CreateNodes(CypherList []string, Connect driver.Conn){
        if len(CypherList) > 0{
            NilList := make([]map[string]interface{}, len(CypherList))
            stmt, _ := Connect.PreparePipeline(CypherList...)
            _, errexc := stmt.ExecPipeline(NilList...)
            if errexc != nil{
                fmt.Println("excute err!!!!!!!!!!!!",errexc)
            }
            stmt.Close()
        }
    }
    
    // 一个job的执行任务
    func woker(CypherList chan []string){
        var cypher_str_arr []string
        connect, errcon := DriverNeo4j.OpenNeo("bolt://neo4j:passord@localhost:7687")
        if errcon != nil {
            fmt.Println("connection err!!!!!!!!!", errcon)
        }
        for cypher_str_arr = range CypherList{
            CreateNodes(cypher_str_arr,connect)
        }   
        connect.Close()
        //time.Sleep(1 * time.Second)
    }
    
    
    func main(){
        DriverNeo4j = driver.NewDriver()
        var cypherList []string
        var clearList []string
        var cypherstr string
        jobch := make(chan []string, 10)
        controlch := make(chan int,100)
        for i := 0; i< 70000; i++{
            cypherstr = fmt.Sprintf(`CREATE (n:JJ{Num:%d})`,i)
            cypherList = append(cypherList,cypherstr)
            if len(cypherList) % 20 == 0{
                templist := cypherList
                cypherList = clearList
                jobch <- templist
            }
        }
        //workpool
            //开始用协程池执行各工作
        for w := 0; w < 10; w++{
            go woker(jobch)
        }
        time.Sleep(5 * time.Second)
    }
    

    相关文章

      网友评论

        本文标题:Go语言协程池模型--图数据库(Neo4j)写入

        本文链接:https://www.haomeiwen.com/subject/owjamctx.html