package main import ( "fmt" "github.com/gocql/gocql" "time" ) func main() { // connect to the cluster var session *gocql.Session var cluster *gocql.ClusterConfig var err error cluster = gocql.NewCluster() cluster.Keyspace = "demo11" hosts := []string{"127.0.0.1:9042"} cluster.Hosts=hosts cluster.ProtoVersion = 4 cluster.ConnectTimeout = 2 * time.Duration(time.Second) cluster.Timeout = 2 * time.Duration(time.Second) cluster.Consistency = gocql.Quorum cluster.NumConns=10 cluster.Consistency = gocql.One session, err = cluster.CreateSession() if err != nil { panic(err) } defer session.Close() //unlogged batch, 进行批量插入,最好是partition key 一致的情况 //t := time.Now() batch := session.NewBatch(gocql.UnloggedBatch) for i := 0; i < 100; i++ { batch.Query(`INSERT INTO bigrow (rowname, iplist) VALUES (?,?)`, fmt.Sprintf("name_%d", i),i) } if err := session.ExecuteBatch(batch); err != nil { fmt.Println("execute batch:", err) } //bt := time.Now().Sub(t).Nanoseconds() // //t = time.Now() //for i := 0; i < 100; i++ { // session.Query(`INSERT INTO bigrow (rowname, iplist) VALUES (?,?)`, fmt.Sprintf("name_%d", i), fmt.Sprintf("ip_%d", i)) //} //nt := time.Now().Sub(t).Nanoseconds() // //t = time.Now() //sbatch := session.NewBatch(gocql.UnloggedBatch) //for i := 0; i < 100; i++ { // sbatch.Query(`INSERT INTO bigrow (rowname, iplist) VALUES (?,?)`, "samerow", fmt.Sprintf("ip_%d", i)) //} //if err := session.ExecuteBatch(sbatch); err != nil { // fmt.Println("execute batch:", err) //} //sbt := time.Now().Sub(t).Nanoseconds() //fmt.Println("bt:", bt, "sbt:", sbt, "nt:", nt) // ////----------out put------------------ //// ./rawtest //// bt: 5795593 sbt: 3003774 nt: 261775 ////------------------------------------ // //// insert a tweet //if err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`, // "me", gocql.TimeUUID(), "hello world").Exec(); err != nil { // log.Fatal(err) //} // //var id gocql.UUID //var text string // ///* Search for a specific set of records whose 'timeline' column matches // * the value 'me'. The secondary index that we created earlier will be // * used for optimizing the search */ //if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ? LIMIT 1`, // "me").Consistency(gocql.One).Scan(&id, &text); err != nil { // log.Fatal(err) //} //fmt.Println("Tweet:", id, text) // //// list all tweets //iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`, "me").Iter() //for iter.Scan(&id, &text) { // fmt.Println("Tweet:", id, text) //} //if err := iter.Close(); err != nil { // log.Fatal(err) //} // //query := session.Query(`SELECT * FROM bigrow where rowname = ?`, "30") //// query := session.Query(`SELECT * FROM bigrow `) // //var m map[string]interface{} //m = make(map[string]interface{}, 10) //err := query.Consistency(gocql.One).MapScan(m) //if err != nil { // log.Fatal(err) //} //fmt.Printf("%#v", m) }
有疑问加站长微信联系(非本文作者)