我想创建一个API主机服务器,它将点击BigQuery并在网页上显示查询结果
我设法创建了一个实际的服务器,该服务器通过使用GET方法(基于日期)显示信息。此外,我还有一个单独的代码,可以从BigQuery获取数据。但不幸的是,我没有足够的知识来将它们组合在一起。
这是当前代码:
尝试合并的服务器:
package main
import (
"fmt"
"io"
"net/http"
"os"
"cloud.google.com/go/civil"
)
type bqStructure struct {
SQLDate civil.Date `bigquery:"SQLDate"`
LocalStoreID string `bigquery:"LocalStoreID"`
LocalStoreName string `bigquery:"LocalStoreName"`
Longitude float64 `bigquery:"Longitude"`
Latitude float64 `bigquery:"Latitude"`
LONG_LAT string `bigquery:"lonLat"`
PLU string `bigquery:"PLU"`
PluProductName string `bigquery:"PluProductName"`
ORDERED string `bigquery:"Ordered"`
SOLD string `bigquery:"Sold"`
IMAGE_URL string `bigquery:"ImageURL"`
}
func queryParamDisplayHandler(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, "StartDate: "+req.FormValue("start"))
io.WriteString(res, "\nEndDate: "+req.FormValue("end"))
}
func main() {
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
if proj == "" {
fmt.Println("GOOGLE_CLOUD_PROJECT environment variable must be set.")
os.Exit(1)
}
http.HandleFunc("/map", func(res http.ResponseWriter, req *http.Request) {
queryParamDisplayHandler(res, req)
})
println("Enter this in your browser: http://localhost:80/example?start=20180101&end=20190101")
http.ListenAndServe(":80", nil)
}
这就是我打BigQuery的方式:
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
"google.golang.org/api/iterator"
)
func main() {
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
if proj == "" {
fmt.Println("GOOGLE_CLOUD_PROJECT environment variable must be set.")
os.Exit(1)
}
rows, err := query(proj)
if err != nil {
log.Fatal(err)
}
if err := printResults(os.Stdout, rows); err != nil {
log.Fatal(err)
}
}
// query returns a slice of the results of a query.
func query(proj string) (*bigquery.RowIterator, error) {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, proj)
if err != nil {
return nil, err
}
query := client.Query(`SELECT * FROM ` + "`XXXXX.XXXX.XXXX`" + `LIMIT 1000`)
return query.Read(ctx)
}
type StackOverflowRow struct {
SQLDate civil.Date `bigquery:"SQLDate"`
LocalStoreID string `bigquery:"LocalStoreID"`
LocalStoreName string `bigquery:"LocalStoreName"`
Longitude float64 `bigquery:"Longitude"`
Latitude float64 `bigquery:"Latitude"`
LONG_LAT string `bigquery:"lonLat"`
PLU string `bigquery:"PLU"`
PluProductName string `bigquery:"PluProductName"`
ORDERED string `bigquery:"Ordered"`
SOLD string `bigquery:"Sold"`
IMAGE_URL string `bigquery:"ImageURL"`
}
func printResults(w io.Writer, iter *bigquery.RowIterator) error {
for {
var row StackOverflowRow
err := iter.Next(&row)
if err == iterator.Done {
return nil
}
if err != nil {
return err
}
fmt.Fprintf(w, "SQLDate: %s LocalStoreID: %s LocalStoreName: %s Longitude: %f Latitude: %f LONG_LAT: %s PLU: %s PluProductName: %s ORDERED: %s SOLD: %s IMAGE_URL: %s\n", row.SQLDate, row.LocalStoreID, row.LocalStoreName, row.Longitude, row.Latitude, row.LONG_LAT, row.PLU, row.PluProductName, row.ORDERED, row.SOLD, row.IMAGE_URL)
}
}
我想找到一种方法来托管服务器,并根据必须在网页上显示的START
和END
变量从BigQuery获取信息。
链接以显示来自服务器的结果-http://localhost:80/map?start=20180101&end=20190101
一切都分开工作,因此每个代码都可以工作
最新更新:
工作正常,来自BQ的BUT信息显示在终端中,需要将其发送到网页。
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
"google.golang.org/api/iterator"
)
type bqStructure struct {
SQLDate civil.Date `bigquery:"SQLDate"`
LocalStoreID string `bigquery:"LocalStoreID"`
LocalStoreName string `bigquery:"LocalStoreName"`
Longitude float64 `bigquery:"Longitude"`
Latitude float64 `bigquery:"Latitude"`
LONG_LAT string `bigquery:"lonLat"`
PLU string `bigquery:"PLU"`
PluProductName string `bigquery:"PluProductName"`
ORDERED string `bigquery:"Ordered"`
SOLD string `bigquery:"Sold"`
IMAGE_URL string `bigquery:"ImageURL"`
}
func query(proj string) (*bigquery.RowIterator, error) {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, proj)
if err != nil {
return nil, err
}
query := client.Query(`SELECT * FROM ` + "`XXX`" + `LIMIT 1000`)
return query.Read(ctx)
}
func printResults(w io.Writer, iter *bigquery.RowIterator) error {
for {
var row bqStructure
err := iter.Next(&row)
if err == iterator.Done {
return nil
}
if err != nil {
return err
}
fmt.Fprintf(w, "SQLDate: %s LocalStoreID: %s LocalStoreName: %s Longitude: %f Latitude: %f LONG_LAT: %s PLU: %s PluProductName: %s ORDERED: %s SOLD: %s IMAGE_URL: %s\n", row.SQLDate, row.LocalStoreID, row.LocalStoreName, row.Longitude, row.Latitude, row.LONG_LAT, row.PLU, row.PluProductName, row.ORDERED, row.SOLD, row.IMAGE_URL)
}
}
func queryParamDisplayHandler(proj string, res http.ResponseWriter, req *http.Request) {
io.WriteString(res, "StartDate: "+req.FormValue("start"))
io.WriteString(res, "\nEndDate: "+req.FormValue("end"))
rows, err := query(proj)
if err != nil {
log.Fatal(err)
}
if err := printResults(os.Stdout, rows); err != nil {
log.Fatal(err)
}
}
func main() {
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
if proj == "" {
fmt.Println("GOOGLE_CLOUD_PROJECT environment variable must be set.")
os.Exit(1)
}
http.HandleFunc("/map", func(res http.ResponseWriter, req *http.Request) {
queryParamDisplayHandler(proj, res, req)
})
println("Enter this in your browser: http://localhost:80/map?start=20180101&end=20190101")
http.ListenAndServe(":80", nil)
}
需要找到如何转换
fmt.Fprintf(w, "SQLDate: %s LocalStoreID: %s LocalStoreName: %s Longitude: %f Latitude: %f LONG_LAT: %s PLU: %s PluProductName: %s ORDERED: %s SOLD: %s IMAGE_URL: %s\n", row.SQLDate, row.LocalStoreID, row.LocalStoreName, row.Longitude, row.Latitude, row.LONG_LAT, row.PLU, row.PluProductName, row.ORDERED, row.SOLD, row.IMAGE_URL)
要能够显示在网页上?