Skip to content

Commit d5cac14

Browse files
authored
Update SetupGuide_Java.md (#633)
* Update SetupGuide_Java.md Fill in samples * Update SetupGuide_Java.md add dependency * Update SetupGuide_Java.md
1 parent 74a7fbc commit d5cac14

File tree

1 file changed

+196
-17
lines changed

1 file changed

+196
-17
lines changed

docs/SetupGuide_Java.md

+196-17
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
- [Empty Parameter Value](#empty-parameter-value)
1313
- [Null Parameter Value](#null-parameter-value)
1414
- [Stored Procedure](#stored-procedure)
15-
- [IAsyncEnumerable](#iasyncenumerable)
1615
- [Output Binding](#output-binding)
1716
- [SQLOutput Attribute](#sqloutput-attribute)
1817
- [Setup for Output Bindings](#setup-for-output-bindings)
1918
- [Samples for Output Bindings](#samples-for-output-bindings)
20-
- [ICollector\<T\>/IAsyncCollector\<T\>](#icollectortiasynccollectort)
2119
- [Array](#array)
2220
- [Single Row](#single-row)
2321
- [Trigger Binding](#trigger-binding)
@@ -44,6 +42,14 @@ These instructions will guide you through creating your Function App and adding
4442
"version": "[4.*, 5.0.0)"
4543
}
4644
```
45+
Add the Java library for SQL bindings to the pom.xml file.
46+
```xml
47+
<dependency>
48+
<groupId>com.microsoft.azure.functions</groupId>
49+
<artifactId>azure-functions-java-library-sql</artifactId>
50+
<version>[0.1.1,)</version>
51+
</dependency>
52+
```
4753
4854
## Input Binding
4955
@@ -67,7 +73,7 @@ When you're developing locally, add your application settings in the local.setti
6773
6874
Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server).
6975
70-
- Open your app that you created in [Create a Function App](./GeneralSetup.md#create-a-function-app) in VS Code
76+
- Open your app that you created in [Setup Function App](#setup-function-app) in VS Code
7177
- Press 'F1' and search for 'Azure Functions: Create Function'
7278
- Choose HttpTrigger -> (Provide a package name) -> (Provide a function name) -> anonymous
7379
- In the file that opens, replace the `public HttpResponseMessage run` block with the below code.
@@ -91,8 +97,8 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
9197
9298
*In the above, "select * from Employees" is the SQL script run by the input binding. The CommandType on the line below specifies whether the first line is a query or a stored procedure. On the next line, the ConnectionStringSetting specifies that the app setting that contains the SQL connection string used to connect to the database is "SqlConnectionString." For more information on this, see the [SQLInput Attribute](#sqlinput-attribute) section*
9399
94-
- Add 'import com.microsoft.azure.functions.sql.annotation.SQLInput;'
95-
- Create a new file and call it 'Employee.java'
100+
- Add `import com.microsoft.azure.functions.sql.annotation.SQLInput;`
101+
- Create a new file and call it `Employee.java`
96102
- Paste the below in the file. These are the column names of our SQL table. Note that the casing of the Object field names and the table column names must match.
97103
98104
```java
@@ -156,23 +162,155 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
156162
157163
#### Query String
158164
159-
_TODO_
165+
The input binding executes the `SELECT * FROM Products WHERE Cost = @Cost` query, returning the result as Product[], where Product is a user-defined object. The Parameters argument passes the {cost} specified in the URL that triggers the function, getproducts/{cost}, as the value of the @Cost parameter in the query. CommandType is set to `Text`, since the constructor argument of the binding is a raw query.
166+
167+
```java
168+
@FunctionName("GetProducts")
169+
public HttpResponseMessage run(
170+
@HttpTrigger(
171+
name = "req",
172+
methods = {HttpMethod.GET},
173+
authLevel = AuthorizationLevel.ANONYMOUS,
174+
route = "getproducts/{cost}")
175+
HttpRequestMessage<Optional<String>> request,
176+
@SQLInput(
177+
name = "products",
178+
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
179+
commandType = "Text",
180+
parameters = "@Cost={cost}",
181+
connectionStringSetting = "SqlConnectionString")
182+
Product[] products) {
183+
184+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
185+
}
186+
```
187+
188+
`Product` is a user-defined object that follows the structure of the Products table. It represents a row of the Products table, with field names and types copying those of the Products table schema. For example, if the Products table has three columns of the form
189+
190+
- **ProductId**: int
191+
- **Name**: varchar
192+
- **Cost**: int
193+
194+
Then the `Product` class would look like
195+
196+
```java
197+
public class Product {
198+
@JsonProperty("ProductId")
199+
private int ProductId;
200+
@JsonProperty("Name")
201+
private String Name;
202+
@JsonProperty("Cost")
203+
private int Cost;
204+
205+
public Product() {
206+
}
207+
208+
public Product(int productId, String name, int cost) {
209+
ProductId = productId;
210+
Name = name;
211+
Cost = cost;
212+
}
213+
214+
public int getProductId() {
215+
return ProductId;
216+
}
217+
218+
public void setProductId(int productId) {
219+
this.ProductId = productId;
220+
}
221+
222+
public String getName() {
223+
return Name;
224+
}
225+
226+
public void setName(String name) {
227+
this.Name = name;
228+
}
229+
230+
public int getCost() {
231+
return Cost;
232+
}
233+
234+
public void setCost(int cost) {
235+
this.Cost = cost;
236+
}
237+
}
238+
```
160239
161240
#### Empty Parameter Value
162241
163-
_TODO_
242+
In this case, the parameter value of the @Name parameter is an empty string.
243+
244+
```java
245+
@FunctionName("GetProductsNameEmpty")
246+
public HttpResponseMessage run(
247+
@HttpTrigger(
248+
name = "req",
249+
methods = {HttpMethod.GET},
250+
authLevel = AuthorizationLevel.ANONYMOUS,
251+
route = "getproducts-nameempty/{cost}")
252+
HttpRequestMessage<Optional<String>> request,
253+
@SQLInput(
254+
name = "products",
255+
commandText = "SELECT * FROM Products WHERE Cost = @Cost and Name = @Name",
256+
commandType = "Text",
257+
parameters = "@Cost={cost},@Name=",
258+
connectionStringSetting = "SqlConnectionString")
259+
Product[] products) {
260+
261+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
262+
}
263+
```
164264
165265
#### Null Parameter Value
166266
167-
_TODO_
267+
If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", the query returns all rows for which the Name column is `NULL`. Otherwise, it returns all rows for which the value of the Name column matches the string passed in `{name}`
268+
269+
```java
270+
@FunctionName("GetProductsNameNull")
271+
public HttpResponseMessage run(
272+
@HttpTrigger(
273+
name = "req",
274+
methods = {HttpMethod.GET},
275+
authLevel = AuthorizationLevel.ANONYMOUS,
276+
route = "getproducts-namenull/{name}")
277+
HttpRequestMessage<Optional<String>> request,
278+
@SQLInput(
279+
name = "products",
280+
commandText = "IF @Name IS NULL SELECT * FROM Products WHERE Name IS NULL ELSE SELECT * FROM Products WHERE Name = @Name",
281+
commandType = "Text",
282+
parameters = "@Name={name}",
283+
connectionStringSetting = "SqlConnectionString")
284+
Product[] products) {
285+
286+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
287+
}
288+
```
168289
169290
#### Stored Procedure
170291
171-
_TODO_
292+
`SelectProductsCost` is the name of a procedure stored in the user's database. In this case, *CommandType* is `StoredProcedure`. The parameter value of the `@Cost` parameter in the procedure is once again the `{cost}` specified in the `getproducts-storedprocedure/{cost}` URL.
172293
173-
#### IAsyncEnumerable
294+
```java
295+
@FunctionName("GetProductsStoredProcedure")
296+
public HttpResponseMessage run(
297+
@HttpTrigger(
298+
name = "req",
299+
methods = {HttpMethod.GET},
300+
authLevel = AuthorizationLevel.ANONYMOUS,
301+
route = "getproducts-storedprocedure/{cost}")
302+
HttpRequestMessage<Optional<String>> request,
303+
@SQLInput(
304+
name = "products",
305+
commandText = "SelectProductsCost",
306+
commandType = "StoredProcedure",
307+
parameters = "@Cost={cost}",
308+
connectionStringSetting = "SqlConnectionString")
309+
Product[] products) {
174310
175-
_TODO_
311+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
312+
}
313+
```
176314
177315
## Output Binding
178316
@@ -208,6 +346,7 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
208346
route = "addemployees-array")
209347
HttpRequestMessage<Optional<String>> request,
210348
@SQLOutput(
349+
name = "output",
211350
commandText = "dbo.Employees",
212351
connectionStringSetting = "SqlConnectionString")
213352
OutputBinding<Employee[]> output) {
@@ -227,18 +366,58 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
227366
228367
### Samples for Output Bindings
229368
230-
#### ICollector&lt;T&gt;/IAsyncCollector&lt;T&gt;
369+
#### Array
231370
232-
_TODO_
371+
``` java
372+
@FunctionName("AddProductsArray")
373+
public HttpResponseMessage run(
374+
@HttpTrigger(
375+
name = "req",
376+
methods = {HttpMethod.POST},
377+
authLevel = AuthorizationLevel.ANONYMOUS,
378+
route = "addproducts-array")
379+
HttpRequestMessage<Optional<String>> request,
380+
@SQLOutput(
381+
name = "products",
382+
commandText = "Products",
383+
connectionStringSetting = "SqlConnectionString")
384+
OutputBinding<Product[]> products) throws JsonParseException, JsonMappingException, IOException {
233385
234-
#### Array
386+
String json = request.getBody().get();
387+
ObjectMapper mapper = new ObjectMapper();
388+
Product[] p = mapper.readValue(json, Product[].class);
389+
products.setValue(p);
235390
236-
_TODO_
391+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
392+
}
393+
```
237394
238395
#### Single Row
239396
240-
_TODO_
397+
```java
398+
@FunctionName("AddProduct")
399+
public HttpResponseMessage run(
400+
@HttpTrigger(
401+
name = "req",
402+
methods = {HttpMethod.POST},
403+
authLevel = AuthorizationLevel.ANONYMOUS,
404+
route = "addproduct")
405+
HttpRequestMessage<Optional<String>> request,
406+
@SQLOutput(
407+
name = "product",
408+
commandText = "Products",
409+
connectionStringSetting = "SqlConnectionString")
410+
OutputBinding<Product> product) throws JsonParseException, JsonMappingException, IOException {
411+
412+
String json = request.getBody().get();
413+
ObjectMapper mapper = new ObjectMapper();
414+
Product p = mapper.readValue(json, Product.class);
415+
product.setValue(p);
416+
417+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(product).build();
418+
}
419+
```
241420
242421
## Trigger Binding
243422
244-
> Trigger binding support is only available for in-proc C# functions at present.
423+
> Trigger binding support is only available for in-proc C# functions at present.

0 commit comments

Comments
 (0)