[TDR List 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 List 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.ListTableTraverser;
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
ListTableTraverser traverser = client.getListTableTraverser();
// 3.2. Set the target table name. Note: The target table must be a List table
traverser.setTableName(tableName);
// 3.3. Add the Value field to be read
traverser.addFieldName("typeid");
traverser.addFieldName("Data");
traverser.addFieldName("uname");
// 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.ListTableTraverser;
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
ListTableTraverser traverser = client.getListTableTraverser();
// 3.2. Set the target table name. Note: The target table must be a List table
traverser.setTableName(tableName);
// 3.3. Add the Value field to be read
traverser.addFieldName("typeid");
traverser.addFieldName("Data");
traverser.addFieldName("uname");
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 (response.isTraverseCompleted()) {
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 Client object method, it means that the method is invalid in the scenario of scanning data.
| Method signature | Method description |
|---|---|
ListTableTraverser getListTableTraverser() |
Creates a traverser object through which subsequent scanning actions can be performed. |
6. Traversal Object Method Description
Note: If there is an unlisted ListTableTraverser object method, it means that the method is invalid in the scenario of scanning data.
| Method signature | Method description |
|---|---|
Traverser setTableName(String tableName) |
Set the target table name. TableName: target table name, which cannot be null. |
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 List Table][C++ SDK] Interface Description for Traversing Table Data](../../01C++_SDK/02Interface_Documents/27[List_Table]Traverse_Table_Data.md
[[TDR List Table] [Go SDK] Interface Description for Traversing Full Table Data](../../03Go_SDK/02Interface_Documents/27[List_Table]Traverse_Table_Data.md