登录
首页 >  Golang >  Go问答

使用golang和apache arrow,在datatype.go中根据指定的数据类型构建模式

来源:stackoverflow

时间:2024-02-08 10:54:22 236浏览 收藏

一分耕耘,一分收获!既然都打开这篇《使用golang和apache arrow,在datatype.go中根据指定的数据类型构建模式》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我正在学习 apache arrow,想要了解有关如何创建模式和箭头记录的更多信息。为此,我引用了一些材料,但到目前为止,所有这些材料都只是使用原始类型来构建如下所示的模式:`

schema := arrow.NewSchema(
    []arrow.Field{
        {Name: "f1-i32", Type: arrow.PrimitiveTypes.Int32},
        {Name: "f2-f64", Type: arrow.PrimitiveTypes.Float64},
    },
    nil,
)

我想要使用的 primitivetypes 中不存在一些数据类型。例如,我想使用bool或decimal128。我正在查看 golang 箭头库,发现文件 datatype.go ,其中包含我想要使用的所有可能的数据类型。 但这里的类型不是构建模式时所需的 datatype 类型。

所以,我有以下三个问题:

  1. 如果可能的话,如何使用 datatype.go 中的这些数据类型来构建我的架构?
  2. 如果我想使用小数类型,如何指定精度和小数位数?
  3. 使用扩展类型的示例。

正确答案


datatype.go 中定义的这些数据类型命名常量已用于创建您想要的新类型的一部分。其中一些是 type decimal128type structtype booleantype struct 如果您检查这些结构的 id 方法的源代码,它们返回在 datatype.go 中定义的常量,其名称与结构的名称相似。这些结构已经实现了 datatype 接口,这意味着您可以将它们分配给 arrow.field.type 因为该字段的类型是 datatype
我对他们的意思是:
bool 中定义的常量 datatype.godatatype_fixedwidth.go 中用作 type booleantype structid 方法的返回值。
func (t *booleantype) id() 类型 { return bool }
同样的事情也适用于 type decimal128type struct
func (*decimal128type) id() 类型 { return decimal128 }.

这些结构之一的方法显示它们正在实现 datatype 接口:

func (*decimal128type) bitwidth() int
func (t *decimal128type) fingerprint() string
func (*decimal128type) id() type
func (*decimal128type) name() string
func (t *decimal128type) string() string

这些方法适用于 type decimal128type struct
以及datatype接口的定义:

type datatype interface {
    id() type
    // name is name of the data type.
    name() string
    fingerprint() string
}

type booleantype struct 也实现了它。

因此,您可以将它们用于 type 字段:

type field struct {
    name     string   // field name
    type     datatype // the field's data type
    nullable bool     // fields can be nullable
    metadata metadata // the field's metadata, if any
}

示范性示例:

package main

import (
    "fmt"

    "github.com/apache/arrow/go/arrow"
)

func main() {
    booltype :=  &arrow.booleantype{}
    decimal128type := &arrow.decimal128type{precision: 1, scale: 1}

    schema := arrow.newschema(
        []arrow.field{
            {name: "f1-bool", type: booltype},
            {name: "f2-decimal128", type: decimal128type},
        },
        nil,
    )

    fmt.println(schema)
}

输出:

schema:
  fields: 2
    - f1-bool: type=bool
    - f2-decimal128: type=decimal(1, 1)

您可以在 文档
还有一些与扩展类型相关的东西。
但我不熟悉扩展类型,因此我无法展示它的示例。但如果你熟悉它,你就可以轻松解决它。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>