Skip to content

Commit 3916155

Browse files
authored
Merge pull request #105296 from Sarah-Aly/java-samples
Adding more detailed Java samples for the @TableInput annotation
2 parents 285046b + 28038e5 commit 3916155

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

articles/azure-functions/functions-bindings-storage-table.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,70 @@ def main(req: func.HttpRequest, messageJSON) -> func.HttpResponse:
398398

399399
# [Java](#tab/java)
400400

401-
The following example shows an HTTP triggered function which returns the total count of the items in a specified partition in Table storage.
401+
The following example shows an HTTP triggered function which returns a list of person objects who are in a specified partition in Table storage. In the example, the partition key is extracted from the http route, and the tableName and connection are from the function settings.
402402

403403
```java
404-
@FunctionName("getallcount")
405-
public int run(
406-
@HttpTrigger(name = "req",
407-
methods = {HttpMethod.GET},
408-
authLevel = AuthorizationLevel.ANONYMOUS) Object dummyShouldNotBeUsed,
409-
@TableInput(name = "items",
410-
tableName = "mytablename", partitionKey = "myparkey",
411-
connection = "myconnvarname") MyItem[] items
412-
) {
413-
return items.length;
404+
public class Person {
405+
private String PartitionKey;
406+
private String RowKey;
407+
private String Name;
408+
409+
public String getPartitionKey() { return this.PartitionKey; }
410+
public void setPartitionKey(String key) { this.PartitionKey = key; }
411+
public String getRowKey() { return this.RowKey; }
412+
public void setRowKey(String key) { this.RowKey = key; }
413+
public String getName() { return this.Name; }
414+
public void setName(String name) { this.Name = name; }
415+
}
416+
417+
@FunctionName("getPersonsByPartitionKey")
418+
public Person[] get(
419+
@HttpTrigger(name = "getPersons", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="persons/{partitionKey}") HttpRequestMessage<Optional<String>> request,
420+
@BindingName("partitionKey") String partitionKey,
421+
@TableInput(name="persons", partitionKey="{partitionKey}", tableName="%MyTableName%", connection="MyConnectionString") Person[] persons,
422+
final ExecutionContext context) {
423+
424+
context.getLogger().info("Got query for person related to persons with partition key: " + partitionKey);
425+
426+
return persons;
427+
}
428+
```
429+
430+
The TableInput annotation can also extract the bindings from the json body of the request, like the following example shows.
431+
432+
```java
433+
@FunctionName("GetPersonsByKeysFromRequest")
434+
public HttpResponseMessage get(
435+
@HttpTrigger(name = "getPerson", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="query") HttpRequestMessage<Optional<String>> request,
436+
@TableInput(name="persons", partitionKey="{partitionKey}", rowKey = "{rowKey}", tableName="%MyTableName%", connection="MyConnectionString") Person person,
437+
final ExecutionContext context) {
438+
439+
if (person == null) {
440+
return request.createResponseBuilder(HttpStatus.NOT_FOUND)
441+
.body("Person not found.")
442+
.build();
443+
}
444+
445+
return request.createResponseBuilder(HttpStatus.OK)
446+
.header("Content-Type", "application/json")
447+
.body(person)
448+
.build();
449+
}
450+
```
451+
452+
The following examples uses the Filter to query for persons with a specific name in an Azure Table, and limits the number of possible matches to 10 results.
453+
454+
```java
455+
@FunctionName("getPersonsByName")
456+
public Person[] get(
457+
@HttpTrigger(name = "getPersons", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="filter/{name}") HttpRequestMessage<Optional<String>> request,
458+
@BindingName("name") String name,
459+
@TableInput(name="persons", filter="Name eq '{name}'", take = "10", tableName="%MyTableName%", connection="MyConnectionString") Person[] persons,
460+
final ExecutionContext context) {
461+
462+
context.getLogger().info("Got query for person related to persons with name: " + name);
463+
464+
return persons;
414465
}
415466
```
416467

@@ -763,7 +814,8 @@ public class Person {
763814
public String getName() {return this.Name;}
764815
public void setName(String name) {this.Name = name; }
765816
}
766-
public class AddPerson {
817+
818+
public class AddPerson {
767819

768820
@FunctionName("addPerson")
769821
public HttpResponseMessage get(

0 commit comments

Comments
 (0)