Tutorial: Using the Data Hub Adapter
Publishing data to SAP Commerce Cloud using the Data Hub Adapter is one of the primary use cases for Data Hub. We demonstrate this use case here.
Prerequisites
For this tutorial to be successful, first do the following:
- Ensure the data models, dataset, and other files used in the previous tutorials are available. The data models, which are the extension.jar files, are installed in the /opt/datahub/extensions directory.
- Remove any of the adapters (File Adapter or Test Adapter) used in previous tutorials from the /opt/datahub/extensions directory. This action ensures that Data Hub publishes to the target system as configured.
Context
This tutorial is optional, as it requires a working SAP Commerce Cloud installation to succeed. If you are able to install SAP Commerce Cloud, then you are encouraged to complete the tutorial.
Procedure
- Install SAP Commerce Cloud using the provided installer recipe.
- If you have not already done so, download and unpack the SAP Commerce Cloud software package somewhere on your local file system.
- Download the file dhtutorial.zip from the Solution Book
- Unzip the contents of the file into the directory <HYBRIS_HOME>/hybris/installer/recipes.
- Open a command line terminal, cd into the <HYBRIS_HOME>/installer directory, and run the installer with ./install.sh -r dhtutorial perform
This command compiles, initializes, and starts SAP Commerce Cloud. When SAP Commerce Cloud has successfully started, you see the following in your terminal:
Jul 13, 2016 3:33:28 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 152624 ms
The installation takes some time to complete. Once complete, you can proceed to the next step.
- Log into the SAP Commerce Cloud Administration Console at https://localhost:9002 using the admin user and associated password.
- Initialize SAP Commerce Cloud manually to remove any default project data.
- From the Platform tab, choose Initialize
- Deselect the checkbox labeled Toggle All under Project Settings. This step ensures that no default project data is loaded during initialization, and that you have a clean database.
- Click on the Initialize button to start the initialization process. Click OK when asked to confirm. The initialization process takes a few minutes.
Once completed, you see the following at the bottom of the initialization page, indicating initialization was successful.
- Import the core Impex that is required to create the product catalogs, using the SAP Commerce Cloud Administration Console import function.
- Copy the ImpEx provided in the following code block.
$productCatalog=apparelProductCatalog $catalogVersion=catalogversion(catalog(id[default=$productCatalog]),version[default='Staged'])[unique=true,default=$productCatalog:Staged] $languages=en,de # Product catalog INSERT_UPDATE Catalog;id[unique=true] ;$productCatalog # Product versions for product catalogs INSERT_UPDATE CatalogVersion;catalog(id)[unique=true];version[unique=true];active;languages(isoCode);readPrincipals(uid) ;$productCatalog;Staged;false;$languages;employeegroup ;$productCatalog;Online;true;$languages;employeegroup
- Navigate to ImpEx Import under the Console tab.
- Paste the ImpEx into the Import Content window.
- Click the Import Content button. You should see the message "Import finished successfully".
- Copy the ImpEx provided in the following code block.
- Check that you have no products in the SAP Commerce Cloud catalog.
- Log in to SAP Commerce Cloud Backoffice in your browser, at https://localhost:9002/backoffice using the admin user and password combination, as before.
- In the left-hand menu, choose
Catalog
Products
If the product catalog is empty, you should see No entries in the main window.
If you see products listed here, start again from the initialization step, and deselect all project settings using Toggle All.
- Create a new target data model specifically for SAP Commerce Cloud. The new extension JAR replaces the target JAR you have used for previous tutorials.
- Save the following XML to the file base-target-datahub-extension.xml, replacing its previous content.
<extension xmlns="http://www.hybris.com/schema/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hybris.com/schema/ datahub-metadata-schema-1.0.0.xsd" name="base-target-datahub-extension"> <dependencies> <dependency> <extension>base-canonical-datahub-extension</extension> </dependency> </dependencies> <targetSystems> <targetSystem> <name>HybrisCoreInstallation</name> <type>HybrisCore</type> <exportURL>${datahub.extension.exportURL}</exportURL> <userName>${datahub.extension.userName}</userName> <password>${datahub.extension.password}</password> <exportCodes> <exportCode> $catalogVersion=catalogversion(catalog(id[default=apparelProductCatalog]),version[default='Staged'])[unique=true,default=apparelProductCatalog:Staged] </exportCode> <exportCode> $baseProduct=baseProduct(code,catalogVersion(catalog(id[default='apparelProductCatalog']),version[default='Staged'])) </exportCode> </exportCodes> <!-- Target items and attributes --> <targetItems> <item> <type>ApparelProduct</type> <exportCode>ApparelProduct</exportCode> <description>Hybris Platform representation of apparel product</description> <updatable>true</updatable> <canonicalItemSource>CanonicalProduct</canonicalItemSource> <status>ACTIVE</status> <attributes> <attribute> <name>code</name> <localizable>false</localizable> <collection>false</collection> <exportCode>code[unique=true]</exportCode> <transformationExpression>identifier</transformationExpression> <mandatoryInHeader>true</mandatoryInHeader> </attribute> <attribute> <name>name</name> <localizable>false</localizable> <collection>false</collection> <exportCode>name</exportCode> <transformationExpression>name</transformationExpression> </attribute> <attribute> <name>description</name> <localizable>false</localizable> <collection>false</collection> <exportCode>description</exportCode> <transformationExpression>description</transformationExpression> </attribute> <attribute> <name>ean</name> <localizable>false</localizable> <collection>false</collection> <exportCode>ean</exportCode> <transformationExpression>barCode</transformationExpression> </attribute> <attribute> <name>catalogVersion</name> <localizable>false</localizable> <collection>false</collection> <transformationExpression>""</transformationExpression> <exportCode>$catalogVersion</exportCode> <mandatoryInHeader>true</mandatoryInHeader> </attribute> </attributes> </item> </targetItems> </targetSystem> </targetSystems> </extension>We are importing the products into the product catalog you defined in step 4. The target system export codes that appear before the target items and attributes are required for generating appropriate ImpEx headers for the catalog.
- Copy the file into the META-INF directory.
- Run the command jar cf base-target-datahub-extension.jar META-INF to compress the file into an extension JAR.
- Copy the JAR file to the /opt/datahub/extensions directory.
- Save the following XML to the file base-target-datahub-extension.xml, replacing its previous content.
- Open a second command line terminal, and start Data Hub to load the new extension.
- In a third command line, load the data in the base_unique_items.csv file using the following command:
curl -k -u admin:<password> --data-binary @base_unique_items.csv -H "Content-Type:application/octet-stream" http://localhost:8080/datahub-webapp/v1/data-feeds/DEFAULT_FEED/items/RawProduct --verbose
- Compose the items you have just loaded into the GLOBAL pool with the following command:
curl -k -u admin:<password> --request POST http://localhost:8080/datahub-webapp/v1/pools/GLOBAL/compositions
- Publish the composed data to SAP Commerce Cloud with the following command:
curl -k -u admin:<password> --data-binary @datafile3 -H "Content-Type:application/json" --request POST http://localhost:8080/datahub-webapp/v1/pools/GLOBAL/publications
At your command line you see the following returned:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <publicationactiondata> <actionid>2</actionid> <type>PUBLICATION</type> <status>PENDING</status> <poolname>GLOBAL</poolname> <targetsystempublications> <publicationid>1</publicationid> <status>PENDING</status> <targetsystemname>FileAdapterTargetSystem</targetsystemname> <actionid>1</actionid> <poolname>GLOBAL</poolname> <numberoferrors>0</numberoferrors> </targetsystempublications> </publicationactiondata>
In the terminal window where you started SAP Commerce Cloud you see output similar to the following:
INFO [hybrisHTTP31] [ItemImportController] Received request PUT: /import/pools/GLOBAL/publications/2 INFO [hybrisHTTP31] [ItemImportController] resultCallbackUrl: http://localhost:8080/datahub-webapp/v1/core-publications/2 WARN [hybrisHTTP31] [DefaultSLDItemDAO] Error decrypting property 'encodedPassword' - probably not encrypted? Using raw value! INFO [TaskExecutor-master-386-Task [8796093088694]] [ImpexDataImportClient] Requesting data from resource http://localhost:8080/datahub-webapp/v1/core-publications/2/ApparelProduct.txt?targetName=HybrisCoreInstallation&locale=&fields=catalogVersion,ean,description,name,code&lastProcessedId=0&pageSize=1000 INFO [TaskExecutor-master-386-Task [8796093088694]] [ImpexDataImportClient] Requesting data from resource http://localhost:8080/datahub-webapp/v1/core-publications/2/ApparelProduct.txt?targetName=HybrisCoreInstallation&locale=&fields=catalogVersion,ean,description,name,code&lastProcessedId=1000&pageSize=1000 INFO [TaskExecutor-master-386-Task [8796093088694]] [DefaultImportService] Starting import synchronous using cronjob with code=0000002O INFO [TaskExecutor-master-386-Task [8796093088694]] (0000002O) [ImpExImportJob] Starting ImpEx cronjob "ImpEx-Import" INFO [TaskExecutor-master-386-Task [8796093088694]] (0000002O) [Importer] Finished 1 pass in 0d 00h:00m:01s:477ms - processed: 1000, no lines dumped (last pass 0) INFO [TaskExecutor-master-386-Task [8796093088694]] [DefaultImportService] Import was successful (using cronjob with code=0000002O) INFO [TaskExecutor-master-386-Task [8796093088694]] [ImpexDataImportClient] Returning SUCCESS to http://localhost:8080/datahub-webapp/v1/core-publications/2 INFO [TaskExecutor-master-386-Task [8796093088694]] [ImpexDataImportClient] Response status from http://localhost:8080/datahub-webapp/v1/core-publications/2: 200
In the terminal window where you started Data Hub you see output similar to the following:
2016-07-14 12:43:33,788 [INFO] [c.h.d.r.r.PoolActionResource] Received POST request /publications with args [GLOBAL,org.glassfish.jersey.server.internal.routing.UriRoutingContext@214af627,PublicationActionData{targetSystemPublications=[TargetSystemPublicationData{publicationId=null, startTime=null, endTime=null, status='null', targetSystemName='HybrisCoreInstallation', actionId=null, poolName='null', numberOfErrors=0, canonicalItemCount=null}]PoolActionData{actionId=null, type='PUBLICATION', startTime=null, endTime=null, status='null', poolName='GLOBAL'}}] 2016-07-14 12:43:33,790 [DEBUG] [c.h.d.s.i.DefaultEventPublicationService] Publishing data hub event : PerformPublicationEvent{actionId=2, systemUrl='http://localhost:8080/datahub-webapp/v1', targetPublicationIds=[4]} 2016-07-14 12:43:33,790 [DEBUG] [c.h.d.p.i.DefaultProcessMonitor] Registered Publication #2: 1 processes running 2016-07-14 12:43:33,790 [DEBUG] [c.h.d.s.i.DefaultEventPublicationService] Publishing data hub event : PublicationStartedEvent{actionId=2} 2016-07-14 12:43:33,792 [DEBUG] [c.h.d.s.i.DefaultPublicationActionService] Pinging target system HybrisCoreInstallation at http://localhost:9001/datahubadapter for target system publication id 4 and pool GLOBAL. 2016-07-14 12:43:33,808 [DEBUG] [c.h.d.s.p.i.DefaultCanonicalMetadataDependencyResolver] Creating target items from canonical item types in order: [CanonicalItemMetadataEntity{id=1, itemMetadataId=2, itemType='CanonicalProduct'}] 2016-07-14 12:43:33,808 [DEBUG] [c.h.d.s.i.DefaultTargetItemService] Creating Target Items for type: 'CanonicalProduct', and Target System: HybrisCoreInstallation 2016-07-14 12:43:33,813 [DEBUG] [c.h.d.s.i.DefaultTargetItemService] Creating Target Items for type: 'CanonicalProduct', and Target System: HybrisCoreInstallation 2016-07-14 12:43:33,856 [DEBUG] [c.h.d.s.i.DefaultTargetItemService] Creating Target Items for type ApparelProduct from 1000 Canonical Items in Pool GLOBAL . . . 2016-07-14 12:43:35,503 [INFO] [c.h.d.r.r.CoreAdapterResource] Core adapter completes publication 5 with status SUCCESS 2016-07-14 12:43:35,506 [DEBUG] [c.h.d.s.i.DefaultPublicationActionService] Publication Result for publication 5 with status SUCCESS 2016-07-14 12:43:35,539 [DEBUG] [c.h.d.s.p.i.DefaultPublicationRetryService] Removed 0 retries for items published in CompositeTargetSystemPublicationEntity{id=4, status=IN_PROGRESS, publicationType=null, publicationAction=PublicationActionEntity{id=2, pool=DataHubPoolEntity{id=0, name=GLOBAL}}, targetSystem=TargetSystemEntity{id=1, targetSystemName='HybrisCoreInstallation', targetSystemType='HybrisCore'}} 2016-07-14 12:43:35,541 [DEBUG] [c.h.d.s.i.DefaultEventPublicationService] Publishing data hub event : com.hybris.datahub.api.event.DeletePublicationRetryEvent@5ed4aa7[] 2016-07-14 12:43:35,542 [WARN] [c.h.d.s.p.i.DeletePublicationRetryEventListener] deleting 0 publication retries from target system id 1 2016-07-14 12:43:35,544 [DEBUG] [c.h.d.s.p.i.DefaultDataHubPublicationService] setting publication status to SUCCESS 2016-07-14 12:43:35,547 [DEBUG] [c.h.d.p.i.DefaultProcessMonitor] Unregistered Publication #2, 0 processes running 2016-07-14 12:43:35,547 [DEBUG] [c.h.d.s.i.DefaultEventPublicationService] Publishing data hub event : PublicationCompletedEvent{actionId=2} 2016-07-14 12:43:35,547 [DEBUG] [c.h.d.s.i.DefaultEventPublicationService] Publishing data hub event : TargetSystemPublicationCompletedEvent{publicationId=4} - Check that the product data has been successfully imported into SAP Commerce Cloud. Click on
Catalog
Products
in the SAP Commerce Cloud Backoffice once again to refresh the view.
Results