SequoiaDBJava development tutorial, sequoiadbjava

Source: Internet
Author: User

SequoiaDBJava development tutorial, sequoiadbjava

1. Background

In recent years, with the advancement of society and the development of information and communication technology, information systems and Internet applications have rapidly expanded in various industries and fields. These systems collect, process, and accumulate more and more data, and the data volume is growing faster and faster. The data volume increases at an explosive rate every year. Before the emergence of the Internet, data was mainly produced by means of Human-Machine sessions, mainly structured data. Therefore, we all need traditional RDBMS to manage the data and application systems. At that time, the data growth was slow and the systems were isolated. Using traditional databases could basically meet the needs of various application development. However, with the advent of the big data era, data is gradually transformed into unstructured and semi-structured data. The use of data is not limited to the needs of business applications. Data Mining and Analysis of massive data generated in the information age has become a common means for many enterprises to obtain valuable information. Traditional databases have almost no technical or functional requirements and applications. Traditional databases (OldSQL) have become OldSQL + NewSQL + NoSQL + other new technologies (stream, real-time, memory, etc.) to support multiple types of applications.

2. Product Introduction

SequoiaDB is a leading distributed database vendor in China, focusing on the R & D of the next-generation big data infrastructure. Its SequoiaDB jushan database is a NewSQL database that supports SQL, high concurrency, real-time, distributed, scalable, and flexible storage. In addition, SequoiaDB supports integration with various big data tools, such as spark, hive, hadoop, kafka, and postgreSQL. In terms of application development, SequoiaDB not only provides native APIs, but also provides c drivers, c ++ drivers, CSharp drivers, java drivers, PHP drivers, and REST interfaces. Developers can easily and quickly use SequoiaDB for application development. This article describes how to use Java API for application development.

3. advantages of using SequoiaDB for application development

3.1 flexible storage

SequoiaDB is a document-type database that stores data in JSON format, which can effectively improve development efficiency. In general, many tables are involved in the development of large applications, and there are many associations between tables, especially in banking, internet finance and other industries, because of the complexity of the business, the relationship between tables is extremely complex. When using traditional databases for development, you must first associate the tables, and the design of complex tables requires a lot of manpower, this not only wastes money, but also slows down the development progress. In addition, in the later maintenance and upgrade process, after multiple iterations, the fields in the table are usually changed. If you need to modify or delete the values of some fields, it is difficult to determine whether the modified results will affect other business systems. This requires developers to understand the business and raise the development threshold. If you use a document-based database, the JSON structure is used to store data. You can store the fields designed by the Business System in a table in the way of objects. When table Association is involved, you can use nested methods in BSON to store data in it. This frees developers from spending a lot of effort on designing tables and reduces development costs.

In addition, SequoiaDB is a dual-engine database that supports data storage in JSON format, distributed object storage, and unstructured data management.

3.2. Low-cost deployment

SequoiaDB can be installed on X86 machines. Compared with databases installed on high-end storage, this is undoubtedly a cost saving. SequoiaDB uses a three-copy storage method, which effectively ensures data security. In addition, due to distributed storage, data is stored on multiple servers. When querying data, query tasks are distributed to multiple machines at the same time, and finally summarized to the same machine, which makes full use of the resources of multiple servers, in this way, low-cost deployment is achieved and the effect is fast.

3.3 support for composite indexes

In traditional databases, many products only support a single index, while SequoiaDB also supports composite indexes. In a complex business system, the data volume is usually large. When querying data, it usually requires multiple query conditions and fast response. This makes it difficult for a single index query to meet business needs. SequoiaDB can effectively solve this problem. developers can collect statistics on some frequently needed query conditions based on their business query habits, in this way, you can create a composite index based on the conditions required for each query. When the business system is querying, SequoiaDB performs a quick query by means of index scanning.

3.4. High Security

SequoiaDB uses multi-copy storage. Developers plan to scatter data in different data groups, and nodes in the same data group are scattered on different machines, because the data in the same data group is the same, this allows the database to quickly switch the master node to other nodes in case of a server failure or other faults, to provide services for the business system, it can also effectively prevent data loss.

4. Establish the environment

4.1 software configuration

Operating System: windows 8

JDK: 1.7.0 _ 80 64-bit,: https://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u80-oth-JPR

