Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写读取MongoDB数据库的Maven Java Azure函数?_Java_Mongodb_Maven_Azure_Azure Functions - Fatal编程技术网

如何编写读取MongoDB数据库的Maven Java Azure函数?

如何编写读取MongoDB数据库的Maven Java Azure函数?,java,mongodb,maven,azure,azure-functions,Java,Mongodb,Maven,Azure,Azure Functions,有人能帮我在Maven Java项目中找到一个Azure函数来读取MongoDB数据库吗 我正在努力在github或Microsoft的Azure文档中找到任何有用的示例 这是我到目前为止所拥有的——尽管它无法编译,因为它无法访问我在一个单独的库中定义的类,我想包括这个库 My pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"

有人能帮我在Maven Java项目中找到一个Azure函数来读取MongoDB数据库吗

我正在努力在github或Microsoft的Azure文档中找到任何有用的示例

这是我到目前为止所拥有的——尽管它无法编译,因为它无法访问我在一个单独的库中定义的类,我想包括这个库

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <name>CRUD Azure Functions</name>
    <version>1.0.0</version>
    <artifactId>crud-azure-functions</artifactId>
    <description>Experimenting with Azure Functions</description>
    <packaging>jar</packaging>

    <parent>
        <artifactId>my-parent-module</artifactId>
        <groupId>com.snoop.dougg</groupId>
        <version>1.0.0</version>
    </parent>

    <properties>
        <start-class>com.snoop.dougg.azure</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-functions-java-core</artifactId>
            <version>1.0.0-beta-2</version>
        </dependency>
        <dependency>
            <groupId>com.snoop.dougg</groupId>
            <artifactId>my-common-lib</artifactId>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <groupId>com.microsoft.azure</groupId>
                    <artifactId>azure-functions-maven-plugin</artifactId>
                    <version>0.1.7</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>java-functions-group</resourceGroup>
                    <appName>crud-azure-functions</appName>
                    <region>Central US</region>
                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>beta</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.basedir}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}</directory>
                                    <includes>
                                        <include>**/*jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <configuration>
                    <outputFile>${project.basedir}/${project.artifactId}-${project.version}.jar</outputFile>
                    <shadedArtifactAttached>false</shadedArtifactAttached>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

4.0.0
积垢Azure函数
1.0.0
积垢azure函数
试用Azure函数
罐子
我的父模块
com.snoop.dougg
1.0.0
com.snoop.dougg.azure
com.microsoft.azure
azure函数java核心
1.0.0-beta-2
com.snoop.dougg
我的公共图书馆
maven资源插件
3.0.2
com.microsoft.azure
azure函数maven插件
0.1.7
org.apache.maven.plugins
maven阴影插件
3.1.0
com.microsoft.azure
azure函数maven插件
java函数组
积垢azure函数
美国中部
功能\扩展\版本
贝塔
包函数
包裹
maven资源插件
复制资源
包裹
复制资源
真的
${project.basedir}
${project.basedir}
**/*罐子
org.apache.maven.plugins
maven阴影插件
${project.basedir}/${project.artifactId}-${project.version}.jar
假的
包裹
阴凉处
下面是我的Java代码:

package com.snoop.dougg.azure;

