![Gitter](https://badges.gitter.im/Join Chat.svg)
Aliyun OTS (Open Table Service) golang SDK.
-
OTS现更名为表格存储(Table Store)
-
此 golang SDK基于 阿里云表格存储服务 API构架,API兼容 Python SDK
-
阿里云表格存储是构建在阿里云飞天分布式系统之上的NoSQL数据存储服务,提供海量结构化数据的存储和实时访问
Tips: 使用OTS前,请参考下OTS使用限制项。
- Table
- CreateTable ☑
- DeleteTable ☑
- ListTable ☑
- UpdateTable ☑
- DescribeTable ☑
- SingleRow
- BatchRow
- BatchGetRow ☑
- BatchWriteRow ☑
- GetRange ☑
XGetRange
$ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
$ go get github.com/GiterLab/goots
set ENV first
OTS_TEST_ACCESS_KEY_ID=<AK_ID>
OTS_TEST_ACCESS_KEY_SECRET=<AK_SECRET>
OTS_TEST_INSTANCE=<ots-instance>
OTS_TEST_HOST=<ots.aliyuncs.com>
OTS_TEST_REGION=<cn-shanghai>
run
go test -v -x -tags=unittest ./
package main
import (
"fmt"
"os"
ots2 "github.com/GiterLab/goots"
. "github.com/GiterLab/goots/otstype" // 为了代码干净
)
// modify it to yours
const (
ENDPOINT = "your_instance_address"
ACCESSID = "your_accessid"
ACCESSKEY = "your_accesskey"
INSTANCENAME = "your_instance_name"
)
func main() {
// set running environment
ots2.OTSDebugEnable = true
ots2.OTSLoggerEnable = true
ots2.OTSErrorPanicMode = true // 默认为开启,如果不喜欢panic则设置此为false
fmt.Println("Test goots start ...")
// 用户可以使用 ots2.NewWithRetryPolicy 来创建带自定义重试策略的 ots_client 对象
// 用户可以参考 DefaultRetryPolicy 的代码来自定义重试策略
ots_client, err := ots2.New(ENDPOINT, ACCESSID, ACCESSKEY, INSTANCENAME)
if err != nil {
fmt.Println(err)
}
// delete a table
ots_err := ots_client.DeleteTable("myTable")
if ots_err != nil {
fmt.Println(ots_err)
// os.Exit(1)
}
fmt.Println("表已删除")
// create a table
// 注意:OTS是按设置的ReservedThroughput计量收费,即使没有读写也会产生费用。
table_meta := &OTSTableMeta{
TableName: "myTable",
SchemaOfPrimaryKey: OTSSchemaOfPrimaryKey{
{K: "gid", V: "INTEGER"},
{K: "uid", V: "INTEGER"},
},
}
// 容量型实例: 只能设置为0
// 高性能实例: 可设置为其他值(0-5000)
reserved_throughput := &OTSReservedThroughput{
OTSCapacityUnit{0, 0},
}
ots_err = ots_client.CreateTable(table_meta, reserved_throughput)
if ots_err != nil {
fmt.Println(ots_err)
os.Exit(1)
}
fmt.Println("表已创建")
// list tables
list_tables, ots_err := ots_client.ListTable()
if ots_err != nil {
fmt.Println(ots_err)
os.Exit(1)
}
fmt.Println("表的列表如下:")
fmt.Println("list_tables:", list_tables.TableNames)
// insert a row
primary_key := &OTSPrimaryKey{
"gid": 1,
"uid": 101,
}
attribute_columns := &OTSAttribute{
"name": "张三",
"mobile": 111111111,
"address": "中国A地",
"age": 20,
}
condition := OTSCondition_EXPECT_NOT_EXIST
put_row_response, ots_err := ots_client.PutRow("myTable", condition, primary_key, attribute_columns)
if ots_err != nil {
fmt.Println(ots_err)
os.Exit(1)
}
fmt.Println("成功插入数据,消耗的写CapacityUnit为:", put_row_response.GetWriteConsumed())
// get a row
primary_key = &OTSPrimaryKey{
"gid": 1,
"uid": 101,
}
columns_to_get := &OTSColumnsToGet{
"name", "address", "age",
}
// columns_to_get = nil // read all
get_row_response, ots_err := ots_client.GetRow("myTable", primary_key, columns_to_get)
if ots_err != nil {
fmt.Println(ots_err)
os.Exit(1)
}
fmt.Println("成功读取数据,消耗的读CapacityUnit为:", get_row_response.GetReadConsumed())
if get_row_response.Row != nil {
if attribute_columns := get_row_response.Row.GetAttributeColumns(); attribute_columns != nil {
fmt.Println("name信息:", attribute_columns.Get("name"))
fmt.Println("address信息:", attribute_columns.Get("address"))
fmt.Println("age信息:", attribute_columns.Get("age"))
fmt.Println("mobile信息:", attribute_columns.Get("mobile"))
} else {
fmt.Println("未查询到数据")
}
} else {
fmt.Println("未查询到数据")
}
}
More examples, please see example/interfaces.go.
This project is under the MIT License. See the LICENSE file for the full license text.