Contact Us 1-800-596-4880

Tutorial: Store and Retrieve Data from Object Store v2

Use this tutorial to configure Anypoint Connector for Object Store (Object Store Connector) in your app so that you can use Object Store v2 to store and view data.

You can send any serializable data as key-value pairs to the object store.

In this tutorial, you:

  1. Create a Mule 4.x app in Anypoint Studio 7.x and configure Object Store Connector for Mule 4 in the app.

    You then test your app by sending data to the object store and viewing it in Runtime Manager.

  2. Optionally, create a Mule 3.9 app in Anypoint Studio 6.x, configure Object Store Connector for Mule 3, and test the connection.

    Because Mule 3.9 and Studio 6.x are in Extended Support, we recommend that you use the latest versions of Mule and Studio.

Before You Begin

Before you begin this tutorial, you must have:

  • Familiarity with Anypoint Studio, Anypoint Platform, and Runtime Manager

  • An Anypoint Platform account

    If you don’t have an Anypoint Platform login, click Sign up on the Anypoint Platform sign-in page.

  • Anypoint Studio installed on your system:

  • An curl command line tool for testing

    This tutorial uses the HTTP Listener source to initiate Object Store Connector access and a curl command to send JSON data to your Mule app.

    Instead of curl, you can use Postman or browser plugins.

Create a Mule 4.x App with an Object Store in Anypoint Studio 7.x

First, use Studio 7.x to configure an app that includes an HTTP Listener source and Object Store Connector. Then, deploy the app to CloudHub and use a curl command to send a key and value, and view the key in Runtime Manager.

To configure a Mule 4.x app in Studio 7.x:

  1. Start Studio, click File > New > Mule Project, and name the project osv2-demo.

  2. Install Object Store Connector for Mule 4:

    1. Click the Exchange icon in the toolbar:

      Studio 7.x window showing the Exchange icon in the toolbar
    2. Enter your Anypoint Platform username and password, if prompted.

    3. In Exchange, select Provided by MuleSoft and Connectors, and then search for Object Store Connector:

      Exchange window showing the Provided by MuleSoft option and search field
    4. Select ObjectStore Connector - Mule 4.

    5. Click Add to project:

      Exchange window showing the Add to project icon
    6. At the confirmation prompt, click Proceed and then click OK.

  3. In the Mule Palette view, click HTTP, and then drag Listener to the Studio canvas:

    Mule Palette view showing the HTTP Connector and Listener operations
    1 HTTP Connector
    2 Listener operation
  4. Click ObjectStore and then drag the Store operation to Listener on the canvas:

    Studio 7.x window showing the Store operation on the canvas
  5. In the canvas, click Listener.

  6. In the Listener properties window, set General > Path to /store.

  7. Click the plus icon (+) for the Connector configuration field.

  8. In the HTTP Listener Global Element Properties window, verify that the value of Host is 0.0.0.0 and Port is 8081, and then click OK.

    The Listener properties window indicates that there are no errors:

    Listener properties window showing no errors
  9. In the canvas, click the Store operation.

  10. In the Store properties window, specify #[payload.key] in the Key field and payload.value in the Value field:

    Store properties windows showing the values in the Key and Value fields

    The maximum number of characters in a key is 256.

    Object Store v2 for CloudHub doesn’t support using pipe (|) characters in keys and converts any spaces to plus (+) characters.

  11. In the canvas, select the flow.

  12. In the Flow Configuration properties window, change the name to Store:

    Flow Configuration properties window showing the Name field set to Store
  13. In the Mule Palette view, click HTTP and then drag another Listener to the canvas after the first flow.

  14. Click ObjectStore and then drag the Retrieve operation after to the Listener in the second flow.

  15. In the canvas, click Listener.

  16. In the Listener properties window, set General > Path to /retrieve.

  17. In the Connector configuration menu, select the same HTTP_Listener_config option that you set up for the Listener operation in the Store flow:

    Listener properties window showing the Connector configuration menu
  18. In the canvas, click the Retrieve operation.

  19. In the Retrieve properties window, specify #[attributes.queryParams.key] in the Key field:

    Retrieve properties window showing the value in the Key field
  20. In the canvas, select the second flow and, in the Flow Configuration properties window, change the name to Retrieve.

    The flows look like this:

    Studio canvas showing the Store and Retrieve operation flows
  21. In Package Explorer in Studio, right-click osv2-demo and select Anypoint Platform > Deploy to CloudHub.

  22. In the Runtime Manager Deploying Application window:

    1. Select CloudHub from Deployment Target.

    2. Click Deploy Application:

      Runtime Manager Deploying Application window showing the Deploy Application button
  23. Click Open in Browser to display the app in Runtime Manager.

    If CloudHub displays a permissions error, select a different environment to deploy to.

