Created
July 29, 2012 09:01
-
-
Save hakobera/3196853 to your computer and use it in GitHub Desktop.
DynamoDB sample
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
package entity; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBAttribute; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBAutoGeneratedKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBHashKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBRangeKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBTable; | |
@DynamoDBTable(tableName = "shortingUrl") | |
public class Bookmark { | |
private String shortingUrlKey; | |
private String redirectUrl; | |
@DynamoDBHashKey | |
public String getShortingUrlKey() { | |
return shortingUrlKey; | |
} | |
public void setShortingUrlKey(String shortingUrlKey) { | |
this.shortingUrlKey = shortingUrlKey; | |
} | |
@DynamoDBRangeKey | |
public String getRedirectUrl() { | |
return redirectUrl; | |
} | |
public void setRedirectUrl(String redirectUrl) { | |
this.redirectUrl = redirectUrl; | |
} | |
} |
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
package dynamo; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.PropertiesCredentials; | |
import com.amazonaws.services.dynamodb.AmazonDynamoDBClient; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression; | |
import com.amazonaws.services.dynamodb.datamodeling.PaginatedList; | |
import com.amazonaws.services.dynamodb.model.ListTablesResult; | |
public class DynamoDBManager { | |
static AmazonDynamoDBClient dynamoDB; | |
public static void init() throws Exception { | |
AWSCredentials credentials = new PropertiesCredentials( | |
AmazonDynamoDBSample.class.getResourceAsStream("AwsCredentials.properties")); | |
dynamoDB = new AmazonDynamoDBClient(credentials); | |
dynamoDB.setEndpoint("https://dynamodb.ap-northeast-1.amazonaws.com"); | |
} | |
public static void save(Object entity) { | |
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB); | |
mapper.save(entity); | |
} | |
public static <T> PaginatedList<T> query(Class<T> klass, DynamoDBQueryExpression query) { | |
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB); | |
PaginatedList<T> list = mapper.query(klass, query); | |
return list; | |
} | |
public static <T> T get(Class<T> klass, DynamoDBQueryExpression query) { | |
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB); | |
PaginatedList<T> list = mapper.query(klass, query); | |
if (list.isEmpty()) { | |
return null; | |
} | |
return list.get(0); | |
} | |
public static List<String> tableNames() { | |
ListTablesResult tables = dynamoDB.listTables(); | |
List<String> result = new ArrayList<String>(); | |
for (String tableName : tables.getTableNames()) { | |
result.add(tableName); | |
} | |
return result; | |
} | |
} |
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
package dynamo; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression; | |
import com.amazonaws.services.dynamodb.datamodeling.PaginatedList; | |
import com.amazonaws.services.dynamodb.model.AttributeValue; | |
import entity.Bookmark; | |
public class SaveAndGet { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
if (args.length != 2) { | |
System.out.println("usage: shortkey url"); | |
return; | |
} | |
DynamoDBManager.init(); | |
Bookmark bookmark = new Bookmark(); | |
String key = args[0]; | |
bookmark.setShortingUrlKey(key); | |
bookmark.setRedirectUrl(args[1]); | |
DynamoDBManager.save(bookmark); | |
AttributeValue value = new AttributeValue(key); | |
DynamoDBQueryExpression query = new DynamoDBQueryExpression(value); | |
Bookmark result = DynamoDBManager.get(Bookmark.class, query); | |
System.out.printf("%s = %s\n", result.getShortingUrlKey(), result.getRedirectUrl()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment