Documentation
¶
Index ¶
- func EscapeCondValue(s string, isFieldVal ...bool) string
- func EscapeFieldValue(s string) string
- func EscapeTagTailBackslash(s string) string
- func EscapeTagValue(s string) string
- func FormatMeasurement(s string) string
- func Quote(s string) string
- func QuoteIfNeed(s string) string
- func SingleQuote(s string) string
- func UnescapeQueryResultValue(s string) string
- type Client
- func (this *Client) AlterRetentionPolicy(db string, rp *RetentionPolicy) error
- func (this *Client) Close()
- func (this *Client) CreateDatabase(db string) (bool, error)
- func (this *Client) CreateRetentionPolicy(db string, rp *RetentionPolicy) (bool, error)
- func (this *Client) DropDatabase(db string) error
- func (this *Client) DropMeasurement(db, measurement string) error
- func (this *Client) DropRetentionPolicy(db, rp string) error
- func (this *Client) Flush()
- func (this *Client) NewQuery() *Query
- func (this *Client) Printf(format string, args ...interface{})
- func (this *Client) RawQuery(db, rp, sql string) ([]*Series, error)
- func (this *Client) RawWrite(db, rp string, lines []string) error
- func (this *Client) ResetWriteStats()
- func (this *Client) ShowDatabases() ([]string, error)
- func (this *Client) ShowFieldKeys(db, measurement string) ([]string, error)
- func (this *Client) ShowMeasurements(db string) ([]string, error)
- func (this *Client) ShowRetentionPolicies(db string) ([]*RetentionPolicy, error)
- func (this *Client) ShowSeries(db, measurement string) ([]string, error)
- func (this *Client) ShowTagKeys(db, measurement string) ([]string, error)
- func (this *Client) Write(db, rp string, points []*Point, immediate bool) error
- func (this *Client) WriteStats() WriteStats
- type ICond
- func And(arr ...ICond) ICond
- func Between(col string, less, more interface{}) ICond
- func BetweenOpen(col string, less, more interface{}) ICond
- func BetweenOpenL(col string, less, more interface{}) ICond
- func BetweenOpenR(col string, less, more interface{}) ICond
- func Expr(col, opr string, val interface{}) ICond
- func In(col string, values ...interface{}) ICond
- func Match(col, pattern string) ICond
- func NewCond() ICond
- func NotIn(col string, values ...interface{}) ICond
- func NotMatch(col, pattern string) ICond
- func Or(arr ...ICond) ICond
- func RawExpr(expr string) ICond
- type Option
- func WithCredential(username, passOrToken string) Option
- func WithFlushInterval(interval time.Duration) Option
- func WithFlushSize(size int) Option
- func WithGroupSize(size int) Option
- func WithHTTPClient(hc *http.Client) Option
- func WithHTTPTimeout(d time.Duration) Option
- func WithInsecureSkipVerify(skip bool) Option
- func WithLogger(logger logs.Logger) Option
- func WithLoggerLevel(level int) Option
- func WithMaxIdleConnsPerHost(n int) Option
- func WithQueryEpoch(epoch string) Option
- func WithTLSConfig(tlsConf *tls.Config) Option
- func WithWritePoolSize(n int) Option
- func WithWritePrecision(precision string) Option
- func WithWriteSortTagKey(sort bool) Option
- type Point
- type Query
- func (this *Query) Asc(field string) *Query
- func (this *Query) Desc(field string) *Query
- func (this *Query) Do() ([]*Series, error)
- func (this *Query) From(db, rp string, measurements ...string) *Query
- func (this *Query) GroupBy(groupBy string) *Query
- func (this *Query) Limit(limit int) *Query
- func (this *Query) OrderBy(orderBy string) *Query
- func (this *Query) Select(fields ...string) *Query
- func (this *Query) String() string
- func (this *Query) TimeZone(tz string) *Query
- func (this *Query) Where(cond ICond) *Query
- type RetentionPolicy
- type Series
- type WriteStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EscapeCondValue ¶
EscapeCondValue 查询数据时,需要将条件值中部分字符进行转义
由于tag和field写入时转义稍有差别,所以查询条件也需要区分,通常都是用tag作为条件,如果是field,需要额外指定 isFieldVal=true https://docs.influxdata.com/influxdb/v1/query_language/data_exploration/#string-literals
func EscapeFieldValue ¶
EscapeFieldValue 写入数据时,需要将field值中不合规的字符进行转义
func EscapeTagTailBackslash ¶
EscapeTagTailBackslash 写入数据时,tag尾部的反斜杠会对后面的空格进行转义,导致行协议无法正确解析,所以需要额外处理,查询时也需要对齐
func EscapeTagValue ¶
EscapeTagValue 写入数据时,需要将tag值中不合规的字符进行转义
行协议:https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_reference
func FormatMeasurement ¶
FormatMeasurement 统一measurement命名规范,避免特殊字符
func QuoteIfNeed ¶
QuoteIfNeed 如有必要,给字符串加上双引号,否则返回原字符串
https://docs.influxdata.com/influxdb/v1/query_language/explore-data/#quoting
func SingleQuote ¶
SingleQuote 给查询条件值加上单引号
https://docs.influxdata.com/influxdb/v1/troubleshooting/frequently-asked-questions/#when-should-i-single-quote-and-when-should-i-double-quote-in-queries
func UnescapeQueryResultValue ¶
UnescapeQueryResultValue 还原查询结果中tag、field值的转义
写入时(RawWrite除外)tag、field值转义时都移除了前后空格,唯一特殊情况是以反斜杠结尾的tag,特殊追加了一个空格,所以查询结果中需要去掉,以保持一致
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) AlterRetentionPolicy ¶
func (this *Client) AlterRetentionPolicy(db string, rp *RetentionPolicy) error
func (*Client) Close ¶
func (this *Client) Close()
Close 关闭客户端,释放资源
!!!务必在程序退出时调用,否则可能会导致数据丢失!!! 幂等:重复调用安全,仅首次生效
func (*Client) CreateRetentionPolicy ¶
func (this *Client) CreateRetentionPolicy(db string, rp *RetentionPolicy) (bool, error)
func (*Client) DropDatabase ¶
func (*Client) DropMeasurement ¶
func (*Client) DropRetentionPolicy ¶
func (*Client) RawQuery ¶
RawQuery 执行查询语句
- db: 数据库名,也可以在sql语句中指定数据库
- rp: 保留策略名,为空时使用默认
- sql: 查询语句,语法参考:https://docs.influxdata.com/influxdb/v1/query_language/explore-data/#the-basic-select-statement
func (*Client) RawWrite ¶
RawWrite 执行写入操作
- db: 数据库名,必须指定
- rp: 保留策略名,为空时使用默认
- lines: 符合influxdb行协议的数据,需自行处理转义
func (*Client) ResetWriteStats ¶
func (this *Client) ResetWriteStats()
ResetWriteStats 清零写入统计,配合周期性采集使用
func (*Client) ShowDatabases ¶
func (*Client) ShowFieldKeys ¶
func (*Client) ShowRetentionPolicies ¶
func (this *Client) ShowRetentionPolicies(db string) ([]*RetentionPolicy, error)
func (*Client) ShowSeries ¶
func (*Client) ShowTagKeys ¶
type ICond ¶
func BetweenOpen ¶
func BetweenOpenL ¶
func BetweenOpenR ¶
func Expr ¶
Expr 表达式
- col: 列名,支持时间索引列、标签列
- opr: 操作符,支持 =, !=, >, <, >=, <=, <>, =~, !~
- val: 值,支持数值、字符串
func Match ¶
Match 模糊匹配
https://docs.influxdata.com/influxdb/v1/query_language/explore-data/#regular-expressions !!! 注意:正则表达式匹配性能较差,尽量避免使用 !!!
type Option ¶
type Option func(*Client)
func WithCredential ¶
func WithFlushInterval ¶
WithFlushInterval 设置异步写入数据的时间间隔
func WithFlushSize ¶
WithFlushSize 设置异步写入数据的行数上限, 最大不超过 bucketSize
func WithHTTPClient ¶
WithHTTPClient 使用自定义的 http.Client,会整体替换默认的 transport/超时/TLS 配置。
适合需要完全掌控连接池、代理、TLS 等的场景;传入 nil 时忽略。
func WithHTTPTimeout ¶
WithHTTPTimeout 设置 HTTP 请求超时,默认 1m。
仅对默认 transport 生效;若已通过 WithHTTPClient 替换整个 client,应直接在其上设置 Timeout。
func WithInsecureSkipVerify ¶
WithInsecureSkipVerify 设置是否跳过 TLS 证书校验,默认跳过(true)。
生产环境建议传 false 启用严格校验。仅对默认 transport 生效;若已通过 WithHTTPClient 替换整个 client, 应直接在其 transport 的 TLSClientConfig 上设置 InsecureSkipVerify。
func WithLogger ¶
func WithLoggerLevel ¶
func WithMaxIdleConnsPerHost ¶
WithMaxIdleConnsPerHost 设置每个 host 的最大空闲连接数,默认 100。
仅对默认 transport 生效;若已通过 WithHTTPClient 替换整个 client,应直接在其 transport 上设置。 若 transport 不是 *http.Transport,本 Option 静默失效。
func WithQueryEpoch ¶
WithQueryEpoch 设置查询返回的时间格式, 默认s, 可选值:ns, u, ms, s, m, h
influxdb默认返回的时间是RFC3339格式,如果需要返回时间戳,需要通过epoch参数指定
func WithTLSConfig ¶
WithTLSConfig 自定义 TLS 配置。默认会跳过证书校验,需要启用严格校验时传入对应的 *tls.Config。
仅对默认 transport 生效;若已通过 WithHTTPClient 替换整个 client,应直接在其 transport 上设置 TLSClientConfig。
func WithWritePrecision ¶
WithWritePrecision 设置写入数据的时间精度, 默认s, 可选值:ns, u, ms, s, m, h
func WithWriteSortTagKey ¶
type Point ¶
type Point struct {
Measurement string // 表名,必须指定
Tags map[string]interface{} // 标签
Fields map[string]interface{} // 字段
Time int64 // 时间戳,不传时数据库会自动写入当前时间
}
Point 数据点
注意:写入精度默认为秒,可以通过 WithWritePrecision 设置
func (*Point) ToLineData ¶
ToLineData 转换为行协议数据
行协议:<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
type RetentionPolicy ¶
type Series ¶
type Series struct {
Name string `json:"name,omitempty"` // 表名,多表查询时有用
Tags map[string]string `json:"tags,omitempty"`
Columns []string `json:"columns"`
Values [][]interface{} `json:"values"`
}
func (*Series) ToStringObjectMap ¶
func (this *Series) ToStringObjectMap() []mapUtil.StringObjectMap
ToStringObjectMap converts the Series to a slice of mapUtil.StringObjectMap.
[
{column1: value1, column2: value2, ...}, // row 1
{column1: value1, column2: value2, ...}, // row 2
...
]
type WriteStats ¶
type WriteStats struct {
Total uint64 // 写入批次数(含成功与失败)
Fail uint64 // 写入失败批次数(HTTP 错误)
Drop uint64 // 因写协程池满被丢弃的批次数
}
WriteStats 写入统计
内部作为常驻原子计数器使用,外部通过 Client.WriteStats() 获取只读快照
func (WriteStats) String ¶
func (s WriteStats) String() string
String 以值接收器实现 Stringer,使得 WriteStats 值(如 Client.WriteStats() 返回值) 在 fmt 默认格式化时也会调用本方法;调用方应优先传入 Snapshot 副本,避免对常驻计数器直接调用