[TDR Generic Table][Java SDK] Traverse Full Table Data
1. Interface Description
Traverse data in a specified table.
2. Version Requirements
This interface is provided in all versions without special requirements.
3. Preparations
Refer to Preparation document to complete the preparation before using this interface and create the following TDR Generic table.
Get the following information after the preparation. These details will be used by the SDK:
- Directory server address list
- App ID
- App access password
- Game zone ID
- Table name
4. Example Code
Basic execution process of example code:
- Create a client;
- Create a request;
- Send a request;
- Process the response;
- Destroy the client.
4.1 Example Code for Synchronous Call
import com.tencent.tcaplus.client.Client;
import com.tencent.tcaplus.client.ClientFactory;
import com.tencent.tcaplus.client.GenericTableTraverser;
import com.tencent.tcaplus.client.Record;
import com.tencent.tcaplus.client.Request;
import com.tencent.tcaplus.client.Response;
import com.tencent.tdr.tcaplus_protocol_cs.TcaplusProtocolCsConstants;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] arguments) {
// 1. Prepare the environment information
// 1.1. Directory server address list
List<String> dirList = new ArrayList<String>();
dirList.add("tcp://x.x.x.x:9999");
dirList.add("tcp://y.y.y.y:9999");
// 1.2. App ID
int appId = 1;
// 1.3. App password
String appPassword = "****************";
// 1.4. Table group ID
int tableGroupId = 1;
// 1.5. Table name
String tableName = "test";
// 2. Create a client
Client client = ClientFactory.createClient(appId, tableGroupId, appPassword, dirList);
try {
// 3. Construct a request to read data according to the value of partial Key fields
// 3.1. Create a scanner
GenericTableTraverser traverser = client.getGenericTableTraverser(tableName);
// 3.2. Add the Value field to be read
traverser.addFieldName("typeid");
traverser.addFieldName("Data");
traverser.addFieldName("uname");
// 3.3. Set the maximum amount of data that can be returned in a batch. Due to the large amount of data returned by scanning, it must be returned in batches.
traverser.setLoadOptions(100);
// 3.4. Set the maximum amount of data returned in total
traverser.setTotalLimit(10000);
// 4. Start the scanner
Iterator<Record> iterator = traverser.start();
// 5. Loop through iterators to get data until all data is read
while (iterator.hasNext()) {
Record result = iterator.next();
// TODO: add code to process the return data
}
} finally {
// 6. Destroy the client object
ClientFactory.destroyClient(client);
}
}
4.2 Example Code for Asynchronous Call
import com.tencent.tcaplus.client.Client;
import com.tencent.tcaplus.client.ClientFactory;
import com.tencent.tcaplus.client.GenericTableTraverser;
import com.tencent.tcaplus.client.Record;
import com.tencent.tcaplus.client.Request;
import com.tencent.tcaplus.client.Response;
import com.tencent.tdr.tcaplus_protocol_cs.TcaplusProtocolCsConstants;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] arguments) {
// 1. Prepare the environment information
// 1.1. Directory server address list
List<String> dirList = new ArrayList<String>();
dirList.add("tcp://x.x.x.x:9999");
dirList.add("tcp://y.y.y.y:9999");
// 1.2. App ID
int appId = 1;
// 1.3. App password
String appPassword = "****************";
// 1.4. Table group ID
int tableGroupId = 1;
// 1.5. Table name
String tableName = "test";
// 2. Create a client
Client client = ClientFactory.createClient(appId, tableGroupId, appPassword, dirList);
try {
// 3. Construct a request to read data according to the value of partial Key fields
// 3.1. Create a scanner
GenericTableTraverser traverser = client.getGenericTableTraverser(tableName);
// 3.2. Add the Value field to be read
traverser.addFieldName("typeid");
traverser.addFieldName("Data");
traverser.addFieldName("uname");
// 3.3. Set the maximum amount of data that can be returned in a batch. Due to the large amount of data returned by scanning, it must be returned in batches.
traverser.setLoadOptions(100);
// 3.4. Set the maximum amount of data returned in total
traverser.setTotalLimit(10000);
CountDownLatch latch = new CountDownLatch(1);
// 4. Start the scanner asynchronously and specify the return result processor
traverser.startAsync(new Future() {
@Override
public void onResponse(Response response) {
// 5. Process the result
if (response.getResult() == 0) {
for (Record result : response.getRecordList()) {
// TODO: add code to process the data
}
}
if (traverser.isCompleted()) {
latch.countDown();
}
}
});
latch.await();
} finally {
// 6. Destroy the client object
ClientFactory.destroyClient(client);
}
}
5. Method Description for the User to Obtain the Traverser Object
Note: If there is an unlisted method to obtain the traverser object, it means that the method is invalid in the scenario of scanning data.
| Method signature | Method description |
|---|---|
GenericTableTraverser getGenericTableTraverser(String tableName) |
Creates a traverser object through which subsequent scanning actions can be performed. TableName: target table name, which cannot be null. |
6. Traversal Object Method Description
Note: If there is an unlisted method to obtain the GenericTableTraverser object, it means that the method is invalid in the scenario of scanning data.
| Method signature | Method description |
|---|---|
void addFieldName(String fieldName) |
Add the name of the Value field of the data to be queried. fieldName: field name, which cannot be null. |
Iterator<Record> start() |
Start the traverser and get the data iterator. |
7. Method Description of Data Object Obtained from Data Iterator
Note: If there is an unlisted method to obtain the Record object, it means that the method is invalid in the scenario of scanning data.
| Method signature | Method description |
|---|---|
int getVersion() |
Get the version number of the data. |
int getValueCount() |
Get the number of Value fields of the data. |
byte getValueByte(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
short getValueShort(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
int getValueInt(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
long getValueLong(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
float getValueFloat(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
double getValueDouble(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
String getValueString(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
byte[] getValueBlob(String fieldName) |
Get the Value of the value field of the specified name. fieldName: field name, which cannot be null. |
8. FAQ
For details, see Meaning and Handling of Error Codes.
9. Other Reference Documents
[[TDR Generic Table][C++ SDK] Interface Description for Traversing Table Data](../../01C++_SDK/02Interface_Documents/13[Generic_Table]Traverse_Table_Data.md
[[TDR Generic Table] [Go SDK] Interface Description for Traversing Table Data](../../03Go_SDK/02Interface_Documents/13[Generic_Table]Traverse_Table_Data.md
[[TDR Generic Table] [MySQL Protocol Compatibility Interface] Interface Description for Traversing Full Table](../../04MySQL_Protocol_Compatibility_Interface/02Syntax_Description/05[Generic_Table]Traverse_Full_Table.md