我最近将我们的暂存环境从neo4j 2.2.2社区版docker容器移到了2.3.0-RC1 HA群集。
此go脚本为经过身份验证的用户提供API端点,以便上传csv文件,然后以与产品ID(upc)与现有产品数据匹配的方式对其进行处理。在匹配时,用户的关联分发者创建与找到的产品的关系,并将csv文件中可用的信息添加到创建的关系中。
我的问题是我在Neo4j中遇到了无法追踪的新环境中的故障:“无法识别的事务ID。事务可能已经超时并被回滚。”当用户尝试上传文件时。
这是完整的脚本(我的第二个golang脚本,所以欢迎关于样式的指针)。
package main
import (
"database/sql"
"encoding/csv"
"fmt"
_ "gopkg.in/cq.v1"
_ "gopkg.in/cq.v1/types"
"net/http"
"os"
"strconv"
"strings"
)
type ProductInfo struct {
Upc uint64
InStock uint64
BasePrice float64
LeadMin uint64
LeadMax uint64
Sku string
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
distributor := strings.Split(r.URL.Path, "/")[2]
reader := csv.NewReader(file)
reader.FieldsPerRecord = 6
reader.TrimLeadingSpace = true
rawCSVdata, err := reader.ReadAll()
if err != nil {
fmt.Fprintln(w, err)
return
}
db, err := sql.Open("neo4j-cypher", os.Getenv("MINAMI_NEO4J_URL"))
if err != nil {
fmt.Fprintln(w, err)
return
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
fmt.Fprintln(w, err)
return
}
stmt, err := tx.Prepare(`
MATCH (d:Distributor {slug:'` + distributor + `'})
MATCH (p:Product {upc: {0}})
MERGE (d)-[r:SELLS]->(p)
SET r.inStock = {1}, r.sellsFor = {2}, r.leadMin = {3}, r.leadMax = {4}, r.sku = {5}
RETURN p LIMIT 1
`)
if err != nil {
fmt.Fprintln(w, err)
return
}
var Row ProductInfo
for _, each := range rawCSVdata {
Row.Upc, _ = strconv.ParseUint(each[0], 10, 0)
Row.InStock, _ = strconv.ParseUint(each[1], 10, 0)
Row.BasePrice, _ = strconv.ParseFloat(each[2], 0)
Row.LeadMin, _ = strconv.ParseUint(each[3], 10, 0)
Row.LeadMax, _ = strconv.ParseUint(each[4], 10, 0)
Row.Sku = each[5]
stmt.Exec(Row.Upc, Row.InStock, Row.BasePrice, Row.LeadMin, Row.LeadMax, Row.Sku)
}
err = tx.Commit()
if err != nil {
fmt.Fprintln(w, err)
return
}
fmt.Fprintf(w, "File uploaded and products in store updated.")
}
func main() {
http.HandleFunc("/", uploadHandler)
http.ListenAndServe(":8888", nil)
}
我正在使用haproxy来了解谁是主人,谁是HA集群中的奴隶。环境变量MINAMI_NEO4J_URL通过haproxy以这种形式设置为master:http:// {haproxyip}:{masterport}