Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "github.com/olivere/elastic"
- )
- func main() {
- // check if all elasticsearch env vars were exported
- url := os.Getenv("ELASTIC_URL")
- index := os.Getenv("ELASTIC_INDEX")
- _type := os.Getenv("ELASTIC_TYPE")
- if url == "" || index == "" || _type == "" {
- log.Fatal("Please set all elastic search env vars.")
- }
- // Open our jsonFile
- jsonFile, _ := os.Open("states.json")
- defer jsonFile.Close()
- byteValue, _ := ioutil.ReadAll(jsonFile)
- var states States
- json.Unmarshal(byteValue, &states)
- // get elasticsearch client
- fmt.Println("Get elasticsearch client...")
- client, _ := elastic.NewClient()
- ctx := context.Background()
- fmt.Println("Start insert into elasticsearch...")
- for i := 0; i < len(states.States); i++ {
- _, _ = client.Index().Index(index).Type(_type).Id(states.States[i].State).BodyJson(states.States[i]).Do(ctx)
- }
- fmt.Println("Done...")
- }
- // State reference of a single state
- type State struct {
- State string `json:"state"`
- Population uint32 `json:"population"`
- }
- // States list of states
- type States struct {
- States []State `json:"states"`
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement