3、SDK获取和安装
您可以通过如下方式获取和安装 SDK: 通过 Maven 安装依赖(推荐) 通过 Maven 安装项目依赖是使用 Java SDK 的推荐方法,首先您需要在您的操作系统中下载并安装 Maven ,安装完成后您只需在 Java 项目的 pom.xml 文件加入相应的依赖项即可。
本示例使用KVS SDK:
Copied!
<dependencies>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-kvs</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
4、代码示例
以下代码展示如何使用上传、查询、删除KV相关SDK
4.1 导入依赖模块
Copied!
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.kvs.v1.KvsClient;
import com.huaweicloud.sdk.kvs.v1.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
4.2 初始化认证信息
Copied!
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
BasicCredentials credentials = new BasicCredentials()
.withAk(ak)
.withSk(sk);
相关参数说明如下所示:
- HUAWEICLOUD_SDK_AK:华为云账号Access Key。
- HUAWEICLOUD_SDK_SK:华为云账号Secret Access Key 。
4.3 初始化KVS服务的客户端
Copied!
HttpConfig config = HttpConfig.getDefaultHttpConfig();
config.withIgnoreSSLVerification(true);
String regionId = "${YOUR_REGION_ID}";
String endpoint = "${KVS_ENDPOINT}";
Region region = new Region(regionId, endpoint);
KvsClient kvsClient = KvsClient.newBuilder()
.withCredential(credentials)
.withRegion(region)
.withHttpConfig(config)
.build();
相关参数说明如下所示:
- credentials: 用户认证信息,见4.2的初始化认证信息。
- regionId:KVS集群所在区域Id。
- endpoint: KVS服务域名。
- config: 客户端属性。
4.4 发送请求
构造请求,然后通过4.3初始化好的客户端发送请求。
4.4.1 创建表
发送创建表请求,向指定仓中创建表,如果仓不存在,将会在自动创建仓。
Copied!
public void createTable(KvsClient client) {
List<Field> shardKeyFields = new ArrayList<>();
shardKeyFields.add(new Field().withName("{your shard key name}").withOrder(true));
List<Field> sortKeyFields = new ArrayList<>();
sortKeyFields.add(new Field().withName("{your sort key name}").withOrder(true));
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema().
withSortKeyFields(sortKeyFields).
withShardKeyFields(shardKeyFields);
CreateTableRequest createTableRequest = new CreateTableRequest().withBody(new CreateTableRequestBody()
.withTableName("{your table name}")
.withPrimaryKeySchema(primaryKeySchema))
.withStoreName("{your store name}");
try {
CreateTableResponse createTableResponse = client.createTable(createTableRequest);
LOGGER.info(createTableResponse.toString());
} catch (ClientRequestException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.toString());
} catch (ServerResponseException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.getMessage());
}
}
4.4.2 插入KV数据
发送插入kv请求,向4.4.1创建的表中插入kv数据。
Copied!
public void putKv(KvsClient client) {
Document kvDoc = new Document();
kvDoc.put("{your shard key name}", "{your shard key value}");
kvDoc.put("{your sort key name}", "{your sort key value}");
kvDoc.put("{your other key}","{your other value}");
PutKvRequest putKvRequest = new PutKvRequest().withBody(new PutKvRequestBody().withKvDoc(kvDoc).
withTableName("{your table name}")).
withStoreName("{your store name}");
try {
PutKvResponse putKvResponse = client.putKv(putKvRequest);
LOGGER.info(putKvResponse.toString());
} catch (ClientRequestException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.toString());
} catch (ServerResponseException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.getMessage());
}
}
4.4.3 查询KV数据
发送查询kv请求,查询4.4.2上传的kv数据。
Copied!
public void getKv(KvsClient client) {
Document primaryKey = new Document();
primaryKey.put("{your shard key name}", "{your shard key value}");
primaryKey.put("{your sort key name}", "{your sort key value}");
GetKvRequest getKvRequest = new GetKvRequest().withBody(new GetKvRequestBody().
withTableName("{your table name}").
withPrimaryKey(primaryKey))
.withStoreName("{your store name}");
try {
GetKvResponse getKvResponse = client.getKv(getKvRequest);
LOGGER.info(getKvResponse.toString());
} catch (ClientRequestException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.toString());
} catch (ServerResponseException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.getMessage());
}
}
4.4.4 删除KV数据
发送删除kv请求,将4.4.2上传的kv数据从表中删除。
Copied!
public void deleteKv(KvsClient client) {
Document primaryKey = new Document();
primaryKey.put(shardkeyName, "{your shard key value}");
primaryKey.put(sortkeyName, "{your sort key value}");
DeleteKvRequest deleteKvRequest = new DeleteKvRequest().withBody(new DeleteKvRequestBody().
withTableName("{your table name}").
withPrimaryKey(primaryKey)).
withStoreName("{your store name}");
try {
DeleteKvResponse deleteKvResponse = client.deleteKv(deleteKvRequest);
LOGGER.info(deleteKvResponse.toString());
} catch (ClientRequestException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.toString());
} catch (ServerResponseException e) {
LOGGER.error(String.valueOf(e.getHttpStatusCode()));
LOGGER.error(e.getMessage());
}
}
1、介绍
华为云提供了KVS服务端SDK,您可以直接集成服务端SDK来调用KVS的相关API,从而实现对KVS服务的快速操作。KVS服务提供完全托管的键值存储及索引服务,主要用于应用的键值类数据(如:元数据、描述数据、管理参数、状态数据)的存储。接下来将介绍如何通过java版SDK创建表、向表中插入KV数据,查询表中的KV数据,以及删除kv数据。
2、前置条件
3、SDK获取和安装
您可以通过如下方式获取和安装 SDK: 通过 Maven 安装依赖(推荐) 通过 Maven 安装项目依赖是使用 Java SDK 的推荐方法,首先您需要在您的操作系统中下载并安装 Maven ,安装完成后您只需在 Java 项目的 pom.xml 文件加入相应的依赖项即可。 本示例使用KVS SDK:
<dependencies> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-kvs</artifactId> <version>${version}</version> </dependency> </dependencies>
4、代码示例
以下代码展示如何使用上传、查询、删除KV相关SDK
4.1 导入依赖模块
//用户身份认证 import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.http.HttpConfig; import com.huaweicloud.sdk.core.region.Region; //异常类 import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ServerResponseException; //Kvs接口类 import com.huaweicloud.sdk.kvs.v1.KvsClient; import com.huaweicloud.sdk.kvs.v1.model.*; //日志类 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import org.bson.Document;
4.2 初始化认证信息
String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); // 创建认证 BasicCredentials credentials = new BasicCredentials() .withAk(ak) .withSk(sk);
相关参数说明如下所示:
4.3 初始化KVS服务的客户端
// 配置客户端属性 HttpConfig config = HttpConfig.getDefaultHttpConfig(); config.withIgnoreSSLVerification(true); // 创建KvsClient实例并初始化 String regionId = "${YOUR_REGION_ID}"; String endpoint = "${KVS_ENDPOINT}"; Region region = new Region(regionId, endpoint); KvsClient kvsClient = KvsClient.newBuilder() .withCredential(credentials) .withRegion(region) .withHttpConfig(config) .build();
相关参数说明如下所示:
4.4 发送请求
构造请求,然后通过4.3初始化好的客户端发送请求。
4.4.1 创建表
发送创建表请求,向指定仓中创建表,如果仓不存在,将会在自动创建仓。
/** * 创建表 * * @param client kvs客户端 */ public void createTable(KvsClient client) { // 设置分区键的名称和排序(必选) List<Field> shardKeyFields = new ArrayList<>(); shardKeyFields.add(new Field().withName("{your shard key name}").withOrder(true)); // 设置排序键的名称和排序(非必选) List<Field> sortKeyFields = new ArrayList<>(); sortKeyFields.add(new Field().withName("{your sort key name}").withOrder(true)); // 设置主键 PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema(). withSortKeyFields(sortKeyFields). withShardKeyFields(shardKeyFields); // 构造创表请求,指定存储仓和表名(必选) CreateTableRequest createTableRequest = new CreateTableRequest().withBody(new CreateTableRequestBody() .withTableName("{your table name}") .withPrimaryKeySchema(primaryKeySchema)) .withStoreName("{your store name}"); try { // 发送创表请求 CreateTableResponse createTableResponse = client.createTable(createTableRequest); LOGGER.info(createTableResponse.toString()); } catch (ClientRequestException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.toString()); } catch (ServerResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getMessage()); } }
4.4.2 插入KV数据
发送插入kv请求,向4.4.1创建的表中插入kv数据。
/** * 插入KV * * @param client kvs客户端 */ public void putKv(KvsClient client) { // 设置主键(必选) Document kvDoc = new Document(); kvDoc.put("{your shard key name}", "{your shard key value}"); kvDoc.put("{your sort key name}", "{your sort key value}"); // 设置kv数据 kvDoc.put("{your other key}","{your other value}"); // 构造插入kv请求 PutKvRequest putKvRequest = new PutKvRequest().withBody(new PutKvRequestBody().withKvDoc(kvDoc). withTableName("{your table name}")). withStoreName("{your store name}"); try { // 发送插入kv请求 PutKvResponse putKvResponse = client.putKv(putKvRequest); LOGGER.info(putKvResponse.toString()); } catch (ClientRequestException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.toString()); } catch (ServerResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getMessage()); } }
4.4.3 查询KV数据
发送查询kv请求,查询4.4.2上传的kv数据。
/** * 查询KV * * @param client kvs客户端 */ public void getKv(KvsClient client) { // 设置主键(必选) Document primaryKey = new Document(); primaryKey.put("{your shard key name}", "{your shard key value}"); primaryKey.put("{your sort key name}", "{your sort key value}"); // 构造查询kv请求 GetKvRequest getKvRequest = new GetKvRequest().withBody(new GetKvRequestBody(). withTableName("{your table name}"). withPrimaryKey(primaryKey)) .withStoreName("{your store name}"); try { // 发送插入kv请求 GetKvResponse getKvResponse = client.getKv(getKvRequest); LOGGER.info(getKvResponse.toString()); } catch (ClientRequestException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.toString()); } catch (ServerResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getMessage()); } }
4.4.4 删除KV数据
发送删除kv请求,将4.4.2上传的kv数据从表中删除。
/** * 删除KV * * @param client kvs客户端 */ public void deleteKv(KvsClient client) { // 设置主键,删除主键指定的kv文档(必选) Document primaryKey = new Document(); primaryKey.put(shardkeyName, "{your shard key value}"); primaryKey.put(sortkeyName, "{your sort key value}"); DeleteKvRequest deleteKvRequest = new DeleteKvRequest().withBody(new DeleteKvRequestBody(). withTableName("{your table name}"). withPrimaryKey(primaryKey)). withStoreName("{your store name}"); try { // 发送删除kv请求 DeleteKvResponse deleteKvResponse = client.deleteKv(deleteKvRequest); LOGGER.info(deleteKvResponse.toString()); } catch (ClientRequestException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.toString()); } catch (ServerResponseException e) { LOGGER.error(String.valueOf(e.getHttpStatusCode())); LOGGER.error(e.getMessage()); } }
5、修订记录