# Recombee API Client
A Java client (SDK) for easy use of the [Recombee](https://www.recombee.com/) recommendation API.
If you don't have an account at Recombee yet, you can create a free account [here](https://www.recombee.com/).
Documentation of the API can be found at [docs.recombee.com](https://docs.recombee.com/).
## Installation
The client is available in the [Maven Central Repository](https://mvnrepository.com/artifact/com.recombee/api-client/), so you just need to add the following `` entry to your project's POM:
```xml
com.recombee
api-client
6.3.0
```
## Examples
### Basic example
Examples are located in [src/examples](https://github.com/Recombee/java-api-client/tree/master/src/examples/java/com/recombee/api_client/examples/).
```java
package com.recombee.api_client.examples;
import com.recombee.api_client.RecombeeClient;
import com.recombee.api_client.util.Region;
import com.recombee.api_client.api_requests.*;
import com.recombee.api_client.bindings.RecommendationResponse;
import com.recombee.api_client.bindings.Recommendation;
import com.recombee.api_client.exceptions.ApiException;
import java.util.ArrayList;
import java.util.Random;
public class BasicExample {
public static void main(String[] args) {
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.US_WEST);
try {
final int NUM = 100;
// Generate some random purchases of items by users
final double PROBABILITY_PURCHASED = 0.1;
Random r = new Random();
ArrayList addPurchaseRequests = new ArrayList();
for (int i = 0; i < NUM; i++)
for (int j = 0; j < NUM; j++)
if (r.nextDouble() < PROBABILITY_PURCHASED) {
AddPurchase request = new AddPurchase(String.format("user-%s", i),String.format("item-%s", j))
.setCascadeCreate(true); // Use cascadeCreate parameter to create
// the yet non-existing users and items
addPurchaseRequests.add(request);
}
System.out.println("Send purchases");
client.send(new Batch(addPurchaseRequests)); //Use Batch for faster processing of larger data
// Get 5 recommendations for user 'user-25'
RecommendationResponse recommendationResponse = client.send(new RecommendItemsToUser("user-25", 5));
System.out.println("Recommended items:");
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
// User scrolled down - get next 3 recommended items
recommendationResponse = client.send(new RecommendNextItems(recommendationResponse.getRecommId(), 3));
System.out.println("Next recommended items:");
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
} catch (ApiException e) {
e.printStackTrace();
//use fallback
}
}
}
```
### Using property values
```java
package com.recombee.api_client.examples;
import com.recombee.api_client.RecombeeClient;
import com.recombee.api_client.util.Region;
import com.recombee.api_client.api_requests.*;
import com.recombee.api_client.bindings.RecommendationResponse;
import com.recombee.api_client.bindings.Recommendation;
import com.recombee.api_client.bindings.SearchResponse;
import com.recombee.api_client.exceptions.ApiException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class ItemPropertiesExample {
public static void main(String[] args) {
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.AP_SE);
try {
client.send(new ResetDatabase()); // Clear everything from the database
/*
We will use computers as items in this example
Computers have four properties
- price (floating point number)
- number of processor cores (integer number)
- description (string)
- image (url of computer's photo)
*/
client.send(new AddItemProperty("price", "double"));
client.send(new AddItemProperty("num-cores", "int"));
client.send(new AddItemProperty("description", "string"));
client.send(new AddItemProperty("image", "image"));
// Prepare requests for setting a catalog of computers
final ArrayList requests = new ArrayList();
final int NUM = 100;
final Random rand = new Random();
for(int i=0; i