influx

package
v1.76.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package influx 提供 InfluxDB 时序数据库集成(对标 Spring Data InfluxDB 的 InfluxDBTemplate):写入/查询封装 + 原生客户端暴露。支持 InfluxDB 2.x (及 1.8+ 的兼容模式:ServerURL 指向 /api/v2 或 8086 根地址 + 1.x token)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetGlobal added in v1.36.0

func SetGlobal(client *Client)

SetGlobal 设置全局客户端(NewClient 成功后自动调用)。

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是 InfluxDB 模板实现。

func Get added in v1.36.0

func Get() *Client

Get 获取全局 InfluxDB 客户端;未初始化时返回 nil。

func NewClient

func NewClient(config Config) (*Client, error)

NewClient 创建 InfluxDB 客户端并验证连通性。

func (*Client) Buckets

func (c *Client) Buckets(ctx context.Context) ([]string, error)

Buckets 列出全部桶(管理用)。

func (*Client) Client

func (c *Client) Client() influxdb2.Client

Client 返回原生客户端。

func (*Client) Close

func (c *Client) Close()

Close 关闭客户端(释放连接池)。

func (*Client) EnsureBucket

func (c *Client) EnsureBucket(ctx context.Context, bucket string) error

EnsureBucket 确保桶存在(不存在则创建;管理/初始化用)。 需要 orgID:从组织名解析。

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping 健康检查。

func (*Client) Query

func (c *Client) Query(ctx context.Context, bucket, flux string) ([]map[string]interface{}, error)

Query 执行 Flux 查询。

func (*Client) QueryRaw

func (c *Client) QueryRaw(ctx context.Context, flux string) (string, error)

QueryRaw 执行 Flux 查询返回原始 CSV。

func (*Client) Write

func (c *Client) Write(ctx context.Context, bucket, measurement string,
	tags, fields map[string]interface{}, timestamp time.Time) error

Write 写入数据点。

func (*Client) WritePoint

func (c *Client) WritePoint(ctx context.Context, bucket string, point *write.Point) error

WritePoint 写入预构造点。

func (*Client) WriteRaw

func (c *Client) WriteRaw(ctx context.Context, bucket, lineProtocol string) error

WriteRaw 写入行协议。

type Config

type Config struct {
	// ServerURL 服务地址(如 http://127.0.0.1:8086)。
	ServerURL string
	// Token 访问令牌(InfluxDB 2.x Token;1.x 兼容模式用 user:password 的 base64)。
	Token string
	// Org 组织(InfluxDB 2.x 概念)。
	Org string
	// Bucket 默认桶(可空,操作时显式指定)。
	Bucket string
	// Timeout 请求超时(默认 10s)。
	Timeout time.Duration
}

Config InfluxDB 客户端配置。

type InfluxTemplate

type InfluxTemplate interface {
	// Ping 健康检查。
	Ping(ctx context.Context) error
	// Write 写入一个数据点(measurement + tags + fields + 时间戳)。
	// timestamp 为零值时使用当前时间。
	Write(ctx context.Context, bucket, measurement string,
		tags, fields map[string]interface{}, timestamp time.Time) error
	// WritePoint 写入预构造的数据点。
	WritePoint(ctx context.Context, bucket string, point *write.Point) error
	// WriteRaw 写入行协议(Line Protocol)原始字符串。
	WriteRaw(ctx context.Context, bucket, lineProtocol string) error
	// Query 执行 Flux 查询,返回记录列表(每行 map:列名 → 值)。
	Query(ctx context.Context, bucket, flux string) ([]map[string]interface{}, error)
	// QueryRaw 执行 Flux 查询,返回原始 CSV 文本。
	QueryRaw(ctx context.Context, flux string) (string, error)
	// Buckets 列出全部桶(管理用)。
	Buckets(ctx context.Context) ([]string, error)
	// Client 返回原生客户端(高级操作入口)。
	Client() influxdb2.Client
}

InfluxTemplate 高频操作层:写入(点/行协议)/查询(Flux)/健康检查 + 原生客户端。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL