Ejemplo n.º 1
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Setup a new query for building an index
	myQuery := gocb.NewN1qlQuery("CREATE PRIMARY INDEX ON `travel-sample`")
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Iterate through rows and print output
	var row interface{}
	for rows.Next(&row) {
		fmt.Printf("Results: %+v\n", row)
	}

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Ejemplo n.º 2
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Setup a new query with a placeholder
	myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
		"WHERE type='airport' AND city=$1 ")

	// Setup an array for parameters
	var myParams []interface{}
	myParams = append(myParams, "Reno")

	// Execute Query
	rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Iterate through rows and print output
	var row interface{}
	for rows.Next(&row) {
		fmt.Printf("Results: %+v\n", row)
	}

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
func queryUntyped(w http.ResponseWriter, r *http.Request) {

	// Grab the parameter for the city we're looking for
	search := r.URL.Query().Get("search")

	// New query, a really generic one with high selectivity
	myQuery := gocb.NewN1qlQuery("SELECT * FROM `travel-sample` " +
		"WHERE name like '" + search + "%' OR airportname like '" + search + "%' ")
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Interfaces for handling streaming return values
	var retValues []interface{}
	var row interface{}

	// Stream the values returned from the query into an untyped and unstructred
	// array of interfaces
	for rows.Next(&row) {
		retValues = append(retValues, row)
	}

	// Marshal array of interfaces to JSON
	jsonOut, err := json.Marshal(retValues)
	if err != nil {
		fmt.Println("ERROR PROCESSING STREAMING OUTPUT:", err)
	}

	// Return the JSON
	w.Write(jsonOut)

}
func queryOne(w http.ResponseWriter, r *http.Request) {

	// Grab the parameter for the city we're looking for
	search := r.URL.Query().Get("search")

	// New query, a really generic one with high selectivity
	myQuery := gocb.NewN1qlQuery("SELECT * FROM `travel-sample` " +
		"WHERE name like '" + search + "%' OR airportname like '" + search + "%' ")
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Interface for handling streaming return values
	var row interface{}

	// Stream the first result only into the interface
	err = rows.One(&row)
	if err != nil {
		fmt.Println("ERROR ITERATING QUERY RESULTS:", err)
	}

	// Marshal single result in interface
	jsonOut, err := json.Marshal(row)
	if err != nil {
		fmt.Println("ERROR PROCESSING STREAMING OUTPUT:", err)
	}

	// Return the JSON
	w.Write(jsonOut)

}
Ejemplo n.º 5
0
func airportHandler(w http.ResponseWriter, r *http.Request) {

	var queryPrep string

	switch search := r.URL.Query().Get("search"); len(search) {
	case 3:
		queryPrep = "SELECT airportname FROM `travel-sample` WHERE faa ='" + strings.ToUpper(search) + "'"
	case 4:
		if s := strings.ToUpper(search); s == search {
			queryPrep = "SELECT airportname FROM `travel-sample` WHERE icao ='" + strings.ToUpper(search) + "'"
		} else {
			queryPrep = "SELECT airportname FROM `travel-sample` WHERE airportname like '" + search + "%'"
		}
	default:
		queryPrep = "SELECT airportname FROM `travel-sample` WHERE airportname like '" + search + "%'"
	}

	myQuery := gocb.NewN1qlQuery(queryPrep)
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	var airports []Airport
	var row Airport
	for rows.Next(&row) {
		airports = append(airports, row)
	}
	_ = rows.Close()
	bytes, _ := json.Marshal(airports)
	w.Write(bytes)
}
func queryTyped(w http.ResponseWriter, r *http.Request) {

	// Grab the parameter for the city we're looking for
	search := r.URL.Query().Get("search")

	// New query, a really generic one with high selectivity
	myQuery := gocb.NewN1qlQuery("SELECT * FROM `travel-sample` " +
		"WHERE name like '" + search + "%' OR airportname like '" + search + "%' ")
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Interfaces for handling streaming return values
	var row TypedJSONAirport
	var retValues []TypedJSONAirport

	// Stream the values returned from the query into a typed array of structs
	for rows.Next(&row) {

		// Check if the current row has a value for FAA, if it does it's an airport
		//  and should be added to the return values
		if row.Inner.FAA != "" {
			retValues = append(retValues, row)
		}

		// Set the row to an empty struct, to prevent current values being added
		//  to the next row in the results collection returned by the query
		row = TypedJSONAirport{}
	}

	// Marshal array of structs to JSON
	bytes, err := json.Marshal(retValues)
	if err != nil {
		fmt.Println("ERROR PROCESSING STREAMING OUTPUT:", err)
	}

	// Return the JSON
	w.Write(bytes)

}
Ejemplo n.º 7
0
func testGet() {
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()

	cluster, e := gocb.Connect("couchbase://127.0.0.1")
	//	cluster, e := gocb.Connect("couchbase://42.62.101.136")
	log.Debugf("connect e=%+v", e)
	if e != nil {
		fmt.Errorf("%+v", e.Error())
	}
	bucket, e = cluster.OpenBucket("travel-sample", "")
	if e != nil {
		fmt.Errorf("OpenBucket ERROR=%+v", e.Error())
	}
	//
	//	myQuery := gocb.NewN1qlQuery("select * from `travel-sample`")
	myQuery := gocb.NewN1qlQuery("select * from `travel-sample` where airportname IS NOT NULL limit 1")
	N := 100
	t1 := time.Now()

	//	for i := 0; i < N; i++ {

	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}
	var airports []AirportIntermediary
	var row AirportIntermediary // 这里的查询结果要严格对应定义的格式,否则转出来的struct的内部值都是空值
	log.Infof("rows=%#v", rows)
	for rows.Next(&row) {
		airports = append(airports, row)
		log.Debugf("row=%+v", row)
	}
	log.Infof("airport = %+v", airports)

	if err := rows.Close(); err != nil {
		fmt.Printf("N1QL query error: %s\n", err)
	}

	//	}
	t2 := time.Now()
	d := t2.Sub(t1)

	log.Infof("BenchmarkGo, %+v times in %+v", N, d)
}
Ejemplo n.º 8
0
func flightPathHandler(w http.ResponseWriter, r *http.Request) {

	var queryPrep, queryTo, queryFrom string
	var fromLon, fromLat, toLon, toLat, dist float64
	var price, flightTime, weekday int
	var leave time.Time
	var row AirportIntermediary
	var airports []AirportIntermediary
	var flight Flight
	var flights []Flight

	from := r.URL.Query().Get("from")
	to := r.URL.Query().Get("to")

	leave, _ = time.Parse(layout, r.URL.Query().Get("leave"))
	weekday = int(leave.Weekday()) + 1

	queryPrep = "SELECT faa as fromAirport,geo FROM `travel-sample` WHERE airportname = '" + from +
		"' UNION SELECT faa as toAirport,geo FROM `travel-sample` WHERE airportname = '" + to + "'"

	myQuery := gocb.NewN1qlQuery(queryPrep)
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	for rows.Next(&row) {
		airports = append(airports, row)
		if row.ToAirport != "" {
			toLat = row.Geo.Lat
			toLon = row.Geo.Lon
			queryTo = row.ToAirport
		}
		if row.FromAirport != "" {
			fromLat = row.Geo.Lat
			fromLon = row.Geo.Lon
			queryFrom = row.FromAirport
		}
		row = AirportIntermediary{}
	}
	dist = Haversine(fromLon, fromLat, toLon, toLat)
	flightTime = int(dist / averageKilometersHour)
	price = int(dist * distanceCostMultiplier)

	_ = rows.Close()

	queryPrep = "SELECT r.id, a.name, s.flight, s.utc, r.sourceairport, r.destinationairport, r.equipment " +
		"FROM `travel-sample` r UNNEST r.schedule s JOIN `travel-sample` a ON KEYS r.airlineid WHERE r.sourceairport='" +
		queryFrom + "' AND r.destinationairport='" + queryTo + "' AND s.day=" + strconv.Itoa(weekday) + " ORDER BY a.name"

	myQuery = gocb.NewN1qlQuery(queryPrep)
	rows, err = bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	for i := 0; rows.Next(&flight); i++ {
		flight.Flighttime = flightTime
		flight.Price = price
		flights = append(flights, flight)
	}
	_ = rows.Close()
	bytes, _ := json.Marshal(flights)
	w.Write(bytes)
}