HBase and MongoDB Plugin update

This commit is contained in:
rzs840707 2014-08-27 16:26:41 +08:00
parent 8fd8b0a890
commit 7626aca95f
2 changed files with 159 additions and 91 deletions

View File

@ -1,16 +1,12 @@
package org.bench4q.agent.plugin.basic.HBasePlugin; package org.bench4q.agent.plugin.basic.HBasePlugin;
import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.share.exception.Bench4QRunTimeException;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Put;
@ -22,6 +18,13 @@ import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.share.exception.Bench4QRunTimeException;
@Plugin("HBase") @Plugin("HBase")
public class HBasePlugin { public class HBasePlugin {
@ -47,7 +50,7 @@ public class HBasePlugin {
HBaseAdmin admin = new HBaseAdmin(config); HBaseAdmin admin = new HBaseAdmin(config);
tableUnderTest = new HTable(conf, tableName); tableUnderTest = new HTable(conf, tableName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); Logger.getLogger(HBasePlugin.class).error(e, e);
throw new Bench4QRunTimeException("Construct HBasePlugin fails!", e); throw new Bench4QRunTimeException("Construct HBasePlugin fails!", e);
} }
} }
@ -61,11 +64,11 @@ public class HBasePlugin {
put.add("key1".getBytes(), "key1".getBytes(), key.getBytes()); put.add("key1".getBytes(), "key1".getBytes(), key.getBytes());
put.add("value1".getBytes(), "value1".getBytes(), value.getBytes()); put.add("value1".getBytes(), "value1".getBytes(), value.getBytes());
this.tableUnderTest.put(put); this.tableUnderTest.put(put);
return new HBaseReturn(true);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(HBasePlugin.class).info(ex, ex); Logger.getLogger(HBasePlugin.class).info(ex, ex);
return new HBaseReturn(false); return new HBaseReturn(false);
} }
return new HBaseReturn(true);
} }
@Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR) @Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR)
@ -73,17 +76,83 @@ public class HBasePlugin {
@Parameter(value = "key", type = SupportTypes.Field) String key) { @Parameter(value = "key", type = SupportTypes.Field) String key) {
try { try {
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("key1"), Filter filter = new SingleColumnValueFilter(Bytes.toBytes("key1"),
Bytes.toBytes("key1"), CompareOp.EQUAL, Bytes.toBytes(key)); // µ±ÁÐcolumn1µÄֵΪaaaʱ½øÐвéѯ Bytes.toBytes("key1"), CompareOp.EQUAL, Bytes.toBytes(key));
Scan s = new Scan(); Scan s = new Scan();
s.setFilter(filter); s.setFilter(filter);
ResultScanner resultScanner = this.tableUnderTest.getScanner(s); ResultScanner resultScanner = this.tableUnderTest.getScanner(s);
for (Result r : resultScanner) { for (Result r : resultScanner) {
System.out.println("get the row key:" + new String(r.getRow())); System.out.println("get the row key:" + new String(r.getRow()));
} }
return new HBaseReturn(true);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(HBasePlugin.class).info(ex, ex); Logger.getLogger(HBasePlugin.class).info(ex, ex);
return new HBaseReturn(false); return new HBaseReturn(false);
} }
return new HBaseReturn(true);
} }
/**
* get style, read according to rowKey
* @param rowkey
* @throws IOException
* @return
*/
@Behavior(value = "Get", type = BehaviorType.USER_BEHAVIOR)
public HBaseReturn get(@Parameter(value = "rowkey", type = SupportTypes.Field) String rowkey) {
try {
Get g = new Get(Bytes.toBytes(rowkey));
Result r = tableUnderTest.get(g);
for(KeyValue kv : r.raw()){
System.out.println("column: " + new String(kv.getRow()));
System.out.println("value: " + new String(kv.getValue()));
}
return new HBaseReturn(true);
} catch (IOException ex) {
Logger.getLogger(HBasePlugin.class).info(ex, ex);
return new HBaseReturn(false);
}
}
/**
* Perform a range scan for a set of records in the database.
* @param startkey The record key of the first record to read.
* @param recordcount The number of records to read
* @return
*/
@Behavior(value = "Scan", type = BehaviorType.USER_BEHAVIOR)
public HBaseReturn scan(@Parameter(value = "startkey", type = SupportTypes.Field) String startkey,
@Parameter(value = "recordcount", type = SupportTypes.Field) String recordcount)
{
Scan s = new Scan(Bytes.toBytes(startkey));
Integer count = Integer.getInteger(recordcount);
s.setCaching(count);
ResultScanner scanner = null;
try {
scanner = tableUnderTest.getScanner(s);
int numResults = 0;
for (Result rr = scanner.next(); rr != null; rr = scanner.next())
{
String key = Bytes.toString(rr.getRow());
for (KeyValue kv : rr.raw()) {
System.out.println("result: key =" + kv.getQualifier() + " value = " + kv.getValue());
}
numResults++;
if (numResults >= count)
{
break;
}
}
}catch (IOException e) {
Logger.getLogger(HBasePlugin.class).info(e, e);
return new HBaseReturn(false);
}finally {
scanner.close();
}
return new HBaseReturn(true);
}
} }

View File

@ -95,7 +95,6 @@ public class MongoDBPlugin {
mongo = new Mongo(hostsList , mongo = new Mongo(hostsList ,
new MongoOptions(new MongoClientOptions.Builder() new MongoOptions(new MongoClientOptions.Builder()
.maxWaitTime(1000 * 40).build())); .maxWaitTime(1000 * 40).build()));
//mongo = new Mongo(hostName, port);
DB db = mongo.getDB(dbName); DB db = mongo.getDB(dbName);
DBCollection table = db.getCollection(this.tableUnderTest); DBCollection table = db.getCollection(this.tableUnderTest);
Table propertiesTable = Table.buildTable(properties, Table propertiesTable = Table.buildTable(properties,