Created
August 24, 2017 20:55
-
-
Save hectorgool/efcf11cb81a5fa1b105f204fe7cf117b to your computer and use it in GitHub Desktop.
Read a csv file To dynamically create json objects with file data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
twitter@hector_gool | |
*/ | |
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"os" | |
"strconv" | |
//"os/exec" | |
) | |
type Document struct { | |
cp, colonia, ciudad, delegacion, lat, lon string | |
} | |
const ( | |
TOTAL_ROWS = 100 | |
ELASTICSEARCH_INDEX string = "mx2" | |
ELASTICSEARCH_TYPE string = "postal_code" | |
) | |
func main() { | |
file, err := os.Open("./MX.txt") | |
printError(err) | |
defer file.Close() | |
reader := csv.NewReader(file) | |
reader.Comma = '\t' //delimeter | |
rows, err := reader.ReadAll() | |
printError(err) | |
d := new(Document) | |
for n, col := range rows { | |
d.cp = col[1] | |
d.colonia = col[2] | |
d.ciudad = col[3] | |
d.delegacion = col[5] | |
d.lat = col[9] | |
d.lon = col[10] | |
n++ | |
if n <= TOTAL_ROWS { | |
// load documents in elasticsearch | |
/* | |
curl := d.toCurl(n) | |
out, err := exec.Command("sh", "-c", curl).Output() | |
printError(err) | |
fmt.Printf("%v\n\n", string(out)) | |
*/ | |
// print documents | |
curl := d.toCurl(n) | |
fmt.Printf("%v\n\n", curl) | |
} | |
} | |
} | |
func (d *Document) toCurl(n int) string { | |
curl := | |
` | |
curl -u elastic:changeme -X PUT "http://localhost:9200/` + ELASTICSEARCH_INDEX + `/` + ELASTICSEARCH_TYPE + `/` + strconv.Itoa(n) + `" -d " | |
{ | |
\"cp\" : ` + d.cp + `, | |
\"colonia\" : \"` + d.colonia + `\", | |
\"ciudad\" : \"` + d.ciudad + `\", | |
\"delegacion\" : \"` + d.delegacion + `\", | |
\"location\": { | |
\"lat\": ` + d.lat + `, | |
\"lon\": ` + d.lon + ` | |
} | |
}" | |
` | |
return curl | |
} | |
func printError(err error) { | |
if err != nil { | |
fmt.Printf("\nError: %v \n ", err.Error()) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment