通过
Java SDK进行操作
下载Java SDK
请访问
http://ots.aliyun.com/,按照下图点击
Java SDK下载。
解压
Java SDK,
aliyun-openservices.jar是
SDK的包,
lib目录下包含的是所有依赖的
jar包,
doc目录下包含的是
Java SDK的文档。
加入Java SDK引用
在
NetBeans或
Eclipse等
IDE中导入
Java SDK包
aliyun-openservices.jar和位于
SDK lib目录下的相关依赖包
: commons-codec-1.4.jar, commons-logging-1.1.1.jar, httpclient-4.1.2.jar, httpcore-4.1.2.jar, jdom.jar.
创建表
下面这段Java代码演示了如何通过create table API来创建一个名为UserTable而且有一个主键列为UserID的表,并通过调用list table API验证了这个Table创建成功。
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyType;
import com.aliyun.openservices.ots.model.TableMeta;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
TableMeta tableMeta = new TableMeta("UserTable");
tableMeta.addPrimaryKey("UserID", PrimaryKeyType.STRING);
tableMeta.setPagingKeyLen(0); // 构造表结构
otsClient.createTable(tableMeta); // 调用OTS进行创建表操作
}
}
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyValue;
import com.aliyun.openservices.ots.model.RowPutChange;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
RowPutChange rowChange = new RowPutChange();
rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001")); // 构造插入行的PK
otsClient.putData("UserTable", rowChange, ""); // 调用OTS插入一行
rowChange = new RowPutChange();
rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0002")); // 构造插入行的PK
otsClient.putData("UserTable", rowChange, ""); // 调用OTS插入一行
}
}
查询数据
1.通过主键列查询数据
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyValue;
import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;
import com.aliyun.openservices.ots.model.Row;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");
rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001")); // 构造查询单行的PK
Row row = otsClient.getRow(rowQuery, ""); // 调用OTS进行单行查询操作
System.out.println(row.getColumns());
}
}
这样,我们就能读取主键列为U0001的行。
2.通过范围查询数据
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyValue;
import com.aliyun.openservices.ots.model.RangeRowQueryCriteria;
import com.aliyun.openservices.ots.model.Row;
import java.util.List;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
RangeRowQueryCriteria rowQuery = new RangeRowQueryCriteria("UserTable");
rowQuery.setRange("UserID", PrimaryKeyValue.fromString("A"),
PrimaryKeyValue.fromString("Z")); // 构造查询的范围
List<Row> rows = otsClient.getRowsByRange(rowQuery, ""); // 调用OTS进行范围查询
for (Row row : rows)
System.out.println(row.getColumns());
}
}
这样,我们就能读取主键列为U0001和U0002的行。
修改数据
1. 插入数据。向已经存在的行中插入值。插入两列值,列名分别为Gender和Name。
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyValue;
import com.aliyun.openservices.ots.model.ColumnValue;
import com.aliyun.openservices.ots.model.RowPutChange;
import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;
import com.aliyun.openservices.ots.model.Row;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
RowPutChange rowChange = new RowPutChange();
rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));
rowChange.addAttributeColumn("Name", ColumnValue.fromString("Bob"));
rowChange.addAttributeColumn("Gender", ColumnValue.fromString("Male"));// 构造更新数据的PK和列
otsClient.putData("UserTable", rowChange, ""); // 调用OTS进行修改数据操作
SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");
rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001")); // 构造单行查询PK
Row row = otsClient.getRow(rowQuery, ""); // 调用OTS进行单行查询
System.out.println(row.getColumns());
}
}
读取一行数据之后,多了两列值,Bob和Male。
2. 删除数据。删除PK为U0001,列名为Gender的值。
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.PrimaryKeyValue;
import com.aliyun.openservices.ots.model.RowDeleteChange;
import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;
import com.aliyun.openservices.ots.model.Row;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
RowDeleteChange rowChange = new RowDeleteChange();
rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));
rowChange.deleteColumn("Gender"); // 构造删除数据的PK和列
otsClient.deleteData("UserTable", rowChange, ""); // 调用OTS进行删除数据操作
SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");
rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001")); // 构造单行查询PK
Row row = otsClient.getRow(rowQuery, ""); // 调用OTS进行单行查询
System.out.println(row.getColumns());
}
}
可以从读取一行的结果中看到,列名为Gender的列已经被删除了。
删除表
删除我们创建的表UserTable。
package otsdemo;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
public class OTSDemo {
public static void main(String[] args) throws OTSException, ClientException {
final String endpoint = "http://service.ots.aliyun.com"; // OTS服务地址
final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX"; // AccessID
final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX="; // AccessKey
OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);
otsClient.deleteTable("UserTable"); // 调用OTS删除表
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。