Getting Data from SAP Commerce Cloud into Data Hub
The Data Hub Outbound Service works together with the CSV Web Service extension. These services give you a ready-built path for easily getting CSV-formatted data into Data Hub.
When moving data from SAP Commerce Cloud to Data Hub, ensure that it is either an at most once delivery (automatic) or exactly once (with intervention). There is a process framework on SAP Commerce Cloud, and there is a process instance created for each order. The customer can retry/rerun the process with a certain status manually if the initial send to Data Hub fails. Some additional custom code could also be added to SAP Commerce Cloud (for example CronJob) to re-run processes that have hung.
Configuring Data Hub Outbound Service
To use Data Hub Outbound Service, configure Data Hub Adapter to communicate with your Data Hub instance. Also, it must not include the CSV Data Hub extension.
Data Hub Outbound Service is provided as a Spring bean named DataHubOutboundService. To use this bean, wire it as a property into a Spring bean. The Spring bean should be in the custom extension you are developing for the SAP Commerce Cloud.
Using Data Hub Outbound Service
The dataHubOutboundService.sendToDataHub method is overloaded with several signatures, allowing a Java object, a Map<String, Object>, or a List<Map<String, Object>> to be sent to Data Hub as CSV data. The Javadoc for dataHubOutboundService provides additional details. The DataHubOutboundService does not support sending collections to Data Hub, and the collections are ignored by the service. Dates are handled specially as documented later in this topic.
Sending an Object to Data Hub
In the following example, the TestProductData class is a typical DTO. The name of each non-null, non-collection property appends to the CSV header, and the string value of each property is appended to the CSV body.
final String FEED_NAME = "DEFAULT_FEED"; // the Feed Name must be a valid Feed in the target Data Hub final String RAW_TYPE = "TestRawProduct"; // the Raw Type must be a valid Raw Type in the target Data Hub final TestProductData testProductDto = new TestProductData(); // set properties of the DTO ... final int numberExpected = 1; try { final ResultData result = dataHubOutboundService.sendToDataHub(FEED_NAME, RAW_TYPE, testProductDto); if(result.getNumberProcessed() != numberExpected) { //handle exception } } catch(final DataHubCommunicationException ex) { throw IllegalStateException("Could not communicate with the Data Hub server",ex); } catch(final DataHubOutboundException ex) { throw IllegalArgumentException("Could not create raw items"); }
Sending a Map to Data Hub
Each Map.Entry key in the Map<String, Object> is appended to the CVS header, and the string value of each Map.Entry value is appended to the CSV body.
final String FEED_NAME = "DEFAULT_FEED"; // the Feed Name must be a valid Feed in the target Data Hub final String RAW_TYPE = "TestRawProduct"; // the Raw Type must be a valid Raw Type in the target Data Hub final private Map<String, Object> testProductMap; // populate the Map with data dataHubOutboundService.sendToDataHub(FEED_NAME, RAW_TYPE, testProductMap);
Sending a List of Maps to Data Hub
Each Map in the List should contain the same keys. The CSV header is populated using the keys from the first Map in the List. The values from each Map are appended as a line of CSV data.
final String FEED_NAME = "DEFAULT_FEED"; // the Feed Name must be a valid Feed in the target Data Hub final String RAW_TYPE = "TestRawProduct"; // the Raw Type must be a valid Raw Type in the target Data Hub final private Map<String, Object> testProductMap1; final private Map<String, Object> testProductMap2; // populate the Maps with data final List<Map<String, Object>> testProductList = Arrays.asList(testProductMap1, testProductMap2); dataHubOutboundService.sendToDataHub(FEED_NAME, RAW_TYPE, testProductList);
Deleting Data from Data Hub
The DataHubOutboundService also supports deletion of canonical items in Data Hub. A single canonical item that has been successfully composed (status=SUCCESS) can be deleted by calling the deleteItem method. The canonical item to be deleted is identified by specifying all of the attributes (and their values) that compose its primary key and its type. The method is overloaded to delete from the GLOBAL pool or any pool specified (see the following examples).
Delete Canonical Item from the GLOBAL Pool
//create a map that holds all of the primary key/value pairs (keys have to match attribute names of your extension)
final Map<String, String> primaryKeyMap = new HashMap<>(2);
primaryKeyMap.put("UPC", "123");
primaryKeyMap.put("SKU", "567");
final String CANONICAL_TYPE = "CanonicalProduct";
final ResultData result = outboundService.deleteItem(CANONICAL_TYPE, primaryKeyMap);
Delete Canonical Item from a Named Pool
//create a map that holds all of the primary key/value pairs (keys have to match attribute names of your extension)
final Map<String, String> primaryKeyMap = new HashMap<>(2);
primaryKeyMap.put("UPC", "123");
primaryKeyMap.put("SKU", "567");
final String CANONICAL_TYPE = "CanonicalProduct";
final String POOL_NAME = "MyTestPool";
final ResultData result = outboundService.deleteItem(POOL_NAME, CANONICAL_TYPE, primaryKeyMap);
One or more canonical items that have been successfully composed can also be deleted. The deletion is based on the data feed and raw type of the raw items they originated from by calling the dataHubOutboundService.deleteByFeed method. The canonical item or items to be deleted can be identified by specifying a data feed name and raw type. This results in the deletion of any canonical items in the pool. The canonical items have to be currently associated with the data feed that originated from the raw items of the given raw item type.
You can further qualify the canonical items to be deleted. The original raw item (source of the canonical items to be deleted) can be specified through an optional map of raw attribute names and values. Only if all canonical items are successfully composed from the raw item, do any of the corresponding canonical items get deleted. For example, the raw item is missing primary key fields resulting in the composed canonical items having incomplete integration keys.
An example is shown in the following of deleting canonical items by only the data feed name and raw item type. The canonical item is also chosen by specifying a raw item with a map of raw item attributes.
Delete Canonical Items Based only on a Data Feed and Raw Item Type
final String RAW_TYPE = "RawProduct"; final String DATA_FEED_NAME = "productFeed"; final ResultData result = outboundService.deleteByFeed(DATA_FEED_NAME, RAW_TYPE);
Delete Canonical Items Based on a Data Feed and Raw Item
//create a map that holds key/value pairs (keys have to match attribute names of your extension) of a specific raw item
final Map<String, Object> rawAttrMap = new HashMap<>(4);
rawAttrMap.put("SKU", new Integer(567));
rawAttrMap.put("category","shirts");
rawAttrMap.put("name","Mens large long-sleeve");
rawAttrMap.put("isoCode","en");
final String RAW_TYPE = "RawProduct";
final String DATA_FEED_NAME = "shirtsFeed";
final ResultData result = outboundService.deleteByFeed(DATA_FEED_NAME, RAW_TYPE, rawAttrMap);
Formatting Dates for Data Hub
Dates are converted to UTC time zone and pattern formatted before sending to Data Hub.
# Dates will be converted to String and formatted using the specific pattern and the UTC time zone datahubadapter.datahuboutbound.date.pattern=yyyy-MM-dd HH:mm:ss.S