Skip to content

Commit f06252e

Browse files
Add java samples (#458)
* add java samples * Update samples/samples-java/pom.xml Co-authored-by: Charles Gagnon <chgagnon@microsoft.com> * update readme + add copyright * update to latest fasterxml * update to 2.13.4.1 * fix employee.java in readme Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>
1 parent 13b8194 commit f06252e

31 files changed

+1275
-2
lines changed

README.md

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Azure SQL bindings for Azure Functions are supported for:
1515
- .NET functions (C# in-process)
1616
- NodeJS functions (JavaScript/TypeScript)
1717
- Python functions
18+
- Java functions
1819

1920
## Table of Contents
2021

@@ -32,6 +33,7 @@ Azure SQL bindings for Azure Functions are supported for:
3233
- [.NET functions](#net-functions)
3334
- [JavaScript functions](#javascript-functions)
3435
- [Python functions](#python-functions)
36+
- [Java functions](#java-functions)
3537
- [More Samples](#more-samples)
3638
- [Input Binding](#input-binding)
3739
- [Query String](#query-string)
@@ -100,7 +102,7 @@ These steps can be done in the Terminal/CLI or with PowerShell.
100102

101103
1. Install [Azure Functions Core Tools](https://docs.microsoft.com/azure/azure-functions/functions-run-local)
102104

103-
2. Create a function app for .NET, JavaScript, TypeScript or Python.
105+
2. Create a function app for .NET, JavaScript, TypeScript, Python, or Java.
104106

105107
**.NET**
106108
```bash
@@ -132,6 +134,13 @@ These steps can be done in the Terminal/CLI or with PowerShell.
132134
func init --worker-runtime python
133135
```
134136

137+
**Java**
138+
```bash
139+
mkdir MyApp
140+
cd MyApp
141+
func init --worker-runtime java
142+
```
143+
135144
3. Enable SQL bindings on the function app. More information can be found [in Microsoft Docs](https://docs.microsoft.com/azure/azure-functions/functions-bindings-azure-sql).
136145

137146
**.NET:** Install the extension.
@@ -167,6 +176,24 @@ These steps can be done in the Terminal/CLI or with PowerShell.
167176
"PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1"
168177
```
169178

179+
**Java:**
180+
Update the `host.json` file to the preview extension bundle.
181+
```json
182+
"extensionBundle": {
183+
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
184+
"version": "[4.*, 5.0.0)"
185+
}
186+
```
187+
188+
Add the `azure-functions-java-library-sql` dependency to the pom.xml file.
189+
```xml
190+
<dependency>
191+
<groupId>com.microsoft.azure.functions</groupId>
192+
<artifactId>azure-functions-java-library-sql</artifactId>
193+
<version>0.1.0</version>
194+
</dependency>
195+
```
196+
170197
### Configure Function App
171198

172199
Once you have your Function App you need to configure it for use with Azure SQL bindings for Azure Functions.
@@ -502,6 +529,148 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
502529
- Hit 'F5' to run your code. Click the link to upsert the output array values in your SQL table. Your upserted values should launch in the browser.
503530
- Congratulations! You have successfully created your first SQL output binding! Checkout [Output Binding](#Output-Binding) for more information on how to use it and explore on your own!
504531
532+
### Java functions
533+
534+
#### Input Binding Tutorial
535+
536+
Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](#Create-a-SQL-Server).
537+
538+
- Open your app that you created in [Create a Function App](#create-a-function-app) in VSCode
539+
- Press 'F1' and search for 'Azure Functions: Create Function'
540+
- Choose HttpTrigger -> (Provide a package name) -> (Provide a function name) -> anonymous
541+
- In the file that opens, replace the `public HttpResponseMessage run` block with the below code.
542+
543+
```java
544+
public HttpResponseMessage run(
545+
@HttpTrigger(
546+
name = "req",
547+
methods = {HttpMethod.GET, HttpMethod.POST},
548+
authLevel = AuthorizationLevel.ANONYMOUS,
549+
route = "getemployees")
550+
HttpRequestMessage<Optional<String>> request,
551+
@SQLInput(
552+
commandText = "SELECT * FROM Employees",
553+
commandType = "Text",
554+
connectionStringSetting = "SqlConnectionString")
555+
Employee[] employees) {
556+
557+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(employees).build();
558+
}
559+
```
560+
561+
*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 [Input Binding](#Input-Binding) section*
562+
563+
- Add 'import com.microsoft.azure.functions.sql.annotation.SQLInput;'
564+
- Create a new file and call it 'Employee.java'
565+
- Paste the below in the file. These are the column names of our SQL table.
566+
567+
```java
568+
package com.function.Common;
569+
570+
public class Employee {
571+
private int EmployeeId;
572+
private String LastName;
573+
private String FirstName;
574+
private String Company;
575+
private String Team;
576+
577+
public Employee() {
578+
}
579+
580+
public Employee(int employeeId, String lastName, String firstName, String company, String team) {
581+
EmployeeId = employeeId;
582+
LastName = lastName;
583+
FirstName = firstName;
584+
Company = company;
585+
Team = team;
586+
}
587+
588+
public int getEmployeeId() {
589+
return EmployeeId;
590+
}
591+
592+
public void setEmployeeId(int employeeId) {
593+
this.EmployeeId = employeeId;
594+
}
595+
596+
public String getLastName() {
597+
return LastName;
598+
}
599+
600+
public void setLastName(String lastName) {
601+
this.LastName = lastName;
602+
}
603+
604+
public String getFirstName() {
605+
return FirstName;
606+
}
607+
608+
public void setFirstName(String firstName) {
609+
this.FirstName = firstName;
610+
}
611+
612+
public String getCompany() {
613+
return Company;
614+
}
615+
616+
public void setCompany(String company) {
617+
this.Company = company;
618+
}
619+
620+
public String getTeam() {
621+
return Team;
622+
}
623+
624+
public void setTeam(String team) {
625+
this.Team = team;
626+
}
627+
}
628+
```
629+
630+
- Navigate back to your HttpTriggerJava file.
631+
- Open the local.settings.json file, and in the brackets for "Values," verify there is a 'SqlConnectionString.' If not, add it.
632+
- Hit 'F5' to run your code. This will start up the Functions Host with a local HTTP Trigger and SQL Input Binding.
633+
- Click the link that appears in your terminal.
634+
- You should see your database output in the browser window.
635+
- Congratulations! You have successfully created your first SQL input binding! Checkout [Input Binding](#Input-Binding) for more information on how to use it and explore on your own!
636+
637+
#### Output Binding Tutorial
638+
639+
Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](#Create-a-SQL-Server), and that you have the 'Employee.java' class from the [Input Binding Tutorial](#Input-Binding-Tutorial).
640+
641+
- Open your app in VSCode
642+
- Press 'F1' and search for 'Azure Functions: Create Function'
643+
- Choose HttpTrigger -> (Provide a package name) -> (Provide a function name) -> anonymous
644+
- In the file that opens, replace the `public HttpResponseMessage run` block with the below code.
645+
646+
```java
647+
public HttpResponseMessage run(
648+
@HttpTrigger(
649+
name = "req",
650+
methods = {HttpMethod.GET},
651+
authLevel = AuthorizationLevel.ANONYMOUS,
652+
route = "addemployees-array")
653+
HttpRequestMessage<Optional<String>> request,
654+
@SQLOutput(
655+
commandText = "dbo.Employees",
656+
connectionStringSetting = "SqlConnectionString")
657+
OutputBinding<Employee[]> output) {
658+
659+
output = new Employee[]
660+
{
661+
new Employee(1, "Hello", "World", "Microsoft", "Functions"),
662+
new Employee(2, "Hi", "SQLupdate", "Microsoft", "Functions")
663+
};
664+
665+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(output).build();
666+
}
667+
```
668+
669+
*In the above, "dbo.Employees" is the name of the table our output binding is upserting into. The line below is similar to the input binding and specifies where our SqlConnectionString is. For more information on this, see the [Output Binding](#Output-Binding) section*
670+
671+
- Hit 'F5' to run your code. Click the link to upsert the output array values in your SQL table. Your upserted values should launch in the browser.
672+
- Congratulations! You have successfully created your first SQL output binding! Checkout [Output Binding](#Output-Binding) for more information on how to use it and explore on your own!
673+
505674
## More Samples
506675
507676
### Input Binding

java-library/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.microsoft.azure.functions</groupId>
66
<artifactId>azure-functions-java-library-sql</artifactId>
7-
<version>0.1.0</version>
7+
<version>0.1.0</version> <!-- Update the dependency version in samples-java/pom.xml too if this version is updated. -->
88
<packaging>jar</packaging>
99

1010
<parent>

samples/samples-java/.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Build output
2+
target/
3+
repo/
4+
*.class
5+
6+
# Log file
7+
*.log
8+
9+
# BlueJ files
10+
*.ctxt
11+
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.ear
19+
*.zip
20+
*.tar.gz
21+
*.rar
22+
23+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24+
hs_err_pid*
25+
26+
# IDE
27+
.idea/
28+
*.iml
29+
.settings/
30+
.project
31+
.classpath
32+
33+
# macOS
34+
.DS_Store
35+
36+
# Azure Functions
37+
local.settings.json
38+
bin/
39+
obj/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"ms-azuretools.vscode-azurefunctions",
4+
"vscjava.vscode-java-debug"
5+
]
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Attach to Java Functions",
6+
"type": "java",
7+
"request": "attach",
8+
"hostName": "127.0.0.1",
9+
"port": 5005,
10+
"preLaunchTask": "func: host start"
11+
}
12+
]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"azureFunctions.javaBuildTool": "maven",
3+
"azureFunctions.deploySubpath": "target/azure-functions/samples-java-1665766173929",
4+
"azureFunctions.projectLanguage": "Java",
5+
"azureFunctions.projectRuntime": "~4",
6+
"debug.internalConsoleOptions": "neverOpen",
7+
"azureFunctions.preDeployTask": "package (functions)",
8+
"java.configuration.updateBuildConfiguration": "automatic"
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "func",
6+
"command": "host start",
7+
"problemMatcher": "$func-java-watch",
8+
"isBackground": true,
9+
"options": {
10+
"cwd": "${workspaceFolder}/target/azure-functions/samples-java-1665766173929"
11+
},
12+
"dependsOn": "package (functions)"
13+
},
14+
{
15+
"label": "package (functions)",
16+
"command": "mvn clean package",
17+
"type": "shell",
18+
"group": {
19+
"kind": "build",
20+
"isDefault": true
21+
}
22+
}
23+
]
24+
}

samples/samples-java/host.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "2.0",
3+
"extensionBundle": {
4+
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
5+
"version": "[4.*, 5.0.0)"
6+
}
7+
}

0 commit comments

Comments
 (0)