Aliyun OTS(Open Table Service) golang SDK.
- Table
- CreateTable ☑
- DeleteTable ☑
- ListTable ☑
- UpdateTable ☑
- DescribeTable ☑
- SingleRow
- GetRow ☑
- PutRow
- UpdateRow
- DeleteRow
- BatchRow
- BatchGetRow
- BatchWriteRow
- GetRange
- XGetRange
$ go get -u code.google.com/p/goprotobuf/{proto,protoc-gen-go}
$ go get github.com/GiterLab/goots
NOTE: If you can't get
goprotobuf
package (you known why),Please refer to gopm.io to download manually.
package main
import (
"fmt"
"os"
ots2 "github.com/GiterLab/goots"
"github.com/GiterLab/goots/log"
. "github.com/GiterLab/goots/otstype"
)
// modify it to yours
const (
ENDPOINT = "http://127.0.0.1:8800"
ACCESSID = "OTSMultiUser177_accessid"
ACCESSKEY = "OTSMultiUser177_accesskey"
INSTANCENAME = "TestInstance177"
)
func main() {
// set running environment
ots2.OTSDebugEnable = true
ots2.OTSLoggerEnable = true
log.OTSErrorPanicMode = true // 默认为开启,如果不喜欢panic则设置此为false
fmt.Println("Test goots start ...")
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
table_meta := &OTSTableMeta{
TableName: "myTable",
SchemaOfPrimaryKey: OTSSchemaOfPrimaryKey{
"gid": "INTEGER",
"uid": "INTEGER",
},
}
reserved_throughput := &OTSReservedThroughput{
OTSCapacityUnit{100, 100},
}
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
// 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.