search Hbase record using java API? - java

I want to search record base on value and my application in Java
I am store record using below code
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value));
table.put(put);
I pass values
Put put = new Put(Bytes.toBytes("rowkey"));
put.add(Bytes.toBytes("family1"), Bytes.toBytes("qualifier1"),
Bytes.toBytes("this is test record"));
this is my get implementation
Get get = new Get(Bytes.toBytes("rowkey"));
String[] family = { "row1" };
get.addColumn(Bytes.toBytes("family1"),Bytes.toBytes("qualifier1"));
Result rs = table.get(get);
now I want to search this record by "this is test record" value.
help me to found this record.

using filter you can do search on any of the element and you can pass different combination
for SubstringComparator BinaryComparator
FilterList flist = new FilterList();
flist.addFilter(new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("Predicatevalue"))));
flist.addFilter(new QualifierFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("Subject value"))));
flist.addFilter(new ValueFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("Objectvalue"))));
This work for me.

Look at the get() method on HTable and the Get class, which are analogous to the put() method on HTable and the Put class.

Related

How to extract only one value from mongoDB?

LIKE I HAVE THREE FIELD 'TO','FROM' AND 'MESSAGE', I just want to display content of message field where I have given some clause in to and from.
Document{{_id=59c7d57c674cd5673c304936, to=9915103230, from=9915103229, date=24/12/2017, message=HELLO WORLD}}
I JUST WANT TO RETRIEVE "HELLO WORLD", not the whole document.
Like I just want, String message=?????---> I need some method here so the Variable of String type gets the value Hello World.
Projection method is not working for me.
I am using JDBC MongoDB 3.5 Driver.
Use projection, the second optional argument to find(). For context, this gives you the whole document:
db.yourCollection.find({to:9915103230,from:9915103229});
This gives you only message from the results. Just name the field and set it to 1:
db.yourCollection.find({to:9915103230,from:9915103229},{message:1};
You can specify more than one thing to return:
db.yourCollection.find({to:9915103230,from:9915103229},{message:1, to:1};
Here's a functioning prog. Compile against the 3.5 drivers.
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase( "testX" );
MongoCollection<BsonDocument> coll = db.getCollection("foo", BsonDocument.class);
coll.drop();
{
BsonDocument doc = new BsonDocument();
doc.put("from", new BsonInt32(23343223));
doc.put("to", new BsonInt32(23343223));
doc.put("msg", new BsonString("hello"));
coll.insertOne(doc);
doc.remove("_id");
doc.put("from", new BsonInt32(8889));
doc.put("to", new BsonInt32(99999));
doc.put("msg", new BsonString("goodbye"));
coll.insertOne(doc);
}
{
BsonDocument query = new BsonDocument("from", new BsonInt32(8889));
BsonDocument proj = new BsonDocument("msg", new BsonInt32(1));
proj.put("_id",new BsonInt32(0));
BsonDocument d2 = coll.find(query).projection(proj).first();
System.out.println(d2);
String s2 = coll.find(query).projection(proj).first().getString("msg").getValue();
System.out.println(s2);
}

Add index to relation in ELKI

I am trying to add index to relation in db, but don't know is it right?
ListParameterization spatparams = new ListParameterization();
spatparams.addParameter(INDEX_ID, RStarTreeFactory.class);
spatparams.addParameter(AbstractPageFileFactory.Parameterizer.PAGE_SIZE_ID, 300);
spatparams.addParameter(AbstractRStarTreeFactory.Parameterizer.INSERTION_STRATEGY_ID, ApproximativeLeastOverlapInsertionStrategy.class);
spatparams.addParameter(ApproximativeLeastOverlapInsertionStrategy.Parameterizer.INSERTION_CANDIDATES_ID, 1);
// Adapter to load data from an existing array.
DatabaseConnection dbc = new ArrayAdapterDatabaseConnection(data);
// Create a database (which may contain multiple relations!)
Collection<IndexFactory<?, ?>> indexFactories = new ArrayList<>();
ObjectListParameter<IndexFactory<?, ?>> indexFactoryP = new ObjectListParameter<>(INDEX_ID, IndexFactory.class, true);
indexFactories.addAll(indexFactoryP.instantiateClasses(spatparams));
Database db = new StaticArrayDatabase(dbc, indexFactories);
db.initialize();
To instantiate a class via the parameterization API, you don't need to create a new parameter.
RStarTreeFactory<DoubleVector> f =
ClassGenericsUtil.parameterizeOrAbort(RStarTreeFactory.class, params);
For the R-star tree, I suggest to use SortTileRecursive bulk loading.

How to fetch rows ending with specific value from hbase using hbase shell

How to fetch rows that ends with specific value in Hbase?
For example I have below rowskeys in my table
D1|V1
D2|V1
D3|V2
D4|V1
Now I need all rows that ends with V1. In this example I should get D1,D2 and D4.
Can some one suggest how to achieve this in Hbase
I achieved it using below:
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
scan 'tableName', {FILTER => org.apache.hadoop.hbase.filter.RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),SubstringComparator.new("SubString"))}
and through Java
FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);
List<String> referenceList = new ArrayList<String>();
ResultScanner results = null;
String substr= "V1";
RowFilter rowfilter = new RowFilter(CompareOp.EQUAL, new SubstringComparator(substr));
filterList.addFilter(rowfilter);
Scan prefilterScan = new Scan();
prefilterScan.setFilter(filterList);

