github.com/erda-project/erda-infra@v1.0.9/providers/clickhouse/examples/main.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"time"
    22  
    23  	ck "github.com/ClickHouse/clickhouse-go/v2"
    24  
    25  	"github.com/erda-project/erda-infra/base/servicehub"
    26  	"github.com/erda-project/erda-infra/providers/clickhouse"
    27  )
    28  
    29  type provider struct {
    30  	Clickhouse clickhouse.Interface
    31  }
    32  
    33  type goco struct {
    34  	Timestamp time.Time `ch:"timestamp"`
    35  	Value     string    `ch:"value"`
    36  }
    37  
    38  func (p *provider) Init(ctx servicehub.Context) error {
    39  	// create table
    40  	err := p.Clickhouse.Client().Exec(context.Background(),
    41  		`create table if not exists example (
    42  					timestamp DateTime(9,'Asia/Shanghai'),
    43  					value String
    44  				) Engine = Memory`)
    45  
    46  	if err != nil {
    47  		fmt.Println(err)
    48  		return err
    49  	}
    50  
    51  	// batch insert
    52  	batch, err := p.Clickhouse.Client().
    53  		PrepareBatch(context.Background(), "insert into example")
    54  	if err != nil {
    55  		return err
    56  	}
    57  	err = batch.Append(time.Now(), "hello clickhouse")
    58  	if err != nil {
    59  		return err
    60  	}
    61  	err = batch.Send()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	writer := p.Clickhouse.NewWriter(&clickhouse.WriterOptions{
    67  		Encoder: func(data interface{}) (item *clickhouse.WriteItem, err error) {
    68  			return &clickhouse.WriteItem{
    69  				Table: "example",
    70  				Data:  data,
    71  			}, nil
    72  		},
    73  	})
    74  	_, err = writer.WriteN(
    75  		&goco{Timestamp: time.Now(), Value: "hi"},
    76  		&goco{Timestamp: time.Now(), Value: "hi"})
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	// query records
    82  	// struct fields must be exposed
    83  	// result fields binding is case-sensitive
    84  	var result []struct {
    85  		Timestamp time.Time `ch:"timestamp"`
    86  		Value     string    `ch:"value"`
    87  	}
    88  	err = p.Clickhouse.Client().
    89  		Select(context.Background(),
    90  			&result,
    91  			"select timestamp, value from example where timestamp < @time",
    92  			ck.Named("time", time.Now()),
    93  		)
    94  
    95  	if err != nil {
    96  		fmt.Println(err)
    97  		return err
    98  	}
    99  	fmt.Printf("query result: %+v", result)
   100  
   101  	// see https://github.com/ClickHouse/clickhouse-go/tree/v2/examples/native for more examples.
   102  	return nil
   103  }
   104  
   105  func init() {
   106  	servicehub.Register("example", &servicehub.Spec{
   107  		Services:     []string{"example"},
   108  		Dependencies: []string{"clickhouse"},
   109  		Description:  "example",
   110  		Creator: func() servicehub.Provider {
   111  			return &provider{}
   112  		},
   113  	})
   114  }
   115  
   116  func main() {
   117  	hub := servicehub.New()
   118  	hub.Run("examples", "", os.Args...)
   119  }