You have successfully used Studio 7 to configure an app with an object store. Next, you test the connection by submitting REST API commands to the Object Store API and using Runtime Manager to view the results.

Test Your Studio 7.x Application

After you create your app in Studio 7, you can test it by using a curl command to submit a REST API command to the Object Store API. Then, use Runtime Manager to view the results.

To test your app:

  1. In Runtime Manager, click Applications and ensure that the osv2-demo app is running.

  2. Click the Status column for the osv2-demo app to display its details.

  3. Copy the App url value:

    Runtime Manager app details pane showing the App url
  4. From the command line, run this curl command, replacing APP-URL with the App url value that you copied from Runtime Manager:

    $ curl -X POST -H "Content-Type: application/json" -d '{ "key": "myKey", "value": "OSv2 Test" }' "http://APP-URL/store";echo

    The output from this command displays the key-value pair that you sent to the object store:

    { "key": "myKey", "value": "OSv2 Test" }
  5. In Runtime Manager, click Manage application in the details pane for the application:

    Runtime Manager app details pane showing the Manage application button
  6. Select Object Store from the sidebar.

  7. Click the object store, the partition, and then the key:

    Runtime Manager window showing the selected object store, partition, and key

    The myKey key that you sent from curl appears in the object store with the value of [binary value] BINARY. The value is binary because Mule 4 wraps values in a Mule object, which causes them to be visible only as binary in Anypoint Platform.

  8. From the command line, run this curl command to view the value for the key, replacing APP-URL with the App url value that you copied from Runtime Manager:

    curl http://APP-URL/retrieve?key=myKey;echo

    The output from this command displays the value for the key:

    "OSv2 Test"

You have successfully tested your app using a curl command to store and retrieve a key-value pair in the object store and viewed the results in Runtime Manager.

Example App XML for Studio 7.x

