GithubHelp home page GithubHelp logo

setriones / goots Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brodic/goots

0.0 2.0 0.0 1.87 MB

Aliyun OTS(Open Table Service) golang SDK.

Home Page: http://ots.aliyun.com/

License: MIT License

Go 97.30% Protocol Buffer 2.70%

goots's Introduction

goots

![Gitter](https://badges.gitter.im/Join Chat.svg)

Aliyun OTS(Open Table Service) golang SDK.

wercker status

Build Status GoDoc

Build Status Go Walker

Support API

Install

$ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
$ go get github.com/GiterLab/goots

Usage

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
	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.

Links

License

This project is under the MIT License. See the LICENSE file for the full license text.

goots's People

Contributors

gitter-badger avatar hui avatar tobyzxj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.