数据推送
本文介绍如何通过召回引擎版JDBC SDK客户端将数据实时同步到召回引擎版的实例中,支持的更新操作有:add、delete。
add操作示例
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
public class Push {
private static Connection connection;
@Before
public void initConnection() throws ClassNotFoundException, SQLException {
// 设置驱动
String name = "com.aliyun.ha3engine.jdbc.Ha3Driver";
Properties props = new Properties();
// API域名,可在实例详情页>API入口 查看
props.setProperty("serviceName", "ha-cn-i7*****605.public.ha.aliyuncs.com");
// 用户名,可在实例详情页>API入口 查看
props.setProperty("username", "username");
// 密码,可在实例详情页>API入口 修改
props.setProperty("password", "password");
// 开启动态参数
props.setProperty("enableDynamicParams", "true");
props.setProperty("enableDetailLog", "true");
Class.forName(name);
connection = DriverManager.getConnection("jdbc:ha3://", props);
}
/**
* 推送数据
* @throws SQLException
*/
@Test
public void push1() throws SQLException {
// 使用原生的Statement,sql语句中第一个字段必须是主键
Statement statement = connection.createStatement();
int count = statement.executeUpdate("insert into `test1` (`id`,`title`,`body`,`count`,`price`) values\n" +
" (1, '测试标题1', '测试文本', 23, 28.55),\n" +
" (2, '测试标题2', '测试文本', 15, 26.77);");
System.out.println("insert count: " + count);
}
/**
* 推送数据
* @throws SQLException
*/
@Test
public void push2() throws SQLException {
// 使用PreparedStatement,可以通过params设置主键。如果不设置,则使用sql语句中第一个字段做为主键。
PreparedStatement preparedStatement = connection.prepareStatement("insert into test1 (`id`,`title`,`body`,`count`,`price`) values\n" +
" (3, ?, '测试文本', 37, 26.55),\n" +
" (4, ?, '测试文本', 6, 26.55);"
);
preparedStatement.setString(1, "测试标题3");
preparedStatement.setString(2, "测试标题4");
Map<String, Object> params = new HashMap<>();
params.put("pk", "id");
preparedStatement.setObject(0, params);
int count = preparedStatement.executeUpdate();
System.out.println("insert count: " + count);
}
}
delete操作示例
import java.sql.*;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
public class Delete {
private static Connection connection;
@Before
public void initConnection() throws ClassNotFoundException, SQLException {
// 设置驱动
String name = "com.aliyun.ha3engine.jdbc.Ha3Driver";
Properties props = new Properties();
// API域名,可在实例详情页>API入口 查看
props.setProperty("serviceName", "ha-cn-i7*****605.public.ha.aliyuncs.com");
// 用户名,可在实例详情页>API入口 查看
props.setProperty("username", "username");
// 密码,可在实例详情页>API入口 修改
props.setProperty("password", "password");
// 开启动态参数
props.setProperty("enableDynamicParams", "true");
props.setProperty("enableDetailLog", "true");
Class.forName(name);
connection = DriverManager.getConnection("jdbc:ha3://", props);
}
/**
* 删除数据,使用 id =
* @throws SQLException
*/
@Test
public void delete1() throws SQLException {
// 删除只支持主键删除,且只支持 id = 或 id in 两种形式
Statement statement = connection.createStatement();
int count = statement.executeUpdate("delete from `test1` where `id` = 4");
System.out.println("delete count: " + count);
}
/**
* 删除数据,使用 id in
* @throws SQLException
*/
@Test
public void delete2() throws SQLException {
// 删除只支持主键删除,且只支持 id = 或 id in 两种形式
Statement statement = connection.createStatement();
int count = statement.executeUpdate("delete from `test1` where `id` in (2,3)");
System.out.println("delete count: " + count);
}
/**
* 删除数据,使用 id =
* @throws SQLException
*/
@Test
public void delete3() throws SQLException {
// 删除只支持主键删除,且只支持 id = 或 id in 两种形式
PreparedStatement preparedStatement = connection.prepareStatement(
"delete from `test1` where `id` = 1");
int count = preparedStatement.executeUpdate();
System.out.println("delete count: " + count);
}
/**
* 删除数据,使用 id in
* @throws SQLException
*/
@Test
public void delete4() throws SQLException {
// 删除只支持主键删除,且只支持 id = 或 id in 两种形式
PreparedStatement preparedStatement = connection.prepareStatement(
"delete from `test1` where `id` in (2, ?, 4)");
preparedStatement.setInt(1, 3);
int count = preparedStatement.executeUpdate();
System.out.println("delete count: " + count);
}
}
该文章对您有帮助吗?