[Java SDK] Get Table Structure
1. Interface Description
Get table structure data from 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 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.Record;
import com.tencent.tcaplus.client.Request;
import com.tencent.tcaplus.client.Response;
import com.tencent.tcaplus.util.TdrType;
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. Create a request to query data
// 3.1. Get the request object. In order to improve the SDK's performance, the Request object is reused
Request request = client.acquireRequest();
// 3.2. Set the request type and target table name.
req.setCmd(TcaplusProtocolCsConstants.TCAPLUS_CMD_METADATA_GET_REQ);
request.setTableName(tableName);
// 4. Send a request and get the result
Response response = client.poll(request);
// 5. Process the result
if (response.getResult() == 0) {
// Get table structure successfully
ResponseLite rspLite = (ResponseLite) response;
TableMetadata meta = rspLite.getMetadata();
//Get table fields
List<TableMetadata.KeyColumn> keyColumnList = meta.keyColumnList;
//And table field type
for (TableMetadata.KeyColumn keyColumn:keyColumnList) {
int type = keyColumn.type;
switch (type) {
case TdrType.TCAPLUS_RECORD_TYPE_BINARY:
//
break;
case TdrType.TCAPLUS_RECORD_TYPE_DOUBLE:
//
break;
default:
//
break;
}
}
} else {
// Failed to get table structure. It may be that the table does not exist
}
} 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.Record;
import com.tencent.tcaplus.client.Request;
import com.tencent.tcaplus.client.Response;
import com.tencent.tcaplus.util.TdrType;
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. Create a request to query data
// 3.1. Get the request object. In order to improve the SDK's performance, the Request object is reused
Request request = client.acquireRequest();
// 3.2. Set the request type and target table name.
req.setCmd(TcaplusProtocolCsConstants.TCAPLUS_CMD_METADATA_GET_REQ);
request.setTableName(tableName);
CountDownLatch latch = new CountDownLatch(1);
// 4. Send the request asynchronously and specify the processor that returns the response. The post method will return the result immediately
client.post(request, new Future() {
@Override
public void onResponse(Response response) {
// 5. Process the result
if (response.getResult() == 0) {
// Get table structure successfully
ResponseLite rspLite = (ResponseLite) response;
TableMetadata meta = rspLite.getMetadata();
//Get table fields
List<TableMetadata.KeyColumn> keyColumnList = meta.keyColumnList;
//And table field type
for (TableMetadata.KeyColumn keyColumn:keyColumnList) {
int type = keyColumn.type;
switch (type) {
case TdrType.TCAPLUS_RECORD_TYPE_BINARY:
//
break;
case TdrType.TCAPLUS_RECORD_TYPE_DOUBLE:
//
break;
default:
//
break;
}
}
} else {
// Failed to get table structure. It may be that the table does not exist
}
}
});
latch.await();
} finally {
// 6. Destroy the client object
ClientFactory.destroyClient(client);
}
}
}
5. Method Description in Request Object
Note: If a Request object method is not listed, it means that the method is invalid in the scenario of reading data.
| Method signature | Method description |
|---|---|
void setCmd(int cmd) |
Set the request type (command). cmd: request type, fixed to TcaplusProtocolCsConstants.TCAPLUS_CMD_METADATA_GET_REQ |
void setTableName(String tableName) |
Set the target table name. TableName: target table name, which cannot be null. |
6. Method Description in Response Object
Note: If a Response object method is not listed, it means that the method is invalid in the scenario of reading data.
| Method signature | Method description |
|---|---|
int getResult() |
Get the response code of the data query request. 0 indicates that the operation succeeds. If the value is not 0, the operation is abnormal. Please refer to Response Code Meaning Description. |
7. ResponseLite Object Method Description
Note: If there is an unlisted ResponseLite object method, it means that the method is invalid in the scenario of getting data.
| Method signature | Method description |
|---|---|
TableMetadata getMetadata() |
Get the meta structure of the table. Non-null indicates that the operation is successful, and null indicates that the operation is abnormal. |
8. TableMetadata Object Method Description
Note: If there is an unlisted TableMetadata object method, it means that the method is invalid in the scenario of getting data.
| Member variable | Member variable description |
|---|---|
| type | Table type, TCAPLUS_TABLE_TYPE_GENERIC and TCAPLUS_TABLE_TYPE_LIST are supported currently. |
| listMaxElementNum | The maximum number of elements supported by a Key in the List. |
| keyColumnList | Metadata information of the Key field. |
| valueColumnList | Metadata information of the Value field. |
9. TableMetadata.KeyColumn Object Method Description
Note: If there is an unlisted TableMetadata.KeyColumn object method, it means that the method is invalid in the scenario of getting data.
| Member variable | Member variable description |
|---|---|
| type | Field type. |
| name | Field name. |
10. TableMetadata.ValueColumn Object Method Description
Note: If there is an unlisted TableMetadata.ValueColumn object method, it means that the method is invalid in the scenario of getting data.
| Member variable | Member variable description |
|---|---|
| type | Field type. |
| name | Field name. |
11. FAQ
For details, see Meaning and Handling of Error Codes.
12. Other Reference Documents
None