列出Azure上的SQL Server实例

时间:2018-07-12 15:19:24

标签: java azure azure-sql-database azure-sdk

我正在尝试实现使用Azure Java SDK列出我的Azure订阅上运行的SQL Server实例的代码。

我遵循发布的示例并编写了以下代码:

Azure azure  = Azure.authenticate(credentials)).withDefaultSubscription();
SqlServers sqlServers = azure.sqlServers();
PagedList<SqlServer> list = sqlServers.list();

但是最后一行抛出带有以下堆栈跟踪的java.lang.NoSuchMethodError

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeBindings.create(Ljava/lang/Class;[Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/type/TypeBindings;
at com.microsoft.rest.serializer.JacksonAdapter.constructJavaType(JacksonAdapter.java:119)
at com.microsoft.rest.serializer.JacksonAdapter.deserialize(JacksonAdapter.java:131)
at com.microsoft.rest.ServiceResponseBuilder.buildBody(ServiceResponseBuilder.java:216)
at com.microsoft.rest.ServiceResponseBuilder.build(ServiceResponseBuilder.java:110)
at com.microsoft.azure.AzureResponseBuilder.build(AzureResponseBuilder.java:56)
at com.microsoft.azure.management.sql.implementation.ServersInner.listDelegate(ServersInner.java:553)
at com.microsoft.azure.management.sql.implementation.ServersInner.access$400(ServersInner.java:42)
at com.microsoft.azure.management.sql.implementation.ServersInner$17.call(ServersInner.java:539)
at com.microsoft.azure.management.sql.implementation.ServersInner$17.call(ServersInner.java:535)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.Observable.unsafeSubscribe(Observable.java:10142)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.subscribe(Observable.java:10238)
at rx.Observable.subscribe(Observable.java:10205)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:444)
at rx.observables.BlockingObservable.single(BlockingObservable.java:341)
at com.microsoft.azure.management.sql.implementation.ServersInner.list(ServersInner.java:488)
at com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.TopLevelModifiableResourcesImpl.list(TopLevelModifiableResourcesImpl.java:116)
at AzureDiscoveryClient.main(AzureDiscoveryClient.java:19)

这显然是一个缺陷,因为SDK允许这样做,并且如果出现问题,应返回合理的错误代码。

仍然,还有其他方法可以列出在Azure订阅中运行的SQL Server实例吗?还是我在代码中做某事?

3 个答案:

答案 0 :(得分:1)

请参考以下我的工作代码:

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.sql.SqlServer;
import com.microsoft.azure.management.sql.SqlServers;

import java.io.IOException;

public class ListSqlInstance {

    public static void main(String[] args) throws IOException {


        ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
                "{client Id}",
                "{talent Id}",
                "{secret}",
                AzureEnvironment.AZURE);
        Azure.Authenticated azureAuth = Azure.authenticate(credentials);
       Azure azure = azureAuth.withDefaultSubscription();

        SqlServers sqlServers = azure.sqlServers();
        PagedList<SqlServer> list = sqlServers.list();
        System.out.println(list.size());
    }
}

我的SDK版本:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    version>1.12.0</version>
</dependency>

请不要忘记将sql server权限授予客户端。

enter image description here

希望它对您有帮助。

答案 1 :(得分:0)

我不确定您是否正在寻找Powershell cmdlet解决方案。但是此Powershell cmdlet应该列出您的订阅中的所有sql服务器实例。

假设在运行此Powershell cmdlet之前,您已经登录Powershell中的Azure帐户,并且如果您有多个订阅映射到您的帐户,则在当前Powershell会话中设置当前订阅。

PS C:\ azure> get-azurermresourcegroup | get-azurermsqlserver
      ResourceGroupName:DbRG
ServerName:testSqlserver
位置:美国东部
SqlAdministratorLogin:XXXX
SqlAdministratorPassword:XXX
ServerVersion:12.0
标签:

答案 2 :(得分:0)

问题解决了!

该错误来自Azure SDK与jackson-databind库之间的不兼容。 该库中使用的一种方法是相对较新的方法,我在项目中使用的版本(仅导入一个干净项目-Azure SDK)没有此方法。

按如下所示导入最新的jackson-databind

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
</dependency>

解决了这个问题。

感谢大家的帮助。