Parse a csv String and map to a java object

I am trying to parse a csv string like this
COL1,COL2,COL3
1,2,3
2,4,5
and map columns to a java object-
Class Person{
COL1,
COL2,
COL3;
}
Most of the libraries I found on google are for csv files but I am working with google app engine so can't write or read files. currently I am using split method but problems with this approach is
column that I am getting in csv string could vary as
COL1,COL3,COL2
don't want to use boiler plate code of splitting and getting each column.so what I need is list of column header and read all columns in a collection using header mapper. While iterating, map column value to a java object.
There are several question based on similar type of requirement but none of them helped me.
If anyone has done this before please could you share the idea? Thanks!
After searching and trying several libraries, I am able to solve it. I am sharing the code if anyone needs it later-
public class CSVParsing {
public void parseCSV() throws IOException {
List<Person> list = Lists.newArrayList();
String str = "COL1,COL2,COL3\n" +
"A,B,23\n" +
"S,H,20\n";
CsvSchema schema = CsvSchema.emptySchema().withHeader();
ObjectReader mapper = new CsvMapper().reader(Person.class).with(schema);
MappingIterator<Person> it = mapper.readValues(str);
while (it.hasNext()) {
list.add(it.next());
}
System.out.println("stored list is:" + (list != null ? list.toString() : null));
}}
Most of the libraries I found on google are for csv files but I am
working with google app engine so can't write or read files
You can read file (in project file system).
You can read and write file in blobstore, google cloud storage
Use a Tokenizer to split the string into objects then set them to the object.
//Split the string into tokens using a comma as the token seperator
StringTokenizer st = new StringTokenizer(lineFromFile, ",");
while (st.hasMoreTokens())
{
//Collect each item
st.nextElement();
}
//Set to object
Person p = new Person(item1, item2, item3);
If the columns can be reversed, you parse the header line, save it's values and and use it to decide which column each token falls under using, say, a Map
String columns[] = new String[3]; //Fill these with column names
Map<String,String> map = new HashMap<>();
int i=0;
while (st.hasMoreTokens())
{
//Collect each item
map.put(columns[i++], st.nextElement());
}
Then just, create the Person
Person p = new Person(map.get("COL1"), map.get("COL2"), map.get("COL3"));

"read" Data with multiple IDs from OpenERP using java with apache xml-rpc

Hi I am currently writing a servlet using Apache XML-RPC connecting to OpenERP. There are not any good resources around, and the java examples are very minimalistic and far from complete on the OpenERP site.
Has anyone a clue where I could find an API on how and what I can call on the OpenERP side?
I would really appreciate that!!!
On a further note, I am specifically looking for the syntax on how to "read" data, using java, with an input of multiple ids.
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
clientConfig.setEnabledForExtensions(true);
clientConfig.setServerURL(new URL(urlStringObject));
client.setConfig(clientConfig);
Object[] params2 = { "city", "name", "email", "create_date","write_date" };
Vector<Object> arg = new Vector<Object>();
arg.add(database);
arg.add(1);
arg.add(password);
arg.add("res.partner.address");
arg.add("read");
arg.add(9); // <- THE PYTHON SYNTAX SAYS input 'LIST OF IDS' here What is the Jave equivalent???
arg.add(params2);
HashMap ids = (HashMap) client.execute("execute", arg);
UPDATE
/* Search for all ids */
xmlrpcConfigLogin.setServerURL(new URL(urlStringObject));
Object[] searchQuery = new Object[] {"id", "!=", -1 };
Vector queryVector = new Vector();
queryVector.addElement(searchQuery);
Object[] params = new Object[] { database, theloginId , password, tableName, "search", queryVector };
Object[] po_ids = (Object[]) xmlrpcLogin.execute("execute", params);
/* Send Read Query */
Object[] readQuery = {"name"};
Vector<Object> arg = new Vector<Object>();
arg.add(database);
arg.add(1);
arg.add(password);
arg.add(tableName);
arg.add("read");
arg.add(po_ids);
arg.add(readQuery);
HashMap globalMap = new HashMap();
Object[] hm = (Object[]) xmlrpcLogin.execute("execute", arg);
for (Object object : hm) {
HashMap hash = (HashMap)object;
globalMap.put("name", hash.get("name"));
_log.info(hash.get("name"));
}
As you can see: It takes an Object[] of ids as input (po_ids)
I assume you've read through the developer book description of XML-RPC. I think that it's just a wrapper around all the methods in the ORM class. That's all the documentation I've seen. Other than that, I run OpenERP in debug mode and put a break point in the LocalService.__call__() method to see what parameters the client is sending to the server. (It's in server/bin/netsvc.py.) I've also seen developers just log every request that comes through that method.
As for how to make the calls in Java, I'm not familiar with the XmlRpcClient API, but it looks like it will accept an array of objects for a list, and probably anything that is enumerable. See if the description of data types is helpful, and check out the word tracker tutorial. It uses a Vector to hold a list parameter.

Categories

Resources