SnappyDB-NoSQL database on Android, snappydbnosql
Or starting from the demand. When developing an App, you often need to cache some data so that it is not empty every time you open the App. You need to download data from the network. For example, a news client needs to cache the last opened news.
Generally, the SQLite database is used to store data or serialize the data to a local file. I have used these two methods in my previous projects. Let me first look at the disadvantages of the two methods:
- Using SQLite database for saving: This is too important for storing cached data, and it is troublesome to access data. Of course, you also need to know the SQL syntax. Carefully design the database structure. For relatively complex data, you need to design multiple tables. You also need to be careful when maintaining database upgrades.
- Using file Caching: Writing and saving files requires you to implement the Serializable interface for all the data you save. Of course, this is not a big problem. You need to maintain the structure of your file content. If there is a large amount of data, you may need to maintain the reading and writing of multiple files. Performance is also worrying.
The disadvantage of the above methods is naturally to invite SnappyDB, the main character of this article.
SnappyDB is a key-value database and a very popular NoSQL database. It can save data of any basic type and Serializable security and its array.
The basic usage is as follows:
12345678910111213 |
DB snappydb = DBFactory.open(context); //create or open an existing databse using the default name snappydb.put( "name" , "Jack Reacher" ); snappydb.putInt( "age" , 42 ); snappydb.putBoolean( "single" , true ); snappydb.put( "books" , new String[]{ "One Shot" , "Tripwire" , "61 Hours" }); String name = snappydb.get( "name" ); int age = snappydb.getInt( "age" ); boolean single = snappydb.getBoolean( "single" ); String[] books = snappydb.getArray( "books" , String. class ); // get array of string snappydb.close(); |
You can see that it is very convenient to use, and the API is as simple as you don't need to learn.
In addition, SnappyDB uses the Kryo library when saving and reading sequence objects, and Java built-in serialization is faster. The bigger advantage is that you do not explicitly implement the Serializable interface for data. This means that your previous code should not be modified at all.
?
123 |
Number[] array = { new AtomicInteger ( 42 ), new BigDecimal( "10E8" ), Double.valueOf(Math.PI)}; snappyDB.put( "array" , array); |
For more API documentation, see the official Cookbook.
Let's take a look at the performance. For example, we can see that the performance is inferior to SQLite.
Of course, the data stability of SnappyDB is still to be verified. It should be inferior to the mature SQLite. I have not mentioned the multi-threaded access security issue. But from our needs (used to cache data), SnappyDB should be a good choice.
Here I have not compared other NoSQL databases on Android horizontally. For example, realm-java is a more rigorous implementation of NoSQL, as well as simple and lightweight implementation, such as Couchbase-Lite-Android and SimpleNoSQL. These have never been used, and it is difficult to make judgments. If they are used in the future, we will try again.
Http://shop116234616.taobao.com /? Spm = a1z10.1-c.0.0.SngWMc