-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from janzenbupa/golang
Added a short page about connecting to dynamodb with golang.
- Loading branch information
Showing
4 changed files
with
250 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>janzenbupa.github.io</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
<body> | ||
<div class="nav-bar-header"> | ||
<h2 class="h2-title">software engineering</h2> | ||
<p></p> | ||
</div> | ||
<div class="nav-bar"> | ||
<ul> | ||
<li><a class="a-look" href="index.html">home</a></li> | ||
<li><a class="a-look" href="dynamodb-with-go.html">DynamoDb with Golang</a></li> | ||
</ul> | ||
</div> | ||
</div> | ||
|
||
<div class="p-center" style="padding-bottom: 20px;"> | ||
<H2 class="h2-title" style="color:#676798">Connecting to DynamoDb with Go</H2> | ||
<p>After creating a Go project and initializing the module name, you can begin to import the necessary | ||
packages required for connecting to DynamoDb. | ||
</p> | ||
<div style="text-align: center;"> | ||
<code class="codeblock" style="display: inline-block;text-align: left;width: 400px;margin-bottom: 20px;"> | ||
go get github.com/aws/aws-sdk-go/aws</br> | ||
go get github.com/aws/aws-sdk-go/aws/credentials</br> | ||
go get github.com/aws/aws-sdk-go/aws/session</br> | ||
go get github.com/aws/aws-sdk-go/service/dynamodb</br> | ||
</code> | ||
</div> | ||
|
||
<p>The next step after getting the required dependencies is to create the method that can connect to DynamoDb.</p> | ||
|
||
<div class="code-container"> | ||
<code class="code-block"> | ||
sess := session.Must(session.NewSession(&aws.Config{ | ||
Region: aws.String("us-east-1"), | ||
Credentials: credentials.NewStaticCredentials( | ||
accessKey, | ||
secretKey, | ||
""), | ||
})) | ||
|
||
svc := dynamodb.New(sess) | ||
return *svc | ||
</code> | ||
</div> | ||
|
||
<p>This creates a connection to DynamoDb using https. This session is created and can be used to read and write to your database.</p> | ||
<p>Now we are going to try writing to our data.</p> | ||
|
||
<div class="code-container"style="height: 300px;margin-bottom: 20px;"> | ||
<code class="code-block" style="height: 300px;"> | ||
func Write(data SomeDataStruct) { | ||
input := &dynamodb.PutItemInput{ | ||
TableName: aws.String("TableName"), | ||
Item: map[string]*dynamodb.AttributeValue{ | ||
"Property/Column": { | ||
S: aws.String(data.Property), | ||
}, | ||
"DifferentProperty": { | ||
S: aws.String(data.DifferentProperty), | ||
}, | ||
}, | ||
} | ||
_, err := svc.PutItem(input) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
</code> | ||
</div> | ||
|
||
<p>The code above will accept some data of type struct, for example, and map that data to properties in our database.</p> | ||
<p>The &dynamodb.PutItemInput pointer allows us to define a variable named input of the PutItemInput struct type, | ||
and allows us to set the Item field on that struct to our actual data that we need to save.</p> | ||
<p>Item field would be an example of our data structure in DynamoDb.</p> | ||
|
||
<p>Next, we may want to read the data that we have written to our database. The function below allows us to do that.</p> | ||
|
||
<div class="code-container"style="height: 300px;margin-bottom: 20px;"> | ||
<code class="code-block" style="height: 300px;width: 550px;"> | ||
func Read(limit int64) ([]map[string]*dynamodb.AttributeValue, error) { | ||
input := &dynamodb.ScanInput{ | ||
TableName: aws.String("TableName"), | ||
Limit: aws.Int64(limit), | ||
} | ||
|
||
result, err := svc.Scan(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
items := result.Items | ||
|
||
return items, nil | ||
} | ||
</code> | ||
</div> | ||
|
||
<p>Finally, we may just want to query a single item. To do that, all we have to do is create a pointer to a QueryInput struct, and define the attribute that we want to query on.</p> | ||
<div class="code-container"style="height: 350px;margin-bottom: 20px;"> | ||
<code class="code-block" style="height: 320px;width: 700px;"> | ||
func ReadByAttribute(attrVal string) ([]map[string]*dynamodb.AttributeValue, error) { | ||
|
||
input := &dynamodb.QueryInput{ | ||
TableName: aws.String("TableName"), | ||
KeyConditionExpression: aws.String("AttributeVal = :attrVal"), | ||
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ | ||
":attrVal": { | ||
S: aws.String(attrVal), | ||
}, | ||
}, | ||
} | ||
|
||
result, err := svc.Query(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result.Items, nil | ||
} | ||
</code> | ||
</div> | ||
<p>Our code accepts a parameter called attrVal, and queries our database based on a field named AttributeVal.</p> | ||
<p>It will run a query that searches for the key AttributeVal, where the value of that key is equal to the value of attrVal that we passed in. It then returns every instance of that data.</p> | ||
</div> | ||
</body> | ||
</html> | ||
|
||
<style> | ||
.p-center{ | ||
text-align: center; | ||
} | ||
|
||
.h2-title{ | ||
text-align: center; | ||
color: #e6e6e6; | ||
font-family: "Retina", Arial, Helvetica, sans-serif; | ||
} | ||
|
||
a{ | ||
font-family: "Retina", Arial, Helvetica, sans-serif; | ||
} | ||
|
||
a:link{ | ||
text-decoration: none; | ||
color: Violet; | ||
} | ||
|
||
a:visited{ | ||
text-decoration: none; | ||
color: Violet; | ||
} | ||
|
||
a:active{ | ||
text-decoration: none; | ||
color: Violet; | ||
} | ||
|
||
.nav-bar-header{ | ||
background-color: #1f1f2e/*SlateBlue*/; | ||
overflow: hidden; | ||
margin: 0px; | ||
border-bottom: 0.5px solid #676798/*LightSlateGray*/; | ||
} | ||
|
||
.nav-bar{ | ||
background-color: #676798; | ||
overflow: hidden; | ||
display: block; | ||
padding: 0%; | ||
text-decoration: none; | ||
} | ||
|
||
body{ | ||
margin: 0%; | ||
} | ||
|
||
.body-padding{ | ||
margin-left: 10%; | ||
margin-right: 10%; | ||
} | ||
|
||
|
||
/*list heading nav bar*/ | ||
ul { | ||
list-style-type: none; | ||
margin: 0; | ||
padding: 0; | ||
overflow: hidden; | ||
background-color: #676798; | ||
} | ||
|
||
li { | ||
float: left; | ||
} | ||
|
||
li a { | ||
display: block; | ||
color: Violet; | ||
text-align: center; | ||
padding: 14px 16px; | ||
text-decoration: none; | ||
} | ||
|
||
li:active{ | ||
background-color: #1f1f2e; | ||
} | ||
|
||
li:hover{ | ||
background-color: #1f1f2e; | ||
} | ||
|
||
code { | ||
background: hsl(0, 3%, 92%); | ||
} | ||
|
||
pre { | ||
/* white-space: pre-wrap; | ||
background: hsl(30,80%,90%); */ | ||
} | ||
|
||
.code-container { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 300px; | ||
} | ||
|
||
.code-block { | ||
width: 500px; | ||
height: 190px; | ||
padding: 1px; | ||
margin: 0px; | ||
background-color: #f5f5f5; | ||
border: 1px solid #ddd; | ||
overflow: auto; | ||
white-space: pre-wrap; | ||
text-align: left; | ||
} | ||
|
||
</style> | ||
|
This file contains 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
This file was deleted.
Oops, something went wrong.