创建公共方法
public static IAcsClient create() {
DefaultProfile profile = DefaultProfile.getProfile(
“”, // 您的可用区ID
“”, // 您的AccessKey ID
“”); // 您的AccessKey Secret
return new DefaultAcsClient(profile);
}
获取文本库列表
private static void describeKeywordLibRequest(IAcsClient client) {
DescribeKeywordLibRequest request = new DescribeKeywordLibRequest();
// open_api:用于内容检测API功能的文本库。
request.setServiceModule(“open_api”);
try {
DescribeKeywordLibResponse response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response, true));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println(“ErrCode:” + e.getErrCode());
System.out.println(“ErrMsg:” + e.getErrMsg());
System.out.println(“RequestId:” + e.getRequestId());
}
}
创建文本库
private static void createKeywordLib(IAcsClient client) {
CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
createKeywordLibRequest.setServiceModule(“open_api”);
createKeywordLibRequest.setName(“测试关键词文本库”);
//设置为文本反垃圾场景使用
createKeywordLibRequest.setResourceType(“TEXT”);
//设置类型为关键词
createKeywordLibRequest.setLibType(“textKeyword”);
//设置创建黑库
createKeywordLibRequest.setCategory(“BLACK”);
try {
CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
System.out.println(JSON.toJSONString(describeKeywordLibResponse, true));
} catch (ClientException e) {
e.printStackTrace();
}
}
修改文本库
private static void updateKeywordLib(IAcsClient client) {
UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
//设置要操作的文本库id
updateKeywordLibRequest.setId(6743);
//设置新的文本库名称
updateKeywordLibRequest.setName(“测试修改名称”);
//设置新的bizType
updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList(“comment”, “title”)));
try {
UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
System.out.println(JSON.toJSONString(updateKeywordLibResponse, true));
} catch (ClientException e) {
e.printStackTrace();
}
}
删除文本库
private static void deleteKeywordLib(IAcsClient client) {
DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
//设置要删除的文本库id
deleteKeywordLibRequest.setId(6743);
try {
DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
System.out.println(JSON.toJSONString(deleteKeywordLibResponse, true));
} catch (ClientException e) {
e.printStackTrace();
}
}
查找文本
private static void describeKeyword(IAcsClient client) {
DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
//要查询的文本库id
describeKeywordRequest.setKeywordLibId(6741);
describeKeywordRequest.setPageSize(10);
describeKeywordRequest.setCurrentPage(1);
//可选,用于模糊查
describeKeywordRequest.setKeyword(“你”);
try {
DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
System.out.println(JSON.toJSONString(describeKeywordResponse, true));
} catch (ClientException e) {
e.printStackTrace();
}
}
添加文本
private static void createKeyword(IAcsClient client) {
CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
//设置文本库id
createKeywordRequest.setKeywordLibId(6741);
//要添加的文本
createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList(“法克儿”)));
try {
CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
System.out.println(JSON.toJSONString(createKeywordResponse,true));
} catch (ClientException e) {
e.printStackTrace();
}
}
删除文本
private static void deleteKeyword(IAcsClient client) {
DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
//设置文本id
deleteKeywordRequest.setKeywordLibId(String.valueOf(6741));
//要删除的文本id
deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2)));
try {
DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
System.out.println(JSON.toJSONString(deleteKeywordResponse));
} catch (ClientException e) {
e.printStackTrace();
}
}