Query in OrientDB - orientdb

I try to print a query through the java console but nothing comes out. this is my code someone could help me.
I'm new to OrientDB and I'm just learning.
The query I need is to know the shortest path between two nodes and print this query on the Java console. It does not give me any errors but nothing comes out.
public class Graph {
private static final String DB_PATH = "C:/OrientDataBase/shortest_path";
static OrientGraphNoTx DBGraph;
static OrientGraphFactory factory;
public static void main(String[] args) {
factory = new OrientGraphFactory("plocal:"+DB_PATH);
DBGraph = factory.getNoTx();
HashMap<String, Vertex> nodes = new HashMap<String, Vertex>();
for(int i = 0; i <= 1000; i++)
{
Vertex v = DBGraph.addVertex("class:V");
v.setProperty("vertexID", i+"");
nodes.put(i+"", v);
}
try(BufferedReader br = new BufferedReader(new FileReader("C:/OrientDataBase/sp1.csv"))) {
int i=0;
for(String line; (line = br.readLine()) !=null ; ) {
if(i==0){
i++;
}
else{
String[] vertices = line.split(",");
String vertex1 = vertices[0];
String vertex2 = vertices[1];
String weight= vertices[2];
vertex2 = vertex2.replaceAll(" ", "");
Vertex v1 = nodes.get(vertex1);
Vertex v2 = nodes.get(vertex2);
Edge eLives = DBGraph.addEdge(null, v1, v2, "belongs");
eLives.setProperty("weight", weight);
System.out.println(v1+","+v2+","+weight);
String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,BOTH))";
Iterable<OrientVertex> res = DBGraph.command(new OCommandSQL(query)).execute();
while(res.iterator().hasNext()){
OrientVertex v = res.iterator().next();
System.out.println("rid: "+v.getId().toString()+"\tn:"+v.getProperty("n"));
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}

I tried your code and you have to put the ticks when you do the query so, it becomes:
String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,'BOTH'))";
I used this csv file.
Hope it helps.
Regards

Related

How to use sequence in multi-threaded enviroment

I try to create multiple vertexes in parallel:
public static void main(String[] args) throws InterruptedException {
//create db and seq
ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:/TestDB");
db.create();
OSequenceLibrary seqLib = db.getMetadata().getSequenceLibrary();
seqLib.createSequence("testSeq",
OSequence.SEQUENCE_TYPE.ORDERED,
new OSequence.CreateParams().setStart(0L).setIncrement(1)
);
OrientGraphFactory factory = new OrientGraphFactory("memory:/TestDB", "admin", "admin").setupPool(1, 8);
//mt
Executor executor = Executors.newFixedThreadPool(8);
CountDownLatch latch = new CountDownLatch(1000);
for (int i = 1; i <= 1000; i++) {
executor.execute(() -> {
OrientGraph g = factory.getTx();
try {
OSequence seq = g.getRawGraph().getMetadata().getSequenceLibrary().getSequence("testSeq");
OrientVertex v = g.addVertex("TestClass");
v.setProperty("seq", seq.next());
latch.countDown();
} finally {
g.shutdown();
}
});
}
latch.await(5, TimeUnit.SECONDS);
System.exit(0);
}
And receive lots of exceptions:
com.orientechnologies.orient.core.exception.OConcurrentModificationException:
Cannot UPDATE the record #7:0 because the version is not the latest.
Probably you are updating an old record or it has been modified by
another user (db=v2 your=v1)
How to use sequence in mt environment properly?
OrientDB is entirely based on an optimistic approach with no or few locks. For this reason you should catch the exception and retry. Example:
OrientGraph g = factory.getTx();
try {
for( int retry = 0; retry < 100; ++retry ){
try {
OSequence seq = g.getRawGraph().getMetadata().getSequenceLibrary().getSequence("testSeq");
OrientVertex v = g.addVertex("TestClass");
v.setProperty("seq", seq.next());
latch.countDown();
break;
} catch( ONeedRetryException e ) {
}
}
} finally {
g.shutdown();
}

OrientDB : Can't find symbol OGraphDatabase

I searched some examples on the Internet and many of them use the OGraphDatabase. However, when these code file was compiled, a exception that says cant find the symbol of "OGraphDatabase" was throwed.
Blow is the source code:
public class TestTreeGraph {
static OGraphDatabase db;
//static int i=0;
//static ODocument currentNode;
public static void main(String[] args) throws FileNotFoundException{
String dbpath="/Users/wuguirongsg/orientdb/orientdbgraph";
File dbfile = new File(dbpath);
if(!dbfile.exists()){
//dbfile.mkdirs();
db = new OGraphDatabase("local:"+dbpath).create();
db = new OGraphDatabase("local:"+dbpath).open("admin", "admin");
}else{
db = new OGraphDatabase("local:"+dbpath).open("admin", "admin");
}
ODocument rootNode = db.createVertex().field("id", 0);
int i=1;
createNode(rootNode,i);
db.setRoot("treegraph", rootNode);
}
private static void createNode(ODocument node,int i){
if(i>=20){
System.out.println("i>=10================== back ");
return ;
}
ODocument leftNode = db.createVertex().field("id", i + "_vertex_left");
System.out.println("create "+i + "_vertex_left ");
ODocument rightNode = db.createVertex().field("id", i + "_vertex_right");
System.out.println("create "+i + "_vertex_right ");
ODocument edgeleft = db.createEdge( node, leftNode);
ODocument edgeright = db.createEdge( node, rightNode);
edgeleft.save();
edgeright.save();
//currentNode = leftNode;
System.out.println("go left");
createNode(leftNode,i+1);
System.out.println("go right");
createNode(rightNode,i+1);
System.out.println("==================");
}
}
OGraphDatabase has been deprecated ages ago. You can use OrientGraph.
Example
String dbpath="C:/test";
OrientGraphFactory factory = new OrientGraphFactory("plocal:"+dbpath);
// if the database doesn't exist it is created and opened
// if the database exists, it is opened
OrientGraph db = factory.getTx();
// inserting a vertex
Vertex rootNode=db.addVertex("class:V");
rootNode.setProperty("myId","0");
db.shutdown();

How select random documents in OrientDB

I write online game. For game logic, I need select random users(etc) from database. How to achive this with java api? What the most perfomance way to do this?
I can use something like(pseudocode): select from User skip(randomNum(0,usersCount)) limit 1 but how write in documentation - skip has bad performance.
I have tried with this code
int numberRandom= 5;
String string="[";
int cluster= db.getMetadata().getSchema().getClass("User").getClusterIds()[0];
for(int i=0;i<numberRandom;i++){
int random=ThreadLocalRandom.current().nextInt(0, 96000);
if(i==(numberRandom-1))
string += cluster+":"+random + "]";
else
string += cluster+":"+random + ",";
}
Iterable<Vertex> result = g.command(new OCommandSQL("select from "+ string)).execute();
for(Vertex v:result)
System.out.println(v.getId());
Let me know if it can be a good solution for you
I wrote two java classes, both are getting X random users from a specific cluster.
The first one seams faster to me. (about 0.8s vs 1.2s)
testRandom.java
public class testRandom {
public static void main(String[] args) {
// TODO Auto-generated method stub
String nomeDb = "RandomUser";
try {
OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
if(serverAdmin.existsDatabase()){ // il db esiste
//connessione a db
OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);
//------------------------------------------------
long Tbegin,Tend;
float millis;
Tbegin = System.currentTimeMillis();
int numberRandom= 5;
int random;
String cluster = "user";
Iterable<Vertex> vertices = g.command(new OCommandSQL("select from cluster:"+cluster)).execute();
List<Vertex> v_array = new ArrayList<Vertex>();
List<Vertex> res = new ArrayList<Vertex>();
for(Vertex v : vertices){
v_array.add(v);
}
int arraysize = v_array.size();
for(int i=0;i<numberRandom;i++){
random=ThreadLocalRandom.current().nextInt(0, arraysize);
res.add(v_array.get(random));
}
for(Vertex v : res){
System.out.println(v.getId());
}
Tend = System.currentTimeMillis();
millis = (Tend-Tbegin);
System.out.println("--Execution time: "+millis/1000+ "s\n");
//------------------------------------------------
//chiude db
g.shutdown();
}
else{
System.out.println("Il database '"+ nomeDb + "' non esiste");
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
testRandomSkip.java
public class testRandom_skip {
public static void main(String[] args) {
// TODO Auto-generated method stub
String nomeDb = "RandomUser";
try {
OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
if(serverAdmin.existsDatabase()){ // il db esiste
//connessione a db
OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);
//------------------------------------------------
long Tbegin,Tend;
float millis;
Tbegin = System.currentTimeMillis();
int numberRandom= 5;
int random;
String cluster = "user";
List<Vertex> res = new ArrayList<Vertex>();
Iterable<Vertex> q_count_V = g.command(new OCommandSQL("select count(*) from cluster:"+cluster)).execute();
Long count_V = 0l;
for(Vertex v : q_count_V){
count_V=v.getProperty("count");
break;
}
for(int i=0;i<numberRandom;i++){
random=(int)ThreadLocalRandom.current().nextLong(0, count_V);
Iterable<Vertex> vertex = g.command(new OCommandSQL("select from cluster:"+cluster+" skip "+random+" limit 1")).execute();
for(Vertex v : vertex){
res.add(v);
break;
}
}
for(Vertex v : res){
System.out.println(v.getId());
}
Tend = System.currentTimeMillis();
millis = (Tend-Tbegin);
System.out.println("--Execution time: "+millis/1000+ "s\n");
//------------------------------------------------
//chiude db
g.shutdown();
}
else{
System.out.println("Il database '"+ nomeDb + "' non esiste");
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope it helps.
Ivan

How to implement boolean retrieval using hitcollector in below scenario

I am running my code on TREC documents and right now implementing scoring scheme to get number of relevant documents. However now i want to implement boolean retrieval, I am trying to use HitCollector.
below is my code..
public class BatchSearch {
private BatchSearch() {}
/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
String usage =
"Usage:\tjava BatchSearch [-index dir] [-simfn similarity] [-field f] [-queries file]";
if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
System.out.println(usage);
System.out.println("Supported similarity functions:\ndefault: DefaultSimilary (tfidf)\n");
System.exit(0);
}
String index = "index";
String field = "contents";
String queries = null;
String simstring = "default";
for(int i = 0;i < args.length;i++) {
if ("-index".equals(args[i])) {
index = args[i+1];
i++;
} else if ("-field".equals(args[i])) {
field = args[i+1];
i++;
} else if ("-queries".equals(args[i])) {
queries = args[i+1];
i++;
} else if ("-simfn".equals(args[i])) {
simstring = args[i+1];
i++;
}
}
Similarity simfn = null;
if ("default".equals(simstring)) {
simfn = new DefaultSimilarity();
} else if ("bm25".equals(simstring)) {
simfn = new BM25Similarity();
} else if ("dfr".equals(simstring)) {
simfn = new DFRSimilarity(new BasicModelP(), new AfterEffectL(), new NormalizationH2());
} else if ("lm".equals(simstring)) {
simfn = new LMDirichletSimilarity();
}
if (simfn == null) {
System.out.println(usage);
System.out.println("Supported similarity functions:\ndefault: DefaultSimilary (tfidf)");
System.out.println("bm25: BM25Similarity (standard parameters)");
System.out.println("dfr: Divergence from Randomness model (PL2 variant)");
System.out.println("lm: Language model, Dirichlet smoothing");
System.exit(0);
}
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
IndexSearcher searcher = new IndexSearcher(reader);
searcher.setSimilarity(simfn);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
BufferedReader in = null;
if (queries != null) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream("queries"), "UTF-8"));
}
QueryParser parser = new QueryParser(Version.LUCENE_41, field, analyzer);
while (true) {
String line = in.readLine();
if (line == null || line.length() == -1) {
break;
}
line = line.trim();
if (line.length() == 0) {
break;
}
String[] pair = line.split(" ", 2);
Query query = parser.parse(pair[1]);
doBatchSearch(in, searcher, pair[0], query, simstring);
}
reader.close();
}
/**
* This function performs a top-1000 search for the query as a basic TREC run.
*/
public static void doBatchSearch(BufferedReader in, IndexSearcher searcher, String qid, Query query, String runtag)
throws IOException {
// Collect enough docs to show 5 pages
TopDocs results = searcher.search(query, 1000);
ScoreDoc[] hits = results.scoreDocs;
HashMap<String, String> seen = new HashMap<String, String>(1000);
int numTotalHits = results.totalHits;
int start = 0;
int end = Math.min(numTotalHits, 1000);
for (int i = start; i < end; i++) {
Document doc = searcher.doc(hits[i].doc);
String docno = doc.get("docno");
// There are duplicate document numbers in the FR collection, so only output a given
// docno once.
if (seen.containsKey(docno)) {
continue;
}
seen.put(docno, docno);
System.out.println(qid+" Q0 "+docno+" "+i+" "+hits[i].score+" "+runtag);
}
}
}
The scoring is done in doBatchSearch and now i want to implement HitCollector here.

How do I convert text files to .arff format(weka)

Please advise me How do I convert text files to .arff format(weka)
because i wan to do data clustering for 1000 txt file.
regards
There are some converters implemented in WEKA, just find the right format or make little changes to your data (using awk, sed...).
Here is the API pages related to this topic: http://weka.sourceforge.net/doc.stable/weka/core/converters/package-summary.html
For exapmle here is how to convert from CSV to ARFF:
java weka.core.converters.CSVLoader filename.csv > filename.arff
Here is the code you can use
package text.Classification;
import java.io.*;
import weka.core.*;
public class TextDirectoryToArff {
public Instances createDataset(String directoryPath) throws Exception {
FastVector atts;
FastVector attVals;
atts = new FastVector();
atts.addElement(new Attribute("contents", (FastVector) null));
String[] s = { "class1", "class2", "class3" };
attVals = new FastVector();
for (String p : s)
attVals.addElement(p);
atts.addElement(new Attribute("class", attVals));
Instances data = new Instances("MyRelation", atts, 0);
System.out.println(data);
InputStreamReader is = null;
File dir = new File(directoryPath);
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".txt")) {
double[] newInst = new double[2];
File txt = new File(directoryPath + File.separator + files[i]);
is = new InputStreamReader(new FileInputStream(txt));
StringBuffer txtStr = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
txtStr.append((char) c);
}
newInst[0] = data.attribute(0).addStringValue(txtStr.toString());
int j=i%(s.length-1);
newInst[1] = attVals.indexOf(s[j]);
data.add(new Instance(1.0, newInst));
}
}
return data;
}
public static void main(String[] args) {
TextDirectoryToArff tdta = new TextDirectoryToArff();
try {
Instances dataset = tdta.createDataset("/home/asadul/Desktop/Downloads/text_example/class5");
PrintWriter fileWriter = new PrintWriter("/home/asadul/Desktop/Downloads/text_example/abc.arff", "UTF-8");
fileWriter.println(dataset);
fileWriter.close();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}