You are a guest. Restricted access. Read more.

Working with databases

SCaVis includes several database engines:

HDataBase class

HDataBase allows to write and read data in a form of disk-based database using the keys. This is handy for non-sequential I/O. The class is based on the original code of D.Hamner (JavaWorld.com).

This raw-level database allows to store and retrieve objects. It associates a key of type String with each record. The keys will be limited to a maximum length, although the record data will not be limited. The record will consist of only one “blob” of binary data. The number of records is not fixed at creation time. The file can grow and shrink as records are inserted and removed. The database operations not depend on the number of records in the file. In other words, they'll be of constant order time with respect to file accesses. The index should be small enough to load into memory.

Here is a small example. We use two keys “0” and “1” (both are strings) and associate a string “Test” and 2D list with each key. Then we retrieve data from the file. In this approach, we use “persistent” database which keeps the data on the disk.

from jhplot.io import *
 
file="output.db"
f=HDataBase(file,"w")
f.insert("0","Test")                # store "Test" using the key "0"
f.insert("1",[ [1,2,3,3],[2,3,4]])  # store 2D array using the key "1"
f.close()
f=HDataBase(file,"r")               # open and retrieve data using the keys
print f.get("0"),f.get("1")

The output of this code is shown below:

Test [[1, 2, 3, 3], [2, 3, 4]]

We can also write complicated objects “classes” into the files as in this example:

Code example

  Download for this example is disabled for non-members
 1: from jhplot  import *
 2: from jhplot.io import *
 3: import os.path
 4:
 5: # build an event from 3 objects
 6: def makeEvent(entry):
 7:     label="Event="+str(entry)
 8:     p=P0D(label)
 9:     p.randomUniform(10,0,1)
10:     h=H1D(label,10,-1,1)
11:     h.fill(i)
12:     return [label,p,h]
13:
14: # write to a raw-level database.
15: # use string as keys
16: file="output.db"
17: f=HDataBase(file,"w")
18: Events=100
19: for i in range(Events):
20:        event=makeEvent(i)
21:        if (i%100 == 0):
22:            print event[0]+" size=",os.path.getsize(file)
23:        f.insert(str(i),event)
24: f.close()
25:
26: # open database
27: f=HDataBase(file,"r")
28: # extract event 26
29: event=f.get("26")
30: print "=== "+event[0]+"===="
31: print event[1].toString()
32: print event[2].toString()
33: f.close()

Here we sequentially write a complicated data structure “event” using several objects, and later retrieve it. This class allows only “string” type as the keys. We will show later how to use arbitrary types foe the keys.

File-based map

Objects can be stored in HashMap which is kept in the memory. One can work with a huge amount of data using FileHashMap which keeps the keys in memory, but stores the values as serialized objects in a random access disk file. This approach is very similar to the previous case, but the implementation is different.

In this example we create a file ”/tmp/mytest” (in the temporary directory tmp) and put some data (1D,2D arrays and histogram):

from jhplot import *
from org.clapper.util.misc import *
 
# make some massive java objects
p1=P0D(); p1.randomNormal(10000,0,2)
p2=P0D(); p2.randomNormal(10000,1,2)
pp=P1D("test",p1,p2);  h1=H1D("OK",100,0,10)
ppp=PND(); ppp.add([1,2,3,4,5]); ppp.add([1,2,3,4,3])
 
######### store all objects in the file
print "Write  to the database"
map=FileHashMap("/tmp/mytest",FileHashMap.FORCE_OVERWRITE);
map.put("d1",p1)
map.put("d2",p2)
map.put("d3",ppp)
map.put("4",h1)
print map.size()
map.save()
map.close()
print "Closed   database"

Note that the database will have 2 files: mytest.ix (index file) and mytest.db (actual data) Now we will read this database and extract stored objects. Then we print them:

from jhplot import *
from org.clapper.util.misc import *
print "Read   to the database"
map=FileHashMap("/tmp/mytest",FileHashMap.RECLAIM_FILE_GAPS);
p1=map.get("d1")
p2=map.get("d2")
ppp=map.get("d3")
hh=map.get("4")
print hh
print ppp
print map.size()
map.close()
print "Closed   database"

Object databases

