Skip to content

Commit

Permalink
Add embedme
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Siegel committed Sep 3, 2020
1 parent 681b140 commit d3a5da3
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 10 deletions.
29 changes: 19 additions & 10 deletions sdk/tables/azure-data-tables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Common uses of the Table service include:
#### Authenticate with a connection string
To use a connection string to authorize your client, call the builder's `connectionString` method with your connection string.

<!-- embedme src/samples/java/ReadmeSamples.java#L33-L35 -->
```java
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString("<your-connection-string>")
Expand All @@ -158,6 +159,7 @@ TableServiceClient tableServiceClient = new TableServiceClientBuilder()
#### Authenticate with a Shared Key
To use a Shared Key to authorize your client, create an instance of `TablesSharedKeyCredential` with your account name and access key. Call the builder's `endpoint` method with your account URL and the `credential` method with the `TablesSharedKeyCredential` object you created.

<!-- embedme src/samples/java/ReadmeSamples.java#L42-L46 -->
```java
TablesSharedKeyCredential credential = new TablesSharedKeyCredential("<your-account-name>", "<account-access-key>");
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
Expand All @@ -169,6 +171,7 @@ TableServiceClient tableServiceClient = new TableServiceClientBuilder()
#### Authenticate with a Shared Access Signature (SAS)
To use a SAS to authorize your client, call the builder's `endpoint` method with your account URL and the `sasToken` method with your SAS.

<!-- embedme src/samples/java/ReadmeSamples.java#L53-L56 -->
```java
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.endpoint("<your-table-account-url>")
Expand All @@ -181,28 +184,32 @@ TableServiceClient tableServiceClient = new TableServiceClientBuilder()
#### Construct a `TableServiceClient`
Construct a `TableServiceClient` by creating an instance of `TableServiceClientBuilder` and then calling the builder's `buildClient` or `buildAsyncClient` methods.

<!-- embedme src/samples/java/ReadmeSamples.java#L63-L65 -->
```java
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString("<your-connection-string>") // or use any of the other authentication methods
.buildClient();
```a
```

#### Create a table
Create a table by calling the `TableServiceClient`'s `createTable` method. An exception will be thrown if a table with the provided name exists.

<!-- embedme src/samples/java/ReadmeSamples.java#L73-L73 -->
```java
tableServiceClient.createTable(tableName);
```

Alternatively, you can call the `createTableIfNotExists` method which will create the table only if no such table exists, and does not throw an exception.

<!-- embedme src/samples/java/ReadmeSamples.java#L80-L80 -->
```java
tableServiceClient.createTableIfNotExists(tableName);
```

#### List tables
List or query the set of existing tables by calling the `TableServiceClient`'s `listTables` method, optionally passing in a `ListTablesOptions` instance to filter or limit the query results. See [Supported Query Options][query_options] for details about supported query options.

<!-- embedme src/samples/java/ReadmeSamples.java#L87-L92 -->
```java
ListTablesOptions options = new ListTablesOptions()
.setFilter(String.format("TableName eq '%s'", tableName));
Expand All @@ -215,6 +222,7 @@ for (TableItem tableItem : tableServiceClient.listTables(options)) {
#### Delete a table
Delete a table by calling the `TableServiceClient`'s `deleteTable` method. An exception will be thrown if no table with the provided name exists.

<!-- embedme src/samples/java/ReadmeSamples.java#L99-L99 -->
```java
tableServiceClient.deleteTable(tableName);
```
Expand All @@ -224,6 +232,7 @@ tableServiceClient.deleteTable(tableName);
#### Construct a `TableClient`
Construct a `TableClient` by creating an instance of `TableClientBuilder`, calling the builder's `tableName` method with the name of the table, and then calling its `buildClient` or `buildAsyncClient` methods.

<!-- embedme src/samples/java/ReadmeSamples.java#L106-L109 -->
```java
TableClient tableClient = new TableClientBuilder()
.connectionString("<your-connection-string>") // or use any of the other authentication methods
Expand All @@ -233,13 +242,15 @@ TableClient tableClient = new TableClientBuilder()

Alternatively, a `TableClient` can be retrieved from an existing `TableServiceClient` by calling its `getTableClient` method.

<!-- embedme src/samples/java/ReadmeSamples.java#L116-L116 -->
```java
TableClient tableClient = tableServiceClient.getTableClient(tableName);
```

#### Create an entity
Create a new `TableEntity` instance, providing the partition key and row key of the entity to create, optionally adding properties to the created object. Then pass the object to the `TableClient`'s `createEntity` method. An exception will be thrown if an entity with the provided partition key and row key exists within the table.

<!-- embedme src/samples/java/ReadmeSamples.java#L124-L129 -->
```java
TableEntity entity = new TableEntity(partitionKey, rowKey)
.addProperty("Product", "Marker Set")
Expand All @@ -249,28 +260,25 @@ TableEntity entity = new TableEntity(partitionKey, rowKey)
tableClient.createEntity(entity);
```

Alternatively, you can call the `createEntityIfNotExists` method which will create the entity only if no entity with the provided partition key and row key exists within the table, and does not throw an exception.

```java
tableClient.createEntityIfNotExists(entity);
```

#### List entities
List or query the set of entities within the table by calling the `TableClient`'s `listEntities` method, optionally passing in a `ListEntitiesOptions` instance to filter, select, or limit the query results. See [Supported Query Options][query_options] for details about supported query options.

<!-- embedme src/samples/java/ReadmeSamples.java#L136-L143 -->
```java
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter(String.format("PartitionKey eq '%s'", partitionKey))
.setSelect("Product, Price");

for (TableEntity entity : tableClient.listEntities(options)) {
System.out.println(String.format("%s: %.2f", entity.getProperty("Product"), entity.getProperty("Price")));
Map<String, Object> properties = entity.getProperties();
System.out.println(String.format("%s: %.2f", properties.get("Product"), properties.get("Price")));
}
```

#### Delete an entity
Delete an entity by calling the `TableClient`'s `deleteEntity` method. An exception will be thrown if no entity with the provided partition key and row key exists.

<!-- embedme src/samples/java/ReadmeSamples.java#L150-L150 -->
```java
tableClient.deleteEntity(partitionKey, rowKey);
```
Expand All @@ -282,15 +290,16 @@ When you interact with Tables service using the Azure Tables library for Java, e

For example, if you try to create a table that already exists, a `409` error is returned, indicating "Conflict".

<!-- embedme src/samples/java/ReadmeSamples.java#L157-L165 -->
```java
// 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 (TableServiceErrorException e) {
System.out.println(e.getResponse().getStatusCode()); // 409
} catch (TableStorageException e) {
System.out.println(e.getStatusCode()); // 409
}
```

Expand Down
167 changes: 167 additions & 0 deletions sdk/tables/azure-data-tables/src/samples/java/ReadmeSamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.data.tables;

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

0 comments on commit d3a5da3

Please sign in to comment.