Как разобрать JSON и вставить в Azure SQL с помощью Java Azure функций - PullRequest
0 голосов
/ 20 апреля 2020

Я новичок с Java Azure функциями. Я получаю триггер служебной шины и вывод Azure SQL, но мне нужен совет по синтаксическому анализу сообщения json.

У меня есть триггер служебной шины, который предоставляет такие данные, как "{" name ":" Джон "," city ":" NY C "}"

Я хотел бы сохранить это в Azure SQL. Как разобрать JSON и сохранить в SQL?

package com.function;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.ResultSet;

 /**
 * Azure Functions with Azure Storage Queue trigger.
 */
public class TopicTriggerSQLOutput {
    /**
     * This function will be invoked when a new message is received at the specified path. The 
message contents are provided as input to this function.
     */
    @FunctionName("TopicTriggerSQLOutput")
    public void run(
        @ServiceBusTopicTrigger(
            name = "message",
            topicName = "newtopic",
            subscriptionName = "newsubscription",
            connection = "topicconnstring"
        ) String message,
        final ExecutionContext context
        ) {

            String connectionUrl = "jdbc:sqlserver:...database.windows.net;loginTimeout=30;";

            try (Connection connection = DriverManager.getConnection(connectionUrl);
                Statement statement = connection.createStatement();) {

                    context.getLogger().info("SeviceBus message" + message); // I get JSON returned 
 correctly here. 

                // How to parse message json string?

                // Create and execute a SELECT SQL statement.
                String InsertSql = "INSERT INTO [dbo].[Target_Table] ([NAME],[CITY]) VALUES 
                                   ('NameString', 'CityString')";

                // insert the data
                statement.executeUpdate(InsertSql);
        }
            // Handle any errors that may have occurred.
            catch (SQLException e) {
                e.printStackTrace();
            }

        context.getLogger().info("Message: " + message);
    }
}

1 Ответ

0 голосов
/ 22 апреля 2020

Проблема решена с импортом Gson. https://www.tutorialspoint.com/gson/gson_first_application.htm

import com.google.gson.Gson;

Student myjson = gson.fromJson(jsonString, myjson.class); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...