89 lines
3.0 KiB
Java
89 lines
3.0 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;
|
|
import com.couchbase.client.java.view.View;
|
|
|
|
public class dbtest {
|
|
|
|
protected static final String DEFAULT_DB = "accounting";
|
|
|
|
protected static final String DEFAULT_DB_SERVICE = "ServiceUsageRecord";
|
|
protected static final String DEFAULT_DB_STORAGE = "accounting_storage";
|
|
|
|
protected static final String URL = "http://couchbase01.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 currentDirectory = new File(".");
|
|
File srcDirectory = new File(currentDirectory, "src");
|
|
File mainDirectory = new File(srcDirectory, "main");
|
|
File resourcesDirectory = new File(mainDirectory, "resources");
|
|
File mapReduceDirectory = new File(resourcesDirectory, "MapReduce");
|
|
File file = new File(mapReduceDirectory, "ServiceUsageRecord");
|
|
|
|
System.out.println(file.getAbsolutePath());
|
|
|
|
|
|
File[] filesInDir = file.listFiles();
|
|
Arrays.sort(filesInDir);
|
|
Map<String, List<View>> maps = new HashMap<String, List<View>>();
|
|
|
|
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 (!maps.containsKey(nameViewCustom)){
|
|
maps.put(nameViewCustom, new ArrayList<View>());
|
|
}
|
|
|
|
System.out.println("add view:" + nameViewCustom + " and map:" + nameMapReduce);
|
|
View view = DefaultView.create(nameMapReduce, stringMap, stringReduce);
|
|
maps.get(nameViewCustom).add(view);
|
|
}
|
|
}
|
|
|
|
for (Map.Entry<String, List<View>> entry : maps.entrySet()) {
|
|
|
|
String key = entry.getKey();
|
|
System.out.println("key" + key);
|
|
DesignDocument designDoc = DesignDocument.create(key, entry.getValue());
|
|
bucketManager.insertDesignDocument(designDoc);
|
|
|
|
}
|
|
|
|
cluster.disconnect();
|
|
}
|
|
|
|
}
|