feat: opensearch客户端初始化 feat: 索引模板接口 ai: 开发准则 chore: TDD流水线脚本
This commit is contained in:
109
main.go
Normal file
109
main.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"es-demo/client"
|
||||
"es-demo/config"
|
||||
"es-demo/operations/cluster"
|
||||
"es-demo/operations/index"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load configuration from .env file and environment variables
|
||||
if err := config.Load(".env"); err != nil {
|
||||
log.Fatalf("Failed to load configuration: %v", err)
|
||||
}
|
||||
|
||||
// Create client configuration
|
||||
cfg := &client.Config{
|
||||
Endpoint: config.Endpoint,
|
||||
Region: config.Region,
|
||||
AccessKey: config.AccessKey,
|
||||
SecretKey: config.SecretKey,
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
// Create OpenSearch client
|
||||
osClient, err := client.NewClient(cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create OpenSearch client: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Get cluster information
|
||||
info, err := cluster.GetInfo(ctx, osClient)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get cluster info: %v", err)
|
||||
}
|
||||
|
||||
// Display cluster information
|
||||
fmt.Printf("=== Cluster Information ===\n")
|
||||
fmt.Printf("Cluster Name: %s\n", info.ClusterName)
|
||||
fmt.Printf("Cluster UUID: %s\n", info.ClusterUUID)
|
||||
fmt.Printf("Version: %s\n", info.Version.Number)
|
||||
fmt.Printf("Lucene Version: %s\n", info.Version.LuceneVersion)
|
||||
fmt.Printf("Tagline: %s\n\n", info.Tagline)
|
||||
|
||||
// Example: Create an index template
|
||||
template := &index.Template{
|
||||
IndexPatterns: []string{"logs-*"},
|
||||
Settings: map[string]any{
|
||||
"number_of_shards": 1,
|
||||
"number_of_replicas": 1,
|
||||
},
|
||||
Mappings: map[string]any{
|
||||
"properties": map[string]any{
|
||||
"timestamp": map[string]any{
|
||||
"type": "date",
|
||||
},
|
||||
"message": map[string]any{
|
||||
"type": "text",
|
||||
},
|
||||
},
|
||||
},
|
||||
Priority: 100,
|
||||
}
|
||||
|
||||
fmt.Printf("=== Index Template Operations ===\n")
|
||||
|
||||
// Put template
|
||||
if err := index.PutTemplate(ctx, osClient, "logs-template", template); err != nil {
|
||||
log.Printf("Warning: Failed to create template: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("✓ Created template: logs-template\n")
|
||||
}
|
||||
|
||||
// Get template
|
||||
retrievedTemplate, err := index.GetTemplate(ctx, osClient, "logs-template")
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to get template: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("✓ Retrieved template: %+v\n", retrievedTemplate.IndexPatterns)
|
||||
}
|
||||
|
||||
// List templates
|
||||
templates, err := index.ListTemplates(ctx, osClient)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to list templates: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("✓ Total templates: %d\n", len(templates))
|
||||
for name := range templates {
|
||||
fmt.Printf(" - %s\n", name)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete template (commented out to avoid cleanup in demo)
|
||||
// if err := index.DeleteTemplate(ctx, osClient, "logs-template"); err != nil {
|
||||
// log.Printf("Warning: Failed to delete template: %v\n", err)
|
||||
// } else {
|
||||
// fmt.Printf("✓ Deleted template: logs-template\n")
|
||||
// }
|
||||
|
||||
fmt.Printf("\nDemo completed successfully!\n")
|
||||
}
|
||||
Reference in New Issue
Block a user