Myeclipse: 12.0.0

SequoiaDB: 2.8.1. For installation and deployment, refer:

Java DRIVER: 2.6.3, which is:

Https://download.sequoiadb.com/cn/ index-cat_id-2

4.2 configure the Eclipse Environment

(1) copy the SequoiaDB. jar file in the sequoiadb driver development kit to the project file directory (it is recommended to place it in all other dependent library directories, such as the lib directory );

(2) In the "Package Development E" window on the left of the main Eclipse window, select a development project and right-click the project;

(3) Select the "properties" menu item from the menu;

(4) In the pop-up "property for project ..." In the window, select "Java Build Path"-> "Libraries", as shown in:

(5) Click "Add External JARs .." and Add sequoiadb. jar to the project;

(6) Click "OK" to complete environment configuration.

5. SequoiaDB Java Development Instance

5.1 Example 1: List collection objects

Connect to the database and list all collections in the database.

Package com. sequoiadb. demo;

Import com. sequoiadb. base. DBCursor;

Import com. sequoiadb. base. Sequoiadb;

Import com. sequoiadb. exception. BaseException;

Public class Demo01 {

Public static void main (String [] args ){

Try {

String connStr = "192.168.43.237 ";

String userName = "";

String password = "";

// Port 11810 connecting to the local database in this routine is the service port of the Coordination node, using an empty username and

// Password. You need to configure parameters according to your actual situation.

Sequoiadb sdb = new Sequoiadb (connStr, userName, password );

DBCursor cursor = sdb. listCollections ();

Try {

While (cursor. hasNext ()){

System. out. println (cursor. getNext (). toString ());

}

} Finally {

Cursor. close ();

}

} Catch (BaseException e ){

System. out. println ("Sequoiadb driver error, error description:" + e. getErrorType ());

}

}

}

5.2 Example 2: Add a search operation

Connect to the database and add search operations to the records of the test table in the database.

Package com. sequoiadb. demo;

Import org. bson. BSONObject;

Import org. bson. BasicBSONObject;

Import com. sequoiadb. base. CollectionSpace;

Import com. sequoiadb. base. DBCollection;

Import com. sequoiadb. base. DBCursor;

Import com. sequoiadb. base. Sequoiadb;

Import com. sequoiadb. exception. BaseException;

Public class Demo02 {

Public static void main (String [] args ){

String connStr = "192.168.43.237: 11810 ";

String userName = "";

String password = "";

DBCursor cursor = null;

Try {

Sequoiadb sdb = new Sequoiadb (connStr, userName, password );

CollectionSpace db = sdb. createCollectionSpace ("space ");

DBCollection cl = db. createCollection ("collection ");

// DBCollection cl = sdb. getCollectionSpace ("space ")

. GetCollection ("collection ");

// Create an inserted bson object

BSONObject obj = new BasicBSONObject ();

Obj. put ("name", "xiaoming ");

Obj. put ("age", 24 );

// Add a record

Cl. insert (obj );

// Query records

Cursor = cl. query ();

Try {

While (cursor. hasNext ()){

BSONObject record = cursor. getNext ();

String name = (String) record. get ("name ");

System. out. println ("name =" + name );

}

} Finally {

Cursor. close ();

}

} Catch (BaseException e ){

System. out. println ("Sequoiadb driver error, error description:" + e. getErrorType ());

}

}

}

5.3 Example 3: Use of the Data Connection Pool

The basic principle of the database connection pool is to maintain a certain number of database connections in the internal Object pool and expose the methods for obtaining and returning database connections. For example, an external user can use the getConnection method to obtain the connection. After the connection is used, the releaseConnection method is used to return the connection. Note that the connection is not closed but is recycled by the connection pool manager, prepare for the next use.

Advantages of database connection pool technology:

1. Resource Reuse: database connections are reused to avoid high performance overhead caused by frequent connection creation and release. On the basis of reducing system consumption, on the other hand, it also improves the stability of the system Runtime Environment (reducing memory fragments and the number of temporary database processes/Threads ).

2. Faster system response speed: During database connection pool initialization, several database connections are often created and used for backup in the pool. The connection Initialization is complete. For service request processing, the existing available connections are used directly to avoid the time of database connection initialization and release, thus reducing the overall response time of the system.

