结构化数据 Unstructed Data

数据可以分为结构化数据和非结构化数据。

  1. 结构化数据:预先知道结构的数据,例如Json

    1. {
    2. "id": 1,
    3. "name": "kylinxiang"
    4. }
  2. 处理结构化数据,需要一个对应的结构体 ```go package main

import ( “encoding/json” “fmt” )

// Student stores student info type Student struct { ID int Name string }

func main() { s := {"id": 1, "name": "kylinxiang"} var student Student err := json.Unmarshal([]byte(s), &student) if err != nil { fmt.Println(err.Error()) } fmt.Printf(“%v”, student) }

  1. <a name="eL9cY"></a>
  2. ## 非结构化数据 Unstructed Data
  3. 非结构化数据:无法预知数据类型或属性名称,无法通过构建预定的struct数据结构来序列化和反序列化数据。例如:
  4. ```json
  5. {
  6. "id": 1,
  7. "name": "kylinxiang",
  8. "description": ... //无法预知其数据类型,所以不能处理
  9. }

因为Go语言是强类型语言,它需要预先知道数据类型,所以在处理JSON数据时不如动态语言便捷。
当无法预知数据结构类型和属性名称时,可以使用如下的结构来解决问题:

  1. var result map[string]interface{}

每个字符串对应一个JSON属性,其映射interface{}类型对应值,可以是任意类型。
使用interface{}字段时,通过Go语言类型断言的方式进行类型转换。

  1. package main
  2. import "fmt"
  3. func main() {
  4. result := make(map[string]interface{})
  5. result["description"] = "kylinxiang"
  6. if description, ok := result["description"].(string); ok {
  7. fmt.Println(description)
  8. }
  9. }

Kubernetes非结构化数据处理

代码路径:vendor/k8s.io/apimachinery/pkg/runtime/interfaces.go

  1. // Unstructured objects store values as map[string]interface{}, with only values that can be serialized
  2. // to JSON allowed.
  3. type Unstructured interface {
  4. Object
  5. // NewEmptyInstance returns a new instance of the concrete type containing only kind/apiVersion and no other data.
  6. // This should be called instead of reflect.New() for unstructured types because the go type alone does not preserve kind/apiVersion info.
  7. NewEmptyInstance() Unstructured
  8. // UnstructuredContent returns a non-nil map with this object's contents. Values may be
  9. // []interface{}, map[string]interface{}, or any primitive type. Contents are typically serialized to
  10. // and from JSON. SetUnstructuredContent should be used to mutate the contents.
  11. UnstructuredContent() map[string]interface{}
  12. // SetUnstructuredContent updates the object content to match the provided map.
  13. SetUnstructuredContent(map[string]interface{})
  14. // IsList returns true if this type is a list or matches the list convention - has an array called "items".
  15. IsList() bool
  16. // EachListItem should pass a single item out of the list as an Object to the provided function. Any
  17. // error should terminate the iteration. If IsList() returns false, this method should return an error
  18. // instead of calling the provided function.
  19. EachListItem(func(Object) error) error
  20. }
  21. // Object interface must be supported by all API types registered with Scheme. Since objects in a scheme are
  22. // expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
  23. // serializers to set the kind, version, and group the object is represented as. An Object may choose
  24. // to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
  25. type Object interface {
  26. GetObjectKind() schema.ObjectKind
  27. DeepCopyObject() Object
  28. }

代码路径:vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructed/unstructed.go

  1. type Unstructured struct {
  2. // Object is a JSON compatible map with string, float, int, bool, []interface{}, or
  3. // map[string]interface{}
  4. // children.
  5. Object map[string]interface{}
  6. }

上述代码中,Kubernetes非结构化数据通过map[string]interface{}表达,并提供接口。在client-go编程式交互的DynamicClient内部,实现了Unstructed类型,用于处理非结构化数据。