Importing Rules Into Rules-Master

While the Rules-Master User Interface is a perfectly acceptable way to implement rules, there are some instances where you would like to maintain your rules in a separate document and import them in batch into your rules master collection.  To that end, I've put together a rudimentary example of how you would go about accomplishing this task.  

To start, let's grab the headers for our sample csv document:


And our sample rule entry, which follows the above header pattern:


For this example I'm creating a simple redirect based on matching keywords, in this case "rfid." 

Now let's have a look at our code.  For this example, I'm using Java with the Apache HttpClient, Tika and OpenCSV as my dependencies.  

import java.util.Iterator;
import java.util.LinkedHashMap;
import net.arnx.jsonic.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;

 * Sample Output
 * {"display_type":"Redirect","ruleName":"redirect-test","description":"","matching":"keywords","values":[""],"param_keys":[],"param_values":[],"param_policies":[],"createdAt":"2017-04-13T15:13:04.182Z","updatedAt":"2017-04-13T15:13:04.182Z","enabled":[true],"type":"response_value","keys":["redirect"],"search_terms":"rfid","filters":""}
public class RulesMasterCSVReader {

    private static  HttpClient httpClient = null;

    public static void main(String[] args) {
        try {
// First, let's set up our parameters: String collectionName = "default";// the collection to which these rules apply String filePath = "rules_test.csv";// full path to .csv file String postUrl = "http://localhost:8764/api/apollo/solr/" + collectionName + "_rules/update/json/docs?commit=true"; // now let's spin up our HttpClient, WITH user and pwd:
CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("solr", "SolrRocks0"); provider.setCredentials(AuthScope.ANY, credentials); httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(provider) .build();
// Now read the CSV file: CSVReader reader = new CSVReader(new FileReader(filePath)); String[] nextLine; int count = 0; String[] headers = new String[0]; LinkedHashMap<String, String> map = null; while ((nextLine = reader.readNext()) != null) { // nextLine[] is an array of values from the line System.out.println(nextLine[0] + nextLine[1] + " etc..."); if (count == 0) { headers = nextLine;// get the headers } else { map = getRuleData(headers, nextLine); String json = getRuleJson(map); postData(postUrl, json); // post the data } count++; } } catch (Exception e) { e.printStackTrace(); } } private static LinkedHashMap<String, String> getRuleData(String[] headers, String[] data) { LinkedHashMap<String, String> map = new LinkedHashMap<>(); for (int i = 0; i < headers.length; i++) { map.put(headers[i], data[i]); } return map; } private static String getRuleJson(LinkedHashMap<String, String> data) { String payload = "{"; payload = JSON.encode(data, true); return payload; } private static void postData(String url, String json) throws Exception { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(json); request.addHeader("Content-Type", "application/json;charset=UTF-8"); request.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"); request.addHeader("Cookie", "id=34560925-677c-4c71-b12f-03cf520f9ea1; doc-sidebar=280px"); request.setEntity(params); HttpResponse response = httpClient.execute(request); System.out.println("Response: "+response); } }

Breaking it down

So what is going on....

There is one main method in our class which launches the entire process, and one try/catch within that method.  I've defined 3 private support methods. 

  1. getRuleData takes the headers and a line of data and returns a LinkedHashmap.
  2. getRuleJson:  This takes a LinkedHashMap and returns a JSON string.  
  3. postData:  POSTs the data to the Rules collection via the Fusion API. 

Initially, we read the CSV file and iterate over the lines.  If out count is zero, we assume the headers line will be that line, and set that as the headers array.  From there we iterate over each data line, create a LinkedHashMap from it using our headers array, and post that data to Fusion.  

Have more questions? Submit a request


Please sign in to leave a comment.
Powered by Zendesk