3. Unified connection management to avoid database connection leaks: In the more complete implementation of the database connection pool, you can forcibly reclaim the occupied connection according to the preset timeout settings. This avoids the possibility of resource leakage during conventional database connection operations.

Package com. sequoiadb. demo;

Import java. util. ArrayList;

Import org. bson. BSONObject;

Import org. bson. BasicBSONObject;

Import com. sequoiadb. base. CollectionSpace;

Import com. sequoiadb. base. DBCollection;

Import com. sequoiadb. base. DBCursor;

Import com. sequoiadb. base. Sequoiadb;

Import com. sequoiadb. base. SequoiadbDatasource;

Import com. sequoiadb. datasource. ConnectStrategy;

Import com. sequoiadb. datasource. performanceoptions;

Import com. sequoiadb. exception. BaseException;

Import com.sequoiadb.net. ConfigOptions;

Public class Datasource {

Public static void main (String [] args) throws InterruptedException {

ArrayList addrs = new ArrayList ();

String user = "";

String password = "";

ConfigOptions nwOpt = new ConfigOptions ();

Performanceoptions dsOpt = new performanceoptions ();

SequoiadbDatasource ds = null;

// Provide the coord Node Address

Addrs. add ("192.168.43.237: 11810 ");

Addrs. add ("zxq3: 11810 ");

// Set network parameters

NwOpt. setConnectTimeout (500); // The Connection establishment timeout value is 500 ms.

NwOpt. setMaxAutoConnectRetryTime (0); // The retry time after the connection fails to be established is 0 ms.

// Set connection pool Parameters

DsOpt. setMaxCount (500); // a connection pool can provide up to 500 connections.

DsOpt. setDeltaIncCount (20); // Add 20 connections each time.

DsOpt. setMaxIdleCount (20); // when the connection pool is idle, 20 connections are retained.

DsOpt. setKeepAliveTimeout (0); // The idle connection survival time in the pool. Unit: milliseconds.

// 0 indicates that the message is not sent or received after the connection interval.

DsOpt. setCheckInterval (60*1000); // more

// The idle connection specified by MaxIdleCount is disabled,

// And the survival time is too long (the connection has stopped sending and receiving

// The connection is closed when the keepAliveTimeout time is exceeded.

// Synchronize the coord address cycle to catord. Unit: milliseconds.

// 0 indicates that synchronization is not performed.

DsOpt. setSyncCoordInterval (0 );

// Check whether the connection availability is detected when the connection is out of the pool. It is not detected by default.

DsOpt. setValidateConnection (false );

// By default, the coord address Server Load balancer policy is used to obtain the connection.

DsOpt. setConnectStrategy (ConnectStrategy. BALANCE );

// Create a connection pool

Ds = new SequoiadbDatasource (addrs, user, password, nwOpt, dsOpt );

// Use the connection pool to run the task

RunTask (ds );

// Close the connection pool after the task is completed

Ds. close ();

}

Static void runTask (SequoiadbDatasource ds) throws InterruptedException {

String clFullName = "mycs. mycl ";

// Prepare the task

Thread createCLTask = new Thread (new CreateCLTask (ds, clFullName ));

Thread insertTask = new Thread (new InsertTask (ds, clFullName ));

Thread queryTask = new Thread (new QueryTask (ds, clFullName ));

// Create a set

CreateCLTask. start ();

CreateCLTask. join ();

// Insert records to the set

InsertTask. start ();

Thread. sleep (3000 );

// Query records from the set

QueryTask. start ();

// Wait until the task ends

InsertTask. join ();

QueryTask. join ();

}

}

Class CreateCLTask implements Runnable {

Private SequoiadbDatasource ds;

Private String csName;

Private String clName;

Public CreateCLTask (SequoiadbDatasource ds, String clFullName ){

This. ds = ds;

This. csName = clFullName. split ("\.") [0];

This. clName = clFullName. split ("\.") [1];

}

@ Override

Public void run (){

Sequoiadb db = null;

CollectionSpace cs = null;

DBCollection cl = null;

// Obtain the connection pool from the connection pool

Try {

Db = ds. getConnection ();

} Catch (BaseException e ){

E. printStackTrace ();

System. exit (1 );

} Catch (InterruptedException e ){

E. printStackTrace ();

System. exit (1 );

}

// Create a set using a connection

If (db. isCollectionSpaceExist (csName ))

Db. dropCollectionSpace (csName );

Cs = db. createCollectionSpace (csName );

Cl = cs. createCollection (clName );

// Return the connection pool

Ds. releaseConnection (db );

System. out. println ("Suceess to create collection" + csName + "." + clName );

}

}