Click Configuration XML in Studio and compare your XML to this example. Make corrections if necessary.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:os="http://www.mulesoft.org/schema/mule/os" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="24b76402-e396-4228-a48e-fcd9ac651488" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<flow name="Store" doc:id="e7d253f9-534b-41df-a189-de815a42facb" >
		<http:listener doc:name="Listener" doc:id="805c84ec-35d4-4753-846b-49b108044df7" config-ref="HTTP_Listener_config" path="/store"/>
		<os:store doc:name="Store" doc:id="d4800501-0fa8-4472-a23e-5213f04155e8" key="#[payload.key]">
			<os:value ><![CDATA[#[payload.value]]]></os:value>
		</os:store>
	</flow>
	<flow name="Retrieve" doc:id="1db04ffc-76c1-48b4-ba33-57807a6404a8" >
		<http:listener doc:name="Listener" doc:id="faed2a0f-3005-4244-a6c6-77407b34d53a" path="/retrieve" config-ref="HTTP_Listener_config"/>
		<os:retrieve doc:name="Retrieve" doc:id="3f907cfc-59e5-49cd-879a-7b2675d488a5" key="#[attributes.queryParams.key]">
		</os:retrieve>
	</flow>
</mule>

Create a Mule 3.9 App with an Object Store in Anypoint Studio 6.x

You can use Anypoint Studio 6.x to create a Mule 3.9 app similar to the app you created in Studio 7.x. First, use Studio 6.x to configure an app that includes Object Store Connector for Mule 3. Then, deploy the app to CloudHub and use a curl command to send a key and value, and view the key in Runtime Manager.

Because Mule 3.9 and Studio 6.x are in Extended Support, we recommend that you use the latest versions of Mule and Studio.

To configure a Mule 3.9 app in Studio 6.x:

  1. Start Studio, click File > New > Mule Project, name the project osv2-demo-studio-6, and then click Finish.

  2. Install Object Store Connector:

    1. Click the Exchange icon in the toolbar:

      Studio 6.x showing the Exchange icon in the toolbar
    2. Enter your Anypoint Platform username and password, if prompted.

    3. In Exchange, select Provided by MuleSoft and Connectors, and then search for Object Store Connector:

      Exchange window showing the Provided by MuleSoft option and search field
    4. Select Object Store Connector - Mule 3.

    5. Click Install:

      Exchange window showing the Install icon
    6. At the prompt, check the items to install and click Next twice.

    7. Accept the license agreement and click Finish.

    8. At the prompt, click Restart Now to finish installing the connector.

  3. In the Mule Palette view:

    1. Drag HTTP to the Studio canvas:

      Mule Palette view showing the HTTP Connector
    2. Search for string and then drag Object to String to the Process area for HTTP on the canvas.

    3. Search for json and then drag JSON to Object to the canvas after Object to String.

    4. Search for objects and then drag ObjectStore to the canvas after JSON to Object.

    5. Search for payload and then drag Set Payload to the canvas after ObjectStore.

      The flow looks like this:

      Studio 6.x canvas showing the HTTP flows
  4. In the canvas, click HTTP.

  5. In the HTTP properties window:

    1. Set Path to /store and Allowed Methods to POST.

      When you submit a curl command, you append the /store option to the URL. The Allowed Methods setting ensures that the curl command can only POST information to the object store.

    2. Click the plus icon (+) for the Connector Configuration option.

    3. In the HTTP Connector Global Element Properties window, verify that the value of Host is 0.0.0.0 and Port is 8081, and then click OK.

      The HTTP properties window now indicates that there are no errors:

      Studio 6.x window showing the HTTP properties
  6. In the canvas, click JSON to Object and then set Return Class to java.util.Map:

    Studio 6.x window showing the JSON to Object properties and value in the Return Class field
  7. In the canvas, click ObjectStore.

  8. In the ObjectStore properties window:

    1. Set the Operation to Store and set these store operation values:

      • Set Key to #[payload.key].

        The maximum number of characters in a key is 256.

        Object Store v2 for CloudHub doesn’t support using pipe (|) characters in keys and converts any spaces to plus (+) characters.

      • Set Value Reference to #[payload.value].

    2. Click the plus icon (+) for the Connector Configuration option.

    3. In the ObjectStore Connector Global Element Properties window, set Partition to myPartition and Object Store Reference to _defaultUserObjectStore, and then click OK.

      Global Element Properties window showing the Partition and Object Store Reference fields
      The value of the Object Store Reference field must be _defaultUserObjectStore for Object Store v2 to store data.

      The ObjectStore properties window now indicates that there are no errors:

      ObjectStore properties window showing no errors
  9. In the canvas, click Set Payload.

  10. In the Set Payload properties window, set the Value field to:

    KEY: #[payload.key] AND VALUE: #[payload.value] WERE SAVED

    This message appears on the command line to indicate that the application received the curl command values:

    Set Payload properties window showing the Value field
  11. Save your project.

  12. In Package Explorer in Studio, right-click osv2-demo-studio-6 and select Anypoint Platform > Deploy to Cloud.

  13. In the Runtime Manager Deploying Application window:

    1. Select CloudHub from Deployment Target.

    2. Ensure that Use Object Store v2 is selected.

    3. Click Deploy Application:

      Runtime Manager Deploying Application window showing the Deploy Application button
  14. Click Open in Browser to display the app in Runtime Manager.

    If CloudHub displays a permissions error, select a different environment to deploy to.

You have successfully used Studio 6.x to configure a Mule 3.9 app with an object store. Next, test the connection by submitting REST API commands to the Object Store API. Then, use Runtime Manager to view the results.

Test Your Studio 6 Application

After you create your app in Studio 6.x, test it by using a curl command to submit a REST API command to the Object Store API. Then, use Runtime Manager to view the results.

To test your app:

  1. In Runtime Manager, click Applications and ensure that the osv2-demo-studio-6 app is running.

  2. Click the Status column for the osv2-demo-studio-6 app to display its details.

  3. Copy the App url value:

    Runtime Manager app details pane showing the App url
  4. From the command line, run this curl command, replacing APP-URL with the App url value that you copied from Runtime Manager:

    $ curl -X POST -H "Content-Type: application/json" -d '{ "key": "myStudio6Key", "value": "OSv2 Test from Studio 6" }' "http://APP-URL/store";echo

    The output from this command displays the key-value pair that you sent to the object store:

    KEY: myStudio6Key AND VALUE: OSv2 Test from Studio 6 WERE SAVED

    Each time you submit this command, change the key name so that each key is unique.

  5. In Runtime Manager, click Manage application in the details pane for the application:

    Runtime Manager app details pane showing the Manage application button
  6. Select Object Store from the sidebar.

  7. Click the object store, the partition, and then the key:

    Runtime Manager window showing the selected object store, partition, and key
    1 Object store
    2 Selected partition
    3 Selected key
  8. Hover over the …​ icon to display actions for each object:

    Metadata and Delete Value actions in the object store
    1 To display the key value and any metadata associated with the value, hover over the …​ icon and click the metadata icon.
    2 To delete an object store, partition, key, or value, hover over the …​ icon and click the trash can icon.