When you use the Timeline model, you must initialize TimelineStoreFactory, TimelineMetaStore, and TimelineStore. This topic describes how to perform the initialization configuration and operations.
Prerequisites
An AccessKey pair is configured. For more information, see Configure an AccessKey pair.
The endpoint of a Tablestore instance is obtained. For more information, see Obtain an endpoint of a Tablestore instance.
Initialize TimelineStoreFactory
Use SyncClient as a parameter to initialize TimelineStoreFactory and create a Store to manage Meta data and Timeline data.
The retry operation required due to an error depends on the retry policy of SyncClient. You can configure SyncClient for the retry operation. If you have any special requirements, you can call the RetryStrategy operation to specify a custom retry policy.
/**
* Configure the retry policy.
* Code: configuration.setRetryStrategy(new DefaultRetryStrategy());
**/
ClientConfiguration configuration = new ClientConfiguration();
SyncClient client = new SyncClient(
"http://instanceName.cn-shanghai.ots.aliyuncs.com",
System.getenv("OTS_AK_ENV"),
System.getenv("OTS_SK_ENV"),
"instanceName", configuration);
TimelineStoreFactory serviceFactory = new TimelineStoreFactoryImpl(client);
Initialize TimelineMetaStore
Create a schema for a Meta table. The schema includes parameters such as Identifier and MetaIndex. Create and obtain a Store to manage Meta data by using TimelineStoreFactory. You need to specify the following parameters: Meta table name, index name, primary key field, and index type.
TimelineIdentifierSchema idSchema = new TimelineIdentifierSchema.Builder()
.addStringField("timeline_id").build();
IndexSchema metaIndex = new IndexSchema();
metaIndex.setFieldSchemas(Arrays.asList(//Specify the names and types of index fields.
new FieldSchema("group_name", FieldType.TEXT).setIndex(true).setAnalyzer(FieldSchema.Analyzer.MaxWord),
new FieldSchema("create_time", FieldType.LONG).setIndex(true)
));
TimelineMetaSchema metaSchema = new TimelineMetaSchema("groupMeta", idSchema)
.withIndex("metaIndex", metaIndex); //Configure the index.
TimelineMetaStore timelineMetaStore = serviceFactory.createMetaStore(metaSchema);
Create a table
Create a table based on the parameters in metaSchema. If an index is configured in metaSchema, the index is created after the table is created.
timelineMetaStore.prepareTables();
Delete a table
If an index is created for a table, the index is deleted before the table is deleted.
timelineMetaStore.dropAllTables();
Initialize TimelineStore
Create a schema for a Timeline table. The schema includes parameters such as Identifier and TimelineIndex. Create and obtain a Store to manage Timeline data by using TimelineStoreFactory. You need to specify the following parameters: Timeline table name, index name, primary key field, and index type.
The BatchStore operation improves the concurrency performance on the basis of DefaultTableStoreWriter of Tablestore. You can set the number of concurrent threads in the thread pool.
TimelineIdentifierSchema idSchema = new TimelineIdentifierSchema.Builder()
.addStringField("timeline_id").build();
IndexSchema timelineIndex = new IndexSchema();
timelineIndex.setFieldSchemas(Arrays.asList(// Specify the names and types of index fields.
new FieldSchema("text", FieldType.TEXT).setIndex(true).setAnalyzer(FieldSchema.Analyzer.MaxWord),
new FieldSchema("receivers", FieldType.KEYWORD).setIndex(true).setIsArray(true)
));
TimelineSchema timelineSchema = new TimelineSchema("timeline", idSchema)
.autoGenerateSeqId() //Specify the auto-increment primary key column as the method to generate the SequenceId value.
.setCallbackExecuteThreads(5) //Set the number of initial threads of DefaultTableStoreWriter to 5.
.withIndex("metaIndex", timelineIndex); //Configure the index.
TimelineStore timelineStore = serviceFactory.createTimelineStore(timelineSchema);
Create a table
Create a table based on the parameters in TimelineSchema. If an index is configured in TimelineSchema, the index is created after the table is created.
timelineStore.prepareTables();
Delete a table
If an index is created for a table, the index is deleted before the table is deleted.
timelineStore.dropAllTables();