Class InsertTask implements Runnable {

Private SequoiadbDatasource ds;

Private String csName;

Private String clName;

Public InsertTask (SequoiadbDatasource ds, String clFullName ){

This. ds = ds;

This. csName = clFullName. split ("\.") [0];

This. clName = clFullName. split ("\.") [1];

}

@ Override

Public void run (){

Sequoiadb db = null;

CollectionSpace cs = null;

DBCollection cl = null;

BSONObject record = null;

// Obtain the connection from the connection pool

Try {

Db = ds. getConnection ();

} Catch (BaseException e ){

E. printStackTrace ();

System. exit (1 );

} Catch (InterruptedException e ){

E. printStackTrace ();

System. exit (1 );

}

// Use the connection to obtain the collection object

Cs = db. getCollectionSpace (csName );

Cl = cs. getCollection (clName );

// Insert records using a collection object

Record = genRecord ();

Cl. insert (record );

// Return the connection pool

Ds. releaseConnection (db );

System. out. println ("Suceess to insert record:" + record. toString ());

}

Private BSONObject genRecord (){

BSONObject obj = new BasicBSONObject ();

Obj. put ("name", "James ");

Obj. put ("age", 30 );

Return obj;

}

}

Class QueryTask implements Runnable {

Private SequoiadbDatasource ds;

Private String csName;

Private String clName;

Public QueryTask (SequoiadbDatasource ds, String clFullName ){

This. ds = ds;

This. csName = clFullName. split ("\.") [0];

This. clName = clFullName. split ("\.") [1];

}

@ Override

Public void run (){

Sequoiadb db = null;

CollectionSpace cs = null;

DBCollection cl = null;

DBCursor cursor = null;

// Obtain the connection from the connection pool

Try {

Db = ds. getConnection ();

} Catch (BaseException e ){

E. printStackTrace ();

System. exit (1 );

} Catch (InterruptedException e ){

E. printStackTrace ();

System. exit (1 );

}

// Use the connection to obtain the collection object

Cs = db. getCollectionSpace (csName );

Cl = cs. getCollection (clName );

// Query using a collection object

Cursor = cl. query ();

Try {

While (cursor. hasNext ()){

System. out. println ("The inserted record is:" + cursor. getNext ());

}

} Finally {

Cursor. close ();

}

// Return the connection object to the connection pool

Ds. releaseConnection (db );

}

}

Because SequoiaDB's javaAPI involves a large number of operations, we will not give an example here. The usage of other operations is similar. For details, refer:

Https://doc.sequoiadb.com/cn/index/Public/Home/document/208/api/java/html/index.html

6. SequoiaDB's support for the persistent layer framework

In software development, almost all large-scale applications use the framework for development. The framework is reusable, which greatly improves the development speed of an application. Similar to Mybatis, Hibernate, and so on, these frameworks encapsulate the jdbc database operation process, so that developers only need to pay attention to the SQL itself, it does not require effort to handle complicated jdbc Process Code, such as registering a driver, creating a connection, creating a statement, manually setting parameters, and retrieving result sets. SequoiaSql, A SequoiaDB database suite, is an extension of PostgreSQL functionality. With SequoiaSql, you can use SQL statements to access the SequoiaDB database and perform operations such as adding, deleting, modifying, and querying databases. When SequoiaSql integrates the persistent layer framework, you only need to use the jdbc connection driver of postgreSQL. the integration process is the same as that of other frameworks when integrating postgreSQL.

7. Summary

With the explosive growth of data, centralized databases are becoming increasingly inadequate, and distributed databases are increasingly favored by developers. SequoiaDB is a NewSQL database that supports SQL, high concurrency, real-time, distributed, scalable, and flexible storage, supports connections to Spark, Hadoop, hive, and other big data products, and provides drivers in multiple languages for developers to use. SequoiaDB is a good choice when you choose to use distributed databases.

Related Article

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.