Search code examples
javamysqlbloomberg

Simplest way to append Bloomberg API data to a MySQL table using Java


I would like to append a Bloomberg API response to a MySQL database using Java.

At the moment I query the API and display the response using the below code:

    session.sendRequest(request, null);

    while (true) {
        Event event = session.nextEvent();
        MessageIterator msgIter = event.messageIterator();
        while (msgIter.hasNext()) {
              Message msg = msgIter.next();
              new JSONObject(msg);
              System.out.println(msg);
        }
        if (event.eventType() == Event.EventType.RESPONSE) {
            break;
        }
    }

I would like to be able to append the msg object to a table in a MySQL database. The msg object looks like it is a JSON format (although I am unsure how to confirm this).

I also have no problems connecting to the MySQL table using JDBC.


Solution

  • Step 5 connected with select query, in you case at step 4 you need use update query and no step 5 will be required. You need to get fields values from json and put them in update statement. To get values from json:

    //example json string
    String json = "{paramsArray: [\"first\", 100],"
            + "paramsObj: {one: \"two\", three: \"four\"},"
            + "paramsStr: \"some string\"}";
    
    JSONParser parser = new JSONParser();
    
    Object obj = parser.parse(json);
    JSONObject jsonObj = (JSONObject) obj;
    System.out.println(jsonObj.get("paramsStr"));
    
    JsonObject jo = jsonObj.get("paramsObj");
    System.out.println(jo.get("three"));
    // output will be 'four'
    

    Here reference to update query in mysql: https://dev.mysql.com/doc/refman/5.0/en/update.html