Oceanbase usage Examples

Source: Internet
Author: User
Tags bulk insert

Http://www.mysqlops.com/2011/08/31/oceanbase-use.html

The use of oceanbase is similar to a relational database, and you need to create schemas in advance, see schema descriptions for schema.

Suppose we have the following schema:

[App_name]
Name=student
max_table_id=1002

[Student]
table_id=1001
max_column_id=22
table_type=1
#rowkey =class_id (8bytes) + Student ID (8bytes)
Rowkey_ Is_fixed_length=1
column_info=1,2,gmt_created,create_time
column_info=1,3,gmt_modified,modify_time
column_info=1,16,student_name,varchar,128
column_info=1,17,student_sex,int
column_info=1,18, Student_age,int
column_info=1,19,student_addr,varchar,128
column_info=1,20,cplusplus,int
column _info=1,21,oceanbase,int
column_info=1,22,english,int

Join=rowkey[7,15]%score:cplusplus$cplusplus, Oceanbase$oceanbase,english$english

rowkey_max_length=16
rowkey_split=8

[score]
table_id= 1002
table_type=1
max_column_id=18
#rowkey =student ID (8bytes)
rowkey_is_fixed_length=1
column_info=1,2,gm_create,create_time
column_info=1,3,gm_modified,modify_time
column_info=1,16 , Cplusplus,int
column_info=1,17,oceanbase,int
column_info=1,18,english,int
rowkey_max_length=8
rowkey_split=0

Here a total of two tables, student and Score,student table redundant score table Some of the fields, there is a join relationship.

Oceanbase currently supports only Java client, and the source code can be downloaded in SVN. The client needs to know the address and port of the cluster rootserver.

0. About Rowkey

In Oceanbase, data is stored in rows, each row is uniquely identified by Rowkey, Rowkey is binary stream, and Oceanbase does not interpret it. Rowkey can be composed of a number of segments, applied to explain it, such as the rowkey of the two tables:

The student Rowkey consists of two parts, the first part is the class ID, and the third part is the student ID.
//The Rowkey is a fixed length of 16 bytes
///The class ID can be given only at the time of the query, indicating that all students under the class are found.
class Studentrowkey extends Rowkey {public
	studentrowkey (long Classid,long student_id) {
	    Bytebuffer.putlong (ClassID);
	    Bytebuffer.putlong (student_id);
	}

    Final Bytebuffer Bytebuffer = Bytebuffer.allocate ();

    Public byte[] GetBytes () {return
    	bytebuffer.array ();
    }
};

Score's rowkey is student ID, fixed length 8 byte
class Scorerowkey extends Rowkey {public
	scorerowkey (byte item_type,long item_id) {
	    bytebuffer.put (item_type);
	    Bytebuffer.putlong (item_id);
	}

    Final Bytebuffer Bytebuffer = bytebuffer.allocate (8);

    Public byte[] GetBytes () {return
    	bytebuffer.array ();
    }
}

1. Initialization:

	Private Clientimpl client;
	Client = new Clientimpl ();
	Client.setip (IP);
	the IP of the Rootserver client.setport (port);//the Port
	of the Rootserver Client.settimeout (2000);//Timeout time, Unit millisecond
	client.init ();

2. Write

In the current release, the update and insert semantics for OB are the same. You need to provide the table name, Rowkey, and values for each column when you update.

	Final private static String student_table_name= "STUDENT";
	Final private static String student_name= "Student_name";
	Final private static String student_age= "Student_age";
	Final private static String student_sex= "Student_sex";
	Final private static String student_addr= "STUDENT_ADDR";
	Final private static String score_cplusplus= "Cplusplus";
	Final private static String score_oceanbase= "Oceanbase";

	Final private static String score_english= "中文版";
	 /** * For OB semantics, update & Insert are identical * DB semantics is not support yet.
                */void Updatedemo () {list<obmutatorbase> mutatorlist = new arraylist<obmutatorbase> (); for (Long i = 0L; I < i++) {Updatemutator mutator = new Updatemutator (Student_table_name, n
                      EW Studentrowkey (0, i));
                      Mutator.addcolumn (User_nick, New Value () {{setstring ("Your_value");}},false); Mutator.addcolumn (Student_sex, New Value () {{SETNUMber (1L);}},false);
                      Mutator.addcolumn (Student_age, New Value () {{setnumber (10L);}},false);
                      Mutator.addcolumn (STUDENT_ADDR, New Value () {{setstring ("ADDR");}},false);
		      Mutatorlist.add (mutator);
                Fractions can only be updated in the score table} result<boolean> ret = Client.update (mutatorlist);
                if (ret = null | |!ret.getresult ()) {System.err.println ("update failed"); } void Insertdemo () {for (Long i = 0L; I < i++) {Insertmutator mutator =
                New Insertmutator (Student_table_name, New Studentrowkey (I, (byte) 0, I));
                Mutator.addcolumn (User_nick, New Value () {{setstring ("Your_value");}},false);
                Mutator.addcolumn (Student_sex, New Value () {{setnumber (1L);}},false);
                Mutator.addcolumn (Student_age, New Value () {{setnumber (10L);}},false); Mutator.addcolumn (student_aDDR, New Value () {{setstring ("ADDR");}},false);
                Similar update,insert can also do BULK INSERT result<boolean> ret = Client.insert (mutator);
                if (ret = null | |!ret.getresult ()) {System.err.println ("update failed" + Ret.getcode ()); }
            }
	}

3. Query

The query is divided into get and Scan,get is the specified rowkey for the query, and scan is the scope query.

	void Querydemo () {
		QueryInfo query_info = new QueryInfo ();
		Query_info.setstartkey (New Studentrowkey (0L, 0L));
		Query_info.setendkey (New Studentrowkey (0l,100l));
		Query_info.addcolumn (student_name);
		Query_info.addcolumn (student_sex);
		result<list<rowdata>> result = Client.query (Student_table_name, query_info);
		System.out.println ("Get" + result.getresult (). Size () + "items");
	void Getdemo () {
		set<string> columns = new hashset<string> ();
		Columns.Add (student_name);
		Columns.Add (student_sex);
		result<rowdata> ret = Client.get (student_table_name, New Studentrowkey (
				0l,1l), columns);
		if (ret = null) {
			System.err.println ("Get Failed");
		} else {
			System.err.println (' Get ' + ret.getresult (). Get (Student_name));
		}
	

4. By & Where

	void Querydemowhere () {queryinfo query_info = new QueryInfo ();
		Query_info.setstartkey (New Studentrowkey (0l,0l));
		Query_info.setendkey (New Studentrowkey (0l,100l));
		Query_info.addcolumn (Student_name);

		Query_info.addcolumn (Student_sex);
		Obsimplefilter filter = new Obsimplefilter ();
		Filter.addcondition (New Obsimplecond (Student_sex,obsimplecond.oblogicoperator.eq,new Value () {{setNumber (0L);}}));
		Query_info.setfilter (filter);
		result<list<rowdata>> result = Client.query (Student_table_name, query_info);
	System.out.println ("Get" + result.getresult (). Size () + "items");
		} void Querydemoorderby () {queryinfo query_info = new QueryInfo ();
		Query_info.setstartkey (New Studentrowkey (0L, 0L));
		Query_info.setendkey (New Studentrowkey (0L, 100L));
		Query_info.addcolumn (Student_name);

		Query_info.addcolumn (Student_sex); Query_info.addorderby (Student_sex, true); ORDER:TRUE-ASC false-desc result<list<rowdata>> result = Client.query (student_tAble_name, Query_info);
	System.out.println ("Get" + result.getresult (). Size () + "items"); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.