To use the Timeline model, you must initialize three core components: TimelineStoreFactory, TimelineMetaStore, and TimelineStore. This topic explains the initialization workflow, describes each component, and provides Java code examples.
Prerequisites
An AccessKey pair is configured. For more information, see Initialize a Tablestore client.
The endpoint of a Tablestore instance is obtained. For more information, see Initialize a Tablestore client.
Object model
The following table lists the key classes involved in Timeline model initialization and their purpose.
| Class | Purpose |
|---|---|
SyncClient | Synchronous client for Tablestore that provides methods to manage tables and data. Supports configurable retry policies. |
TimelineStoreFactory | Factory that creates TimelineMetaStore and TimelineStore instances from a shared SyncClient. |
TimelineIdentifierSchema | Defines the primary key column structure for identifying timelines. |
IndexSchema | Defines search index fields for full-text search and Boolean query. |
TimelineMetaStore | Manages metadata for timelines. Metadata is stored in a free-schema structure and can contain any column. |
TimelineStore | Manages timeline message data with support for auto-incrementing sequence IDs and batch writing. |
Initialization dependency chain
The following diagram shows how the components depend on each other during initialization:
SyncClient (connection + authentication)
|
v
TimelineStoreFactory
|
+--> TimelineMetaStore (metadata table + search index)
| |
| +--> prepareTables() / dropAllTables()
|
+--> TimelineStore (message table + search index + batch writer)
|
+--> prepareTables() / dropAllTables()Initialize TimelineStoreFactory
TimelineStoreFactory is the entry point for creating stores that manage metadata and timeline data. You initialize it by passing a SyncClient instance.
Retry behavior is determined by the retry policy of SyncClient. You can configure SyncClient with a custom retry policy. If you have specific requirements, call the RetryStrategy operation to define a custom retry policy.
/**
* Configure the retry policy.
* Code: configuration.setRetryStrategy(new DefaultRetryStrategy());
**/
ClientConfiguration configuration = new ClientConfiguration();
SyncClient client = new SyncClient(
"yourEndpoint",
// Obtain the AccessKey ID and AccessKey secret from the environment variables.
System.getenv("TABLESTORE_ACCESS_KEY_ID"),
System.getenv("TABLESTORE_ACCESS_KEY_SECRET"),
"yourInstanceName", configuration);
TimelineStoreFactory serviceFactory = new TimelineStoreFactoryImpl(client);Initialize TimelineMetaStore
TimelineMetaStore manages metadata for your timelines. To initialize it, define a schema that specifies the primary key column structure (Identifier) and the search index configuration (MetaIndex), then use TimelineStoreFactory to create the store.
You need to specify the following parameters: Meta table name, search index name, primary key column, and search 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
Call prepareTables() to create a table based on the parameters in metaSchema. If a search index is configured in metaSchema, the search index is created after the table is created.
timelineMetaStore.prepareTables();Delete a table
Call dropAllTables() to delete the table. If a search index exists for the table, the search index is deleted before the table is deleted.
timelineMetaStore.dropAllTables();Initialize TimelineStore
TimelineStore manages timeline message data. Similar to TimelineMetaStore, you define a schema that specifies the primary key column structure (Identifier) and the search index configuration (TimelineIndex), then use TimelineStoreFactory to create the store.
You need to specify the following parameters: Timeline table name, search index name, primary key column, and search index type.
The BatchStore operation improves concurrency performance based on 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
Call prepareTables() to create a table based on the parameters in TimelineSchema. If a search index is configured in TimelineSchema, the search index is created after the table is created.
timelineStore.prepareTables();Delete a table
Call dropAllTables() to delete the table. If a search index exists for the table, the search index is deleted before the table is deleted.
timelineStore.dropAllTables();