A massive data can be stored a a map, where each key corresponds to some object (can be arbitrary Java objects). You can store collections backed up by disk storage and you can store and handle billions of data objects. Unlike the previous approach, we will consider the true database where you can rollback changes, encrypt the data, etc. As before, only serialisable object can be stored. In this section we will show example of using this database. As before, no SQL is required and the key can have arbitrary types:

Below is an example in which we write many values uing their keys, close the database, and read the values back from the database using their keys.

This code example can be viewed by SCaVis members.

Note that the database creates several files in the /tmp/ directory (db,db.p).

Here is an example were we write 100 histograms into the database using integer keys, and then we read all histograms back.

This code example can be viewed by SCaVis members.

Neodatis Object databases

NeoDatis] is another simple Java-based Object Database. Below we show a rather complete example on how to use the [[http://www.neodatis.org/ |NeoDatis database to store objects of the jhplot package:

Code example

  Download for this example is disabled for non-members
 1: from java.awt import Color,Font
 2: from jhplot  import *
 3: from java.util import Random
 4:
 5: # import from NeoDatis DB
 6: from  org.neodatis.odb import *;
 7: from  org.neodatis.odb.impl.core.query.criteria import *;
 8: from  org.neodatis.odb.core.query.criteria import *;
 9: from  org.neodatis.odb.xml import *;
10:
11: c1 = HPlot("Canvas",600,400)
12: c1.setGTitle("Writing and reading objects from a database");
13: c1.visible(1)
14: c1.setAutoRange()
15:
16: p0=P0D("Normal distribution")
17: p0.randomNormal(1000, 2.0, 1.0)
18:
19: p1=P1D("X-Y points")
20: p1.add(10,20)
21: p1.add(30,40)
22:
23: h1 = H1D("GaussianHistogram",100, -2, 2.0)
24: rand = Random()
25: for i in range(500):
26:       h1.fill(rand.nextGaussian())
27:
28: lab=HLabel("Label", 0.15, 0.7, "NDC")
29: lab.setColor(Color.blue)
30:
31: if os.path.exists("database.db"):   # remove database  if exists
32:     os.remove("database.db")
33:
34: print "Store all objects in a database"
35: odb = ODBFactory.open("database.db")
36: odb.store(p0)
37: odb.store(p1)
38: odb.store(h1)
39: odb.store(lab)
40:
41: # store several arrays
42: for i in range(20):
43:     p0=P0D("data"+str(i))
44:     p0.randomNormal(10*i, 2.0, 1.0)
45:     odb.store(p0)
46:
47: odb.close()
48:
49: print "######### open the database ################"
50:
51: odb = ODBFactory.open("database.db")
52: query = CriteriaQuery(P0D);
53: obj = odb.getObjects(query)
54: print  "Number of P0D objects=",obj.size()
55: query = CriteriaQuery(H1D);
56: obj = odb.getObjects(query)
57: print  "Number of H1D  objects=",obj.size()
58:
59: # find an P0D object with the name data1
60: query =  CriteriaQuery(P0D, Where.equal("title", "data1"));
61: p0d=odb.getObjects(query).getFirst()
62: print p0d.toString()
63:
64: # find an H1D object with the name GaussianHistogram
65: query =  CriteriaQuery(H1D, Where.equal("title", "GaussianHistogram"));
66: h1d=odb.getObjects(query).getFirst()
67: h1d.setTitle("GaussianHistogram from a database")
68: c1.draw( h1d)
69:
70: odb.close()

SQL databases

SCaVis supports SQL-type of databases:

Below we will show Jython scripts how to work with such databases.

SQLite database

This code example can be viewed by SCaVis members.

HyperSQL database

This code example can be viewed by SCaVis members.

Derby database

Starting from v3.8, Derby is excluded from the package since it comes with JDK as JavaDB (see the JAVAHOME/db/ directory). Here is example of how to write and read data using Derby:

This code example can be viewed by SCaVis members.

Third-party IO classes

click here if you want to know more

click here if you want to know more

click here if you want to know more

here are a lot of other Java-based I/O classes designed for storing and retrieving data. A complete description of how to use Java, Jython and SCaVis for scientific analysis is described in the book Scientific data analysis using Jython and Java published by Springer Verlag, London, 2010 (by S.V.Chekanov)

Sergei Chekanov 2010/03/07 17:35

                                                                            440,2
Navigation

Print/export