import com.snoop.dougg.common.model.mongodb.Item;
import com.microsoft.azure.serverless.functions.ExecutionContext;
import com.microsoft.azure.serverless.functions.HttpRequestMessage;
import com.microsoft.azure.serverless.functions.HttpResponseMessage;
import com.microsoft.azure.serverless.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.serverless.functions.annotation.FunctionName;
import com.microsoft.azure.serverless.functions.annotation.HttpTrigger;
import com.microsoft.azure.serverless.functions.annotation.TableInput;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public class ItemCrudFunctions {

    public static void main() {}

    @FunctionName("DouggHelloWorld")
    public HttpResponseMessage<String> helloWorld(
            @HttpTrigger(name = "req", methods = {"get"}, authLevel = AuthorizationLevel.ANONYMOUS)
                 HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        return request.createResponse(HttpServletResponse.SC_OK, "Good news everyone!");
    }

    @FunctionName("ItemRetrieval")
    public HttpResponseMessage<Item> retrieveItem(
            @HttpTrigger(name = "retrieveItem",
                    route = "/item/{itemId}",
                    methods = {"get"},
                    authLevel = AuthorizationLevel.FUNCTION)
                HttpRequestMessage<Optional<String>> requestMessage,
            @TableInput(name = "itemTableInput",
                    tableName = "item",
                    connection = "AzureWebJobsStorage",
                    rowKey = "{itemId}") Item item) {
        return requestMessage.createResponse(HttpServletResponse.SC_OK, item);
    }
}
package com.snoop.dougg.azure;
导入com.snoop.dougg.common.model.mongodb.Item;
导入com.microsoft.azure.serverless.functions.ExecutionContext;
导入com.microsoft.azure.serverless.functions.HttpRequestMessage;
导入com.microsoft.azure.serverless.functions.HttpResponseMessage;
导入com.microsoft.azure.serverless.functions.annotation.AuthorizationLevel;
导入com.microsoft.azure.serverless.functions.annotation.FunctionName;
导入com.microsoft.azure.serverless.functions.annotation.HttpTrigger;
导入com.microsoft.azure.serverless.functions.annotation.TableInput;
导入javax.servlet.http.HttpServletResponse;
导入java.util.Optional;
公共类ItemCrudFunctions{
公共静态void main(){}
@函数名(“DouggHelloWorld”)
公共HTTPResponseMessageHelloWorld(
@HttpTrigger(name=“req”,methods={“get”},authLevel=AuthorizationLevel.ANONYMOUS)
HttpRequestMessage请求,
最终执行(上下文){
returnrequest.createResponse(HttpServletResponse.SC_OK,“各位好消息!”);
}
@FunctionName(“ItemRetrieval”)
公共HTTPResponseMessageRetrieveItem(
@HttpTrigger(name=“retrieveItem”,
route=“/item/{itemId}”,
方法={“get”},
authLevel=AuthorizationLevel.FUNCTION)
HttpRequestMessage请求消息,
@TableInput(name=“itemTableInput”,
tableName=“项目”,
connection=“azurewebjobstorage”,
rowKey=“{itemId}”)项){
返回requestMessage.createResponse(HttpServletResponse.SC_OK,item);
}
}

但我得到了一个错误:找不到symbol:class项,而且最重要的是,我不知道如果它确实编译了,它是否还能工作。到目前为止,我完全找不到Maven Java Azure函数的任何好例子…

我试图在Java Azure函数中访问Azure mongo数据库。在我这方面,我使用了
mongojava驱动程序
lib,而不是您使用的
com.snoop.dougg
lib。请参见下面的示例代码:

函数类:

pom.xml:


4.0.0
com.fabrikam.functions

一个是它不包含
依赖jar包
,第二个包含
依赖jar包

fabrikam-functions-1.0-SNAPSHOT-jar-with-dependencies
jar移动到路径:
${project.basedir}/target/azure functions/${function-app-name}/

对我来说,它看起来像
E:\TestAzureFunction\fabrikam functions\target\azure functions\fabrikam-functions-201710171122094

别忘了
package com.fabrikam.functions;

import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.ExecutionContext;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;

public class Function {
    @FunctionName("mongo")
    public String mongo(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,
                    ExecutionContext context) {

    try {
        MongoClientURI uri = new MongoClientURI("***");
        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase database = mongoClient.getDatabase("db");
        return String.format("Hello, I get database name : %s!", database.getName());

    } catch (Exception e) {
        e.printStackTrace();
        return "Access Error!";
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fabrikam.functions</groupId>
    <artifactId>fabrikam-functions</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Azure Java Functions</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <functionAppName>fabrikam-functions-20171017112209094</functionAppName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-functions-java-core</artifactId>
            <version>1.0.0-beta-1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.6.3</version>
        </dependency>


        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <groupId>com.microsoft.azure</groupId>
                    <artifactId>azure-functions-maven-plugin</artifactId>
                    <version>0.1.5</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>java-functions-group</resourceGroup>
                    <appName>${functionAppName}</appName>
                    <region>westus2</region>
                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>beta</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}</directory>
                                    <includes>
                                        <include>host.json</include>
                                        <include>local.settings.json</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.fabrikam.functions.Function</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>