generate-map-reduce-couchbase/src/main/java/org/gcube/dbtest/dbtest.java

82 lines
2.7 KiB
Java

package org.gcube.dbtest;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.bucket.BucketManager;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.view.DefaultView;
import com.couchbase.client.java.view.DesignDocument;
public class dbtest {
protected static final String DEFAULT_DB = "accounting";
protected static final String DEFAULT_DB_SERVICE = "accounting_service";
protected static final String DEFAULT_DB_STORAGE = "accounting_storage";
protected static final String MAP_REDUCE_PATH_IMPORT = "/home/pieve/Documents/AccountingAnalyticsListMapReduceCouchbase/couchbase/2.0.0/_design/ServiceUsageRecord";
protected static final String URL="http://couchbase01-d-d4s.d4science.org";
protected static final String PASSWORD="accounting";
/* The environment configuration */
protected static final CouchbaseEnvironment ENV = DefaultCouchbaseEnvironment
.builder().queryEnabled(true).build();
public static void main(String[] args) throws Exception{
createMapReduceCouchBase();
}
private static void createMapReduceCouchBase() throws Exception{
Cluster cluster = CouchbaseCluster.create(ENV, URL);
Bucket bucket = cluster.openBucket(DEFAULT_DB_SERVICE,PASSWORD);
BucketManager bucketManager = bucket.bucketManager();
File file = new File(MAP_REDUCE_PATH_IMPORT);
File[] filesInDir = file.listFiles();
Arrays.sort(filesInDir);
Map<String, List> mappe = new HashMap<String, List>();
for(File fs : filesInDir)
{
String nameMapReduce=fs.getName();
String nameViewCustom=fs.getName().split("__")[0];
if (!fs.getName().equals("Invalid")){
String stringMap= new String (Files.readAllBytes(Paths.get(fs.toString()+"/map.js")));
String stringReduce= new String (Files.readAllBytes(Paths.get(fs.toString()+"/reduce.js")));
if (!mappe.containsKey(nameViewCustom))
mappe.put(nameViewCustom, new ArrayList());
System.out.println("add view:"+nameViewCustom+" and map:"+nameMapReduce);
mappe.get(nameViewCustom).add(DefaultView.create(nameMapReduce,stringMap,stringReduce));
}
}
for(Map.Entry<String, List> entry : mappe.entrySet()) {
String key = entry.getKey();
System.out.println("key"+key);
DesignDocument designDoc = DesignDocument.create(key,entry.getValue());
bucketManager.insertDesignDocument(designDoc);
}
cluster.disconnect();
}
}