I am trying to read Redis Key-val in Go. Key is string and value is JSON string. Eg- Key=
discov_32161296
and Value as Json string=
"{\"10283\":true,\"11064\":true,\"15123\":true,\"15447\":true,\"15926\":true,\"16530\":true,\"16537\":true,\"16799\":true,\"17088\":true,\"17249\":true,\"18501\":true,\"18529\":true,\"18601\":true,\"3044\":true,\"3687\":true,\"4926\":true,\"5483\":true,\"6\":true,\"6675\":true,\"8332\":true,\"8336\":true,\"8674\":true}"
Getting below error while reading in Go
redis.Values err redigo: unexpected type for Values, got type []uint8
Here's my code :
uIDDiscoveryOffer := fmt.Sprintf("%s_%d", "discov", uid)
opDataStr, err := redis.String(redis.Values(con.Do("GET", uIDDiscoveryOffer)))
if err != nil || err != redis.ErrNil {
utils.Log1("readCacheTxnByUID-Disc-redis.Values-err", fmt.Sprint("redis.Values err : ", uidDiscoveryOffer, " error: ", err.Error()))
} else {
//Some Logic
}
The Redis GET returns the value of a key. redis.Values() may be used to convert the result of a command that returns multiple items.
Since GET returns a single item, only use redis.String(), you don't need redis.Values() here:
opDataStr, err := redis.String(con.Do("GET", uIDDiscoveryOffer))
Related
I am trying to fetch data from blockchain using query in chaincode. I have invoked around 2,50,000 records in blockchain and trying to fetch the data using query. When I run the chaincode and get the peer logs, I am getting the below error.
Chaincode error in peer logs:
Here is my code:
queryStringsa := fmt.Sprintf("{\"selector\":{\"$and\":[{\"savesID\":{\"$ne\":\"%s\"}},{\"bankID\":{\"$eq\":\"%s\"}},{\"ytdSavedFlag\":{\"$ne\":\"%s\"}},{\"saveMonthYear\":{\"$eq\":\"%s\"}}]},\"use_index\":[\"_design/indexSavesDataReportDoc\",\"indexSavesDataReportName\"]}","null",bankidsave,"Yes",lastImportDatekey)
queryResultss11sa, errsav := getQueryResultForQueryString(stub, queryStringsa)
// getQueryResultForQueryString
func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string) ([]byte, error) {
_scbLogger.Infof(string("**********************************"))
_scbLogger.Infof(string("getQueryResultForQueryString queryString : "+ queryString))
_scbLogger.Infof(string("**********************************"))
resultsIterator, err := stub.GetQueryResult(queryString)
if err != nil {
_scbLogger.Error("Error Starting SCB-Efficiency Chaincode is " + string(err.Error()))
return nil, err
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
fmt.Println("resultsIterator length : ", (resultsIterator))
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
//fmt.Println("queryresponse inside for next : ", queryResponse)
if err != nil {
fmt.Println("$$$$$$$$$$$ error in reuslt iterator : ", err)
return nil, err
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
//fmt.Println("string(queryResponse.Value) : ",string(queryResponse.Value))
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
//fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())
return buffer.Bytes(), nil
}
I have 5 set of different queries in same function. sometimes few queries return the query result and sometimes none of the queries gives the result, rather it shows the above error.
When I run the same queries in CouchDB fauxton I am getting the query result. When I run the same function for less number of records, the queries work properly without any errors.
i have to insert into a file the result of a mgo query MongoDB converted in Go to get the id of images
var path="/home/Medo/text.txt"
pipe := cc.Pipe([]bson.M{
{"$unwind": "$images"},
{"$group": bson.M{"_id": "null", "images":bson.M{"$push": "$images"}}},
{"$project": bson.M{"_id": 0}}})
response := []bson.M{}
errResponse := pipe.All(&response)
if errResponse != nil {
fmt.Println("error Response: ",errResponse)
}
fmt.Println(response) // to print for making sure that it is working
data, err := bson.Marshal(&response)
s:=string(data)
if err22 != nil {
fmt.Println("error insertion ", err22)
}
Here is the part where I have to create a file and write on it.
The problem is when I got the result of the query in the text file I got an enumeration values in the last of each value for example:
id of images
23456678`0`
24578689`1`
23678654`2`
12890762`3`
76543890`4`
64744848`5`
so for each value i got a number sorted in the last , and i can't figure out how , after getting the reponse from the query i converted the Bson to []Byte and then to Stringbut it keeps me getting that enumeration sorted values in the last of each results
I'd like to drop those 0 1 2 3 4 5
var _, errExistFile = os.Stat(path)
if os.IsNotExist(errExistFile) {
var file, errCreateFile = os.Create(path)
if isError(erro) {
return
}
defer file.Close()
}
fmt.Println("==> done creating file", path)
var file, errii = os.OpenFile(path, os.O_RDWR, 0644)
if isError(errii) {
return
}
defer file.Close()
// write some text line-by-line to file
_, erri := file.WriteString(s)
if isError(erri) {
return
}
erri = file.Sync()
if isError(erri) {
return
}
fmt.Println("==> done writing to file")
You could declare a simple struct eg
simple struct {
ID idtype `bson:"_id"`
Image int `bson:"images"`
}
The function to put the image ids into the file would be
open file stuff…
result := simple{}
iter := collection.Find(nil).Iter()
for iter.Next(&result){
file.WriteString(fmt.Sprintf("%d\n",result.Image))
}
iter.Close()
I am writing a function in golang to search for a string in elasticsearch documents which are indexed. I am using elasticsearch golang client elastic. For example consider the object is tweet,
type Tweet struct {
User string
Message string
Retweets int
}
And the search function is
func SearchProject() error{
// Search with a term query
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(&termQuery). // specify the query
Sort("user", true). // sort by "user" field, ascending
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do() // execute
if err != nil {
// Handle error
panic(err)
return err
}
// searchResult is of type SearchResult and returns hits, suggestions,
// and all kinds of other information from Elasticsearch.
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
// Each is a convenience function that iterates over hits in a search result.
// It makes sure you don't need to check for nil values in the response.
// However, it ignores errors in serialization. If you want full control
// over iterating the hits, see below.
var ttyp Tweet
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
t := item.(Tweet)
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
// TotalHits is another convenience function that works even when something goes wrong.
fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
// Here's how you iterate through results with full control over each step.
if searchResult.Hits != nil {
fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
// Iterate through results
for _, hit := range searchResult.Hits.Hits {
// hit.Index contains the name of the index
// Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
var t Tweet
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
// Deserialization failed
}
// Work with tweet
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no tweets\n")
}
return nil
}
This search is printing tweets by the user 'olivere'. But if I give 'olive' then search is not working. How do I search for a string which is part of User/Message/Retweets?
And the Indexing function looks like this,
func IndexProject(p *objects.ElasticProject) error {
// Index a tweet (using JSON serialization)
tweet1 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
put1, err := client.Index().
Index("twitter").
Type("tweet").
Id("1").
BodyJson(tweet1).
Do()
if err != nil {
// Handle error
panic(err)
return err
}
fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
return nil
}
Output:
Indexed tweet 1 to index twitter, type tweet
Got document 1 in version 1 from index twitter, type tweet
Query took 4 milliseconds
Tweet by olivere: It's a Raggy Waltz
Found a total of 1 tweets
Found a total of 1 tweets
Tweet by olivere: It's a Raggy Waltz
Version
Go 1.4.2
Elasticsearch-1.4.4
Elasticsearch Go Library
github.com/olivere/elastic
Could anyone help me on this.? Thank you
How you search and find data depends on your analyser - from your code it's likely that the standard analyser is being used (i.e. you haven't specified an alternative in your mapping).
The Standard Analyser will only index complete words. So to match "olive" against "olivere" you could either:
Change the search process
e.g. switch from a term query to a Prefix query or use a Query String query with a wildcard.
Change the index process
If you want to find strings within larger strings then look at using nGrams or Edge nGrams in your analyser.
multiQuery := elastic.NewMultiMatchQuery(
term,
"name", "address", "location", "email", "phone_number", "place", "postcode",
).Type("phrase_prefix")
So, I'm trying to add a string to an existing map that is created from toml.
http://hastebin.com/vayolavose
When I try and build I get the error:
./web.go:56: arguments to copy have different element types: []proxy.Address and string
How would I go about converting it? I've been trying this for the past like 4 hours.
Thanks
while,the code below is your source code
func handleAddFunc(w http.ResponseWriter, r *http.Request) {
backend := r.FormValue("backend")
key := r.FormValue("key")
if !isAuthorized(key) {
respond(w, r, 403, "")
return
}
w.Header().Set("Content-Type", "text/plain")
if !readConfig() {
return
}
activeAddrs = make([]proxy.Address, len(config.Proxy.ServerAddrs))
backendAddr = make([]proxy.Address, len(backend))
copy(backendAddr, config.Proxy.ServerAddrs)
copy(backendAddr, backend)
loadBalancer.SetAddrs(backendAddr)
fmt.Fprintf(w, "Input value of ", backend, "and here is the byte", backendAddr)
}
your code's error, is copy(backendAddr, backend), variable backend is a string value from the request from, you may change this into []proxy.Address, such as (consider I donnot know the struct of proxy.Address ):
var backendAddr = []proxy.Address{}
for _,str := range strings.split(backend,","){
backendAddr = append(backendAddr, &proxy.Address(str))
}
Using the database/sql package in go for things like sql.Exec will return dynamically generated, unreferenced errors such as
"Error 1062: Duplicate entry '192' for key 'id'"
The problem is that it can also return errors such as
"Error 1146: Table 'tbl' doesn't exist"
From the same call to sql.Exec
How can I tell the difference between these two errors without
String comparison, or
Pattern matching for error code
Or are those idiomatic viable solutions for this problem?
database/sql package does not solve this problem. It's driver specific. For example, for mysql you can use:
if mysqlError, ok := err.(*mysql.MySQLError); ok {
if mysqlError.Number == 1146 {
//handling
}
}
Also, you can find some error constant package, like mysqlerr from VividCortex, and use it:
if mysqlError, ok := err.(*mysql.MySQLError); ok {
if mysqlError.Number == mysqlerr.ER_NO_SUCH_TABLE {
//handling
}
}
It's not much better than pattern matching, but seems to be more idiomatic.
I think there's no idiomatic solution, but I wrote a simple function for getting the error number, so you can easily compare them.
In this solution I assume that the construction of the error message is always the same: "Error -some number here-: Error description".
If there's no number in the error or something went wrong it returns 0.
func ErrorCode(e error) int {
err := e.Error() //the description of the error
if len(err) < 6 { //if its too small return 0
return 0
}
i := 6 //Skip the part "Error "
for ; len(err) > i && unicode.IsDigit(rune(err[i])); i++ {
} // Raising i until we reach the end of err or we reach the end of error code
n, e := strconv.Atoi(string(err[6:i])) //convert it to int
if e != nil {
return 0 //something went wrong
}
return n //return the error code
}
Go playground link: http://play.golang.org/p/xqhVycsuyI