Skip to content

Instantly share code, notes, and snippets.

@binarytemple
Created June 5, 2015 19:49
Show Gist options
  • Select an option

  • Save binarytemple/73d71bba065e871d14ec to your computer and use it in GitHub Desktop.

Select an option

Save binarytemple/73d71bba065e871d14ec to your computer and use it in GitHub Desktop.
package com.vbrick.riak;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import org.apache.commons.io.FileUtils;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.StoreValue;
import com.basho.riak.client.api.commands.search.StoreIndex;
import com.basho.riak.client.api.commands.search.StoreSchema;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakNode;
import com.basho.riak.client.core.operations.SearchOperation;
import com.basho.riak.client.core.operations.StoreBucketPropsOperation;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.RiakObject;
import com.basho.riak.client.core.query.search.YokozunaIndex;
import com.basho.riak.client.core.query.search.YokozunaSchema;
import com.basho.riak.client.core.util.BinaryValue;
public class RiakSearchTest {
//public static final String RIAK_SERVER = "dev-u1204-riak-99.lab.vb.loc";
public static final String RIAK_SERVER = "dev-u1204-ha-28.lab.vb.loc";
public static final String SEARCH_SCHEMA_PATH = "/Users/slohiya/dev/Java/Riak/riak-test/src/main/java/com/vbrick/riak";
public static final String SEARCH_SCHEMA_NAME = "solrtestschema";
public static final String BUCKET_TYPE = "search_test";
public static final String BUCKET = "search_test_bucket";
public static final String SEARCH_INDEX = "search_index";
public static final String CONTENT_TYPE = "application/json";
public static void main(String[] args) {
try {
//setup search
if (args != null && args[0].equalsIgnoreCase("searchSetup"))
{
System.out.println("Setting up Riak Search....");
setupSearch();
System.out.println("Completed Setting up Riak Search....");
}
//setup search
else if (args != null && args[0].equalsIgnoreCase("bucketCreate"))
{
System.out.println("Create Bucket....");
createBucket();
System.out.println("Completed Create Bucket....");
}
else if (args != null && args[0].equalsIgnoreCase("addData"))
{
System.out.println("Adding Data....");
addTestData();
System.out.println("Completed Adding Data....");
}
else if (args != null && args[0].equalsIgnoreCase("addAnotherTestData"))
{
System.out.println("Adding Another Data....");
addAnotherTestData();
System.out.println("Completed Adding Another Data....");
}
else if (args != null && args[0].equalsIgnoreCase("uploadSchema"))
{
System.out.println("Uploading Search Schema....");
uploadSchema();
System.out.println("Completed Uploading Search Schema....");
}
else
{
System.out.println("Running Riak Search....");
query();
System.out.println("Completed Running Riak Search....");
}
} catch (IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
static void setupSearch() throws IOException, ExecutionException, InterruptedException
{
uploadSchemaToRiak(SEARCH_SCHEMA_NAME);
createIndex(SEARCH_SCHEMA_NAME, SEARCH_INDEX);
}
static void uploadSchema() throws IOException, ExecutionException, InterruptedException
{
uploadSchemaToRiak(SEARCH_SCHEMA_NAME);
}
static void createBucket() throws IOException, ExecutionException, InterruptedException
{
createBucketWithIndex(BUCKET_TYPE, BUCKET, SEARCH_INDEX);
}
static void addTestData() throws UnknownHostException, ExecutionException, InterruptedException
{
//String id = UUID.randomUUID().toString();
String id = "1";
String data = getJsonTestData(id, "shailesh", "lohiya");
addData(BUCKET_TYPE, BUCKET, id, CONTENT_TYPE, data);
}
static void addAnotherTestData() throws UnknownHostException, ExecutionException, InterruptedException
{
//String id = UUID.randomUUID().toString();
String id = "2";
String data = getJsonTestData(id, "shailesh-2", "lohiya-2");
addData(BUCKET_TYPE, BUCKET, id, CONTENT_TYPE, data);
}
static void query() throws UnknownHostException, ExecutionException, InterruptedException
{
//query
SearchOperation searchOp = new SearchOperation
//.Builder(BinaryValue.create(SEARCH_INDEX), "fname:sh*")
.Builder(BinaryValue.create(SEARCH_INDEX), "lname:*")
.build();
getRiakCluster().execute(searchOp);
// This will display the actual results as a List of Maps:
List<Map<String, List<String>>> results = searchOp.get().getAllResults();
// This will display the number of results:
System.out.println(results);
}
static void uploadSchemaToRiak(String schemaName) throws IOException, ExecutionException, InterruptedException
{
File xml = new File(SEARCH_SCHEMA_PATH + "/" + schemaName + ".xml");
String xmlString = FileUtils.readFileToString(xml);
YokozunaSchema schema = new YokozunaSchema(schemaName, xmlString);
StoreSchema storeSchemaOp = new StoreSchema.Builder(schema).build();
getRiakClient().execute(storeSchemaOp);
}
static void createIndex(String schemaName, String indexName) throws UnknownHostException, ExecutionException, InterruptedException
{
YokozunaIndex index = new YokozunaIndex(indexName, schemaName);
StoreIndex storeIndex = new StoreIndex.Builder(index)
.build();
getRiakClient().execute(storeIndex);
}
static void createBucketWithIndex(String bucketType, String bucket, String indexName) throws UnknownHostException, ExecutionException, InterruptedException
{
Namespace bucketName = new Namespace(bucketType, bucket);
StoreBucketPropsOperation storePropsOp = new StoreBucketPropsOperation.Builder(bucketName)
.withSearchIndex(indexName)
.build();
getRiakCluster().execute(storePropsOp);
}
static void addData(String bucketType, String bucket, String key, String contentType, String value) throws UnknownHostException, ExecutionException, InterruptedException
{
Namespace ns = new Namespace(bucketType, bucket);
Location location = new Location(ns, key);
RiakObject riakObject = new RiakObject();
riakObject.setContentType(contentType);
riakObject.setValue(BinaryValue.create(value));
StoreValue store = new StoreValue.Builder(riakObject).withLocation(location).build();
getRiakClient().execute(store);
}
static String getJsonTestData(String id, String firstName, String lastName)
{
String val = "{ "
+ "\"Id\": \"" + id + "\""
+ ", \"fname\": \"" + firstName + "\""
+ ", \"lname\": \"" + lastName + "\""
+ " }";
return val;
}
static RiakClient getRiakClient() throws UnknownHostException
{
RiakClient client =
RiakClient.newClient(RIAK_SERVER);
return client;
}
private static RiakCluster getRiakCluster() throws UnknownHostException {
RiakNode node = new RiakNode.Builder()
.withRemoteAddress(RIAK_SERVER)
.build();
// This cluster object takes our one node as an argument
RiakCluster cluster = new RiakCluster.Builder(node)
.build();
// The cluster must be started to work, otherwise you will see errors
cluster.start();
return cluster;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment