-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brandon Siegel
committed
Sep 3, 2020
1 parent
681b140
commit aef2d61
Showing
2 changed files
with
191 additions
and
10 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
172 changes: 172 additions & 0 deletions
172
sdk/tables/azure-data-tables/src/samples/java/ReadmeSamples.java
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,172 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.data.tables; | ||
|
||
import com.azure.data.tables.TableClient; | ||
import com.azure.data.tables.TableClientBuilder; | ||
import com.azure.data.tables.TableServiceClient; | ||
import com.azure.data.tables.TableServiceClientBuilder; | ||
import com.azure.data.tables.TablesSharedKeyCredential; | ||
import com.azure.data.tables.models.ListEntitiesOptions; | ||
import com.azure.data.tables.models.ListTablesOptions; | ||
import com.azure.data.tables.models.TableEntity; | ||
import com.azure.data.tables.models.TableItem; | ||
import com.azure.data.tables.models.TableStorageException; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS ARE USED TO EXTRACT | ||
* APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING LINE NUMBERS OF EXISTING CODE | ||
* SAMPLES. | ||
* | ||
* Class containing code snippets that will be injected to README.md. | ||
*/ | ||
public class ReadmeSamples { | ||
private final String partitionKey = "<partition-key>"; | ||
private final String rowKey = "<row-key>"; | ||
private final String tableName = "<table-name>"; | ||
private final TableServiceClient tableServiceClient = new TableServiceClient(null); | ||
private final TableClient tableClient = new TableClient(null); | ||
private final TableEntity entity = new TableEntity(partitionKey, rowKey); | ||
|
||
/** | ||
* Code sample for authenticating with a connection string. | ||
*/ | ||
public void authenticateWithConnectionString() { | ||
TableServiceClient tableServiceClient = new TableServiceClientBuilder() | ||
.connectionString("<your-connection-string>") | ||
.buildClient(); | ||
} | ||
|
||
/** | ||
* Code sample for authenticating with a shared key. | ||
*/ | ||
public void authenticateWithSharedKey() { | ||
TablesSharedKeyCredential credential = new TablesSharedKeyCredential("<your-account-name>", "<account-access-key>"); | ||
TableServiceClient tableServiceClient = new TableServiceClientBuilder() | ||
.endpoint("<your-table-account-url>") | ||
.credential(credential) | ||
.buildClient(); | ||
} | ||
|
||
/** | ||
* Code sample for authenticating with a SAS. | ||
*/ | ||
public void authenticateWithSAS() { | ||
TableServiceClient tableServiceClient = new TableServiceClientBuilder() | ||
.endpoint("<your-table-account-url>") | ||
.sasToken("<sas-token-string>") | ||
.buildClient(); | ||
} | ||
|
||
/** | ||
* Code sample for constructing a service client. | ||
*/ | ||
public void constructServiceClient() { | ||
TableServiceClient tableServiceClient = new TableServiceClientBuilder() | ||
.connectionString("<your-connection-string>") // or use any of the other authentication methods | ||
.buildClient(); | ||
} | ||
|
||
/** | ||
* Code sample for creating a table. | ||
* @throws TableStorageException if the table exists. | ||
*/ | ||
public void createTable() { | ||
tableServiceClient.createTable(tableName); | ||
} | ||
|
||
/** | ||
* Code sample for creating a table if it doesn't exist. | ||
*/ | ||
public void createTableIfNotExists() { | ||
tableServiceClient.createTableIfNotExists(tableName); | ||
} | ||
|
||
/** | ||
* Code sample for listing tables. | ||
*/ | ||
public void listTables() { | ||
ListTablesOptions options = new ListTablesOptions() | ||
.setFilter(String.format("TableName eq '%s'", tableName)); | ||
|
||
for (TableItem tableItem : tableServiceClient.listTables(options)) { | ||
System.out.println(tableItem.getName()); | ||
} | ||
} | ||
|
||
/** | ||
* Code sample for deleting a table. | ||
*/ | ||
public void deleteTable() { | ||
tableServiceClient.deleteTable(tableName); | ||
} | ||
|
||
/** | ||
* Code sample for constructing a table client. | ||
*/ | ||
public void constructTableClient() { | ||
TableClient tableClient = new TableClientBuilder() | ||
.connectionString("<your-connection-string>") // or use any of the other authentication methods | ||
.tableName(tableName) | ||
.buildClient(); | ||
} | ||
|
||
/** | ||
* Code sample for retrieving a table client from a service client. | ||
*/ | ||
public void retrieveTableClient() { | ||
TableClient tableClient = tableServiceClient.getTableClient(tableName); | ||
} | ||
|
||
/** | ||
* Code sample for creating an entity. | ||
* @throws TableStorageException if the entity exists. | ||
*/ | ||
public void createEntity() { | ||
TableEntity entity = new TableEntity(partitionKey, rowKey) | ||
.addProperty("Product", "Marker Set") | ||
.addProperty("Price", 5.00) | ||
.addProperty("Quantity", 21); | ||
|
||
tableClient.createEntity(entity); | ||
} | ||
|
||
/** | ||
* Code sample for listing entities. | ||
*/ | ||
public void listEntities() { | ||
ListEntitiesOptions options = new ListEntitiesOptions() | ||
.setFilter(String.format("PartitionKey eq '%s'", partitionKey)) | ||
.setSelect("Product, Price"); | ||
|
||
for (TableEntity entity : tableClient.listEntities(options)) { | ||
Map<String, Object> properties = entity.getProperties(); | ||
System.out.println(String.format("%s: %.2f", properties.get("Product"), properties.get("Price"))); | ||
} | ||
} | ||
|
||
/** | ||
* Code sample for deleting an entity. | ||
*/ | ||
public void deleteEntity() { | ||
tableClient.deleteEntity(partitionKey, rowKey); | ||
} | ||
|
||
/** | ||
* Code sample for accessing information about an error. | ||
*/ | ||
public void accessErrorInfo() { | ||
// Create the table if it doesn't already exist. | ||
tableServiceClient.createTableIfNotExists(tableName); | ||
|
||
// Now attempt to create the same table unconditionally. | ||
try { | ||
tableServiceClient.createTable(tableName); | ||
} catch (TableStorageException e) { | ||
System.out.println(e.getStatusCode()); // 409 | ||
} | ||
} | ||
} |