Golang中如何将ScyllaDB时间戳反序列化为*string类型

Golang中如何将ScyllaDB时间戳反序列化为string类型 我在ScyllaDB中存储了一些时间戳,但在检索时显示错误“无法将时间戳反序列化为string”。

我正在创建一个待办事项API,当从数据库检索待办事项时,它返回错误:“无法将时间戳反序列化为*string”。包含createdAtupdatedAt字段的待办事项结构体是time.Time类型。请帮助我如何解决这个问题。

以下是待办事项结构体类型:

type Todo struct {
    ID          gocql.UUID `json:"id"`
    UserID    string		`json:"user_id"`
    Title       string		`json:"title"`
    Description string		`json:"description"`
    Status      string		`json:"status"`
    CreatedAt   time.Time	`json:"created_at"`
    UpdatedAt   time.Time	`json:"updated_at"`
}

以下是处理程序:

func GetAllTodos(user_id string) ([]*Todo, error) {
    var todos []*Todo

    query := `SELECT * FROM todo WHERE user_id = ? ALLOW FILTERING`
    iter := Session.Query(query, user_id).Iter()

    var todo Todo
	var createdAtTime, updatedAtTime time.Time
    for iter.Scan(&todo.ID, &todo.UserID, &todo.Title, &todo.Description, &todo.Status, &ctodo.CreatedAt,&todo.UpdatedAt) {      
        todos = append(todos, &todo)
        todo = Todo{}
    }

    if err := iter.Close(); err != nil {
        return nil, err
    }
    return todos, nil
}

更多关于Golang中如何将ScyllaDB时间戳反序列化为*string类型的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang中如何将ScyllaDB时间戳反序列化为*string类型的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在ScyllaDB/Cassandra中处理时间戳时,需要确保数据库列类型与Go结构体类型正确匹配。错误表明数据库返回的时间戳类型无法直接反序列化为*string,但你的结构体使用的是time.Time类型。这通常是因为gocql驱动期望扫描到正确的类型。

以下是修正后的代码:

import (
    "time"
    "github.com/gocql/gocql"
)

type Todo struct {
    ID          gocql.UUID `json:"id"`
    UserID      string     `json:"user_id"`
    Title       string     `json:"title"`
    Description string     `json:"description"`
    Status      string     `json:"status"`
    CreatedAt   time.Time  `json:"created_at"`
    UpdatedAt   time.Time  `json:"updated_at"`
}

func GetAllTodos(user_id string) ([]*Todo, error) {
    var todos []*Todo
    
    query := `SELECT id, user_id, title, description, status, created_at, updated_at FROM todo WHERE user_id = ? ALLOW FILTERING`
    iter := Session.Query(query, user_id).Iter()
    
    for {
        todo := &Todo{}
        // 直接扫描到结构体字段
        if !iter.Scan(&todo.ID, &todo.UserID, &todo.Title, &todo.Description, 
                     &todo.Status, &todo.CreatedAt, &todo.UpdatedAt) {
            break
        }
        todos = append(todos, todo)
    }
    
    if err := iter.Close(); err != nil {
        return nil, err
    }
    return todos, nil
}

如果问题仍然存在,可能是数据库列类型不匹配。检查ScyllaDB中created_atupdated_at列的数据类型:

DESCRIBE TABLE todo;

确保列类型为timestamp。如果列类型是其他类型(如textvarchar),需要修改结构体或转换数据:

// 如果数据库存储的是字符串时间戳
type Todo struct {
    // ... 其他字段
    CreatedAt   string `json:"created_at"`
    UpdatedAt   string `json:"updated_at"`
}

// 或者使用自定义类型处理
type Todo struct {
    // ... 其他字段
    CreatedAt   gocql.UUID `json:"created_at"`  // 如果使用timeuuid
    UpdatedAt   gocql.UUID `json:"updated_at"`
}

如果需要从字符串解析时间:

func GetAllTodos(user_id string) ([]*Todo, error) {
    var todos []*Todo
    
    query := `SELECT id, user_id, title, description, status, created_at, updated_at FROM todo WHERE user_id = ? ALLOW FILTERING`
    iter := Session.Query(query, user_id).Iter()
    
    var id gocql.UUID
    var userID, title, description, status string
    var createdAtStr, updatedAtStr string
    
    for iter.Scan(&id, &userID, &title, &description, &status, &createdAtStr, &updatedAtStr) {
        createdAt, _ := time.Parse(time.RFC3339, createdAtStr)
        updatedAt, _ := time.Parse(time.RFC3339, updatedAtStr)
        
        todos = append(todos, &Todo{
            ID:          id,
            UserID:      userID,
            Title:       title,
            Description: description,
            Status:      status,
            CreatedAt:   createdAt,
            UpdatedAt:   updatedAt,
        })
    }
    
    if err := iter.Close(); err != nil {
        return nil, err
    }
    return todos, nil
}

确保ScyllaDB中的时间戳列使用正确的数据类型,并且gocql驱动版本与ScyllaDB兼容。

回到顶部