All Products
Search
Document Center

OpenSearch:Data push demo

Last Updated:Jul 24, 2023

This topic describes the data push demo that shows you how to push data. The demo dynamically encapsulates document data into Map objects and calls the add() method to add these Map objects to the cache. Then, the demo calls the pushDocuments() method to submit the document data in these Map objects at a time.

Sample code

package main

import (
    "fmt"
    util "github.com/alibabacloud-go/tea-utils/service"
    "github.com/alibabacloud-go/tea/tea"
    ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {
    
    // Create a client instance for sending requests.
    // Endpoint: the endpoint of the OpenSearch API in your region.
    // AccessUserName and AccessPassWord: the AccessKey pair used for authentication.
    
    config := &ha3engine.Config{
        Endpoint:       tea.String("<Endpoint>"),
        InstanceId:     tea.String("<InstanceId>"),
        AccessUserName: tea.String("<AccessUserName>"),
        AccessPassWord: tea.String("<AccessPassWord>"),
    }
    
    // Create a client for sending requests.
    client, _clientErr := ha3engine.NewClient(config)
    
    // If an error occurs when the system creates the client, return _clientErr and display the error information.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }
    runtime := &util.RuntimeOptions{
        ConnectTimeout: tea.Int(5000),
        ReadTimeout:    tea.Int(10000),
        Autoretry:      tea.Bool(false),
        IgnoreSSL:      tea.Bool(false),
        MaxIdleConns:   tea.Int(50),
        HttpProxy:      tea.String("http://116.*.*.187:8088"),
    }
    docPush(client)
    docPushWithOptions(client, runtime)
}

func docPush(client *ha3engine.Client) {
    pushDocumentsRequestModel := &ha3engine.PushDocumentsRequestModel{}
    // # The name of the data source from which you want to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side pane, choose Configuration Center > Data Source. You can view the data source name on the Data Source page.
    dataSourceName := "<The name of the data source>"
    keyField := "id"
    
    a := [1000]int{}
    b := [10]int{}
    for x := range a {
        array := []map[string]interface{}{
        }
        for j := range b {
            filed := map[string]interface{}{
                "fields": map[string]interface{}{
                    "id":          tea.ToString(x*100) + tea.ToString(j),
                    "fb_boolean":  tea.BoolValue(nil),
                    "fb_datetime": "2167747200000",
                    "fb_string":   "409a6b18-a10b-409e-af91-07121c45d899",
                },
                "cmd": tea.String("add"),
            }
            array = append(array, filed)
        }
        
        pushDocumentsRequestModel.SetBody(array)
        
        // Call the method for sending a request.
        response1, _requestErr1 := client.PushDocuments(tea.String(dataSourceName), tea.String(keyField), pushDocumentsRequestModel)
        
        // If an error occurs when the request is sent, return _requestErr and the error information.
        if _requestErr1 != nil {
            fmt.Println(_requestErr1)
            return
        }
        
        // Display the response if no error occurs.
        fmt.Println(response1)
        
    }
    
}

func docPushWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    pushDocumentsRequestModel := &ha3engine.PushDocumentsRequestModel{}
    dataSourceName := "{InstanceId}_odps"
    keyField := "id"
    
    a := [1000]int{}
    b := [10]int{}
    for x := range a {
        array := []map[string]interface{}{
        }
        for j := range b {
            filed := map[string]interface{}{
                "fields": map[string]interface{}{
                    "id":          tea.ToString(x*100) + tea.ToString(j),
                    "fb_boolean":  tea.BoolValue(nil),
                    "fb_datetime": "2167747200000",
                    "fb_string":   "409a6b18-a10b-409e-af91-07121c45d899",
                },
                "cmd": tea.String("add"),
            }
            array = append(array, filed)
        }
        
        pushDocumentsRequestModel.SetBody(array)
        
        // Call the method for sending a request.
        response1, _requestErr1 := client.PushDocumentsWithOptions(tea.String(dataSourceName), tea.String(keyField), pushDocumentsRequestModel, runtime)
        
        // If an error occurs when the request is sent, return _requestErr and the error information.
        if _requestErr1 != nil {
            fmt.Println(_requestErr1)
            return
        }
        
        // Display the response if no error occurs.
        fmt.Println(response1)
        
    }
}