Last active
September 28, 2015 18:34
-
-
Save lukebakken/df5ef740c82997957cba to your computer and use it in GitHub Desktop.
Riak - Go client examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func ex1_ping() { | |
| client := BuildClient() | |
| defer client.Stop() | |
| success, perr := client.Ping() | |
| CheckErr(perr) | |
| if actual, expected := success, true; actual != expected { | |
| ErrLog.Printf("[ERROR] got %v, expected %v", actual, expected) | |
| } else { | |
| Log.Printf("[INFO] ping result %v", success) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| riak "github.com/basho/riak-go-client" | |
| ) | |
| func ex2_simple_app() { | |
| client := BuildClient() | |
| defer client.Stop() | |
| monty_python := []string{ | |
| "Graham Chapman", "Eric Idle", | |
| "Terry Gilliam", "Terry Jones", | |
| "John Cleese", "Michael Palin", | |
| } | |
| j, jerr := json.Marshal(monty_python) | |
| CheckErr(jerr) | |
| object := &riak.Object{ | |
| ContentType: "application/json", | |
| Value: j, | |
| } | |
| var cmd riak.Command | |
| var err error | |
| cmd, err = riak.NewStoreValueCommandBuilder(). | |
| WithBucket("ex2"). | |
| WithKey("Monty Python"). | |
| WithContent(object). | |
| Build() | |
| CheckErr(err) | |
| err = client.Execute(cmd) | |
| CheckErr(err) | |
| cmd, err = riak.NewFetchValueCommandBuilder(). | |
| WithBucket("ex2"). | |
| WithKey("Monty Python"). | |
| Build() | |
| CheckErr(err) | |
| err = client.Execute(cmd) | |
| CheckErr(err) | |
| var fetched_pythons []string | |
| fcmd := cmd.(*riak.FetchValueCommand) | |
| val := fcmd.Response.Values[0].Value | |
| err = json.Unmarshal(val, &fetched_pythons) | |
| CheckErr(err) | |
| JsonDump(fetched_pythons) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func ex3_enable_search() { | |
| client := BuildClient() | |
| defer client.Stop() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func main() { | |
| ex1_ping() | |
| ex2_simple_app() | |
| ex3_enable_search() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| setlocal | |
| C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "%~dp0\make.ps1" %* | |
| exit /b %ERRORLEVEL% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Powershell script to run Intro to Riak KV for Go on Windows | |
| .DESCRIPTION | |
| This script will run 'go' correctly depending on parameters passed to this script. | |
| .PARAMETER Target | |
| Target to build. Can be one of the following: | |
| * Format - run *.go files through 'go fmt' | |
| * Run - Run all examples | |
| .PARAMETER Verbose | |
| Use to increase verbosity. | |
| .EXAMPLE | |
| C:\Users\Bashoman> cd $env:GOPATH\src | |
| C:\Users\Bashoman> git clone -o intro-riak-kv git@gist.github.com:df5ef740c82997957cba.git | |
| C:\Users\Bashoman> cd intro-riak-kv | |
| C:\Users\Bashoman\go\src\intro-riak-kv>.\make.ps1 -Target Run -Verbose | |
| .NOTES | |
| Author: Luke Bakken | |
| Date: August 26, 2015 | |
| #> | |
| [CmdletBinding()] | |
| Param( | |
| [Parameter(Mandatory=$False, Position=0)] | |
| [ValidateSet('Format', 'Run', IgnoreCase = $True)] | |
| [string]$Target = 'Run' | |
| ) | |
| Set-StrictMode -Version Latest | |
| $package = 'intro-riak-kv' | |
| $IsDebug = $DebugPreference -ne 'SilentlyContinue' | |
| $IsVerbose = $VerbosePreference -ne 'SilentlyContinue' | |
| # Note: | |
| # Set to Continue to see DEBUG messages | |
| if ($IsVerbose) { | |
| $DebugPreference = 'Continue' | |
| } | |
| trap | |
| { | |
| Write-Error -ErrorRecord $_ | |
| exit 1 | |
| } | |
| function Get-ScriptPath { | |
| $scriptDir = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value } | |
| if (!$scriptDir) { | |
| if ($MyInvocation.MyCommand.Path) { | |
| $scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent | |
| } | |
| } | |
| if (!$scriptDir) { | |
| if ($ExecutionContext.SessionState.Module.Path) { | |
| $scriptDir = Split-Path (Split-Path $ExecutionContext.SessionState.Module.Path) | |
| } | |
| } | |
| if (!$scriptDir) { | |
| $scriptDir = $PWD | |
| } | |
| return $scriptDir | |
| } | |
| function Execute($cmd, $argz) { | |
| Write-Verbose "$cmd $argz" | |
| & $cmd $argz | |
| if ($? -ne $True) { | |
| throw "'$cmd $argz' failed: $LastExitCode" | |
| } | |
| Write-Debug "'$cmd $argz' exit code: $LastExitCode" | |
| } | |
| function Do-Format { | |
| $script_path = Get-ScriptPath | |
| $cmd = 'gofmt' | |
| $argz = '-s', '-w', $script_path | |
| Execute $cmd $argz | |
| } | |
| function Do-InstallDeps { | |
| $cmd = 'go.exe' | |
| $argz = 'get', '-t', './...' | |
| Execute $cmd $argz | |
| } | |
| function Do-Vet { | |
| $cmd = 'go.exe' | |
| $script_path = Get-ScriptPath | |
| $argz = 'tool', 'vet', '-shadow=true', '-shadowstrict=true', $script_path | |
| Execute $cmd $argz | |
| $argz = 'vet', "$package/..." | |
| Execute $cmd $argz | |
| } | |
| function Do-Run { | |
| $cmd = 'go.exe' | |
| $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './util.go' | |
| Execute $cmd $argz | |
| } | |
| Write-Debug "Target: $Target" | |
| switch ($Target) | |
| { | |
| 'Format' { Do-Format } | |
| 'Run' { Do-InstallDeps; Do-Vet; Do-Run; } | |
| default { throw "Unknown target: $Target" } | |
| } | |
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .PHONY: all install-deps lint run | |
| PROJDIR = $(realpath $(CURDIR)) | |
| all: install-deps lint run | |
| install-deps: | |
| go get -t ./... | |
| lint: install-deps | |
| go tool vet -shadow=true -shadowstrict=true $(PROJDIR) | |
| go vet intro-riak-kv/... | |
| fmt: | |
| gofmt -s -w $(PROJDIR) | |
| run: lint | |
| go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./util.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "os" | |
| "strconv" | |
| "time" | |
| riak "github.com/basho/riak-go-client" | |
| ) | |
| const iso8601format = "2006-01-02T15:04:05" | |
| var Log = log.New(os.Stdout, "", log.LstdFlags) | |
| var ErrLog = log.New(os.Stderr, "", log.LstdFlags) | |
| func Iso8601(t time.Time) string { | |
| return t.Format(iso8601format) | |
| } | |
| func CheckErr(err error) { | |
| if err != nil { | |
| ErrExit(err) | |
| } | |
| } | |
| func ErrExit(err error) { | |
| ErrLog.Println(err) | |
| os.Exit(1) | |
| } | |
| func GetRiakPort() uint16 { | |
| riakPort := uint16(10017) | |
| if portEnvVar := os.ExpandEnv("$RIAK_PORT"); portEnvVar != "" { | |
| if portNum, err := strconv.Atoi(portEnvVar); err == nil { | |
| riakPort = uint16(portNum) | |
| } | |
| } | |
| return riakPort | |
| } | |
| func GetRiakHost() string { | |
| riakHost := "riak-test" | |
| if hostEnvVar := os.ExpandEnv("$RIAK_HOST"); hostEnvVar != "" { | |
| riakHost = hostEnvVar | |
| } | |
| return riakHost | |
| } | |
| func GetRiakAddress() string { | |
| return fmt.Sprintf("%s:%d", GetRiakHost(), GetRiakPort()) | |
| } | |
| func GetRiakAddresses() []string { | |
| // Assume that a 4-node devrel is being used where PB port numbers | |
| // increase by 10 | |
| host := GetRiakHost() | |
| basePort := GetRiakPort() | |
| count := uint16(4) | |
| addrs := make([]string, count) | |
| for i := uint16(0); i < count; i++ { | |
| port := basePort + (i * 10) | |
| addrs[i] = fmt.Sprintf("%s:%d", host, port) | |
| } | |
| return addrs | |
| } | |
| func BuildClient() *riak.Client { | |
| opts := &riak.NewClientOptions{ | |
| RemoteAddresses: GetRiakAddresses(), | |
| } | |
| client, err := riak.NewClient(opts) | |
| CheckErr(err) | |
| return client | |
| } | |
| func JsonDump(val interface{}) { | |
| if val == nil { | |
| Log.Println("[JsonDump]", "NIL VAL") | |
| } else { | |
| if json, err := json.MarshalIndent(val, "", " "); err != nil { | |
| ErrLog.Printf("[JsonDump ERROR] %s", err.Error()) | |
| } else { | |
| Log.Println(string(json)) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment