开发者社区> 问答> 正文

通过Java SDK进行操作


通过 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进行创建表操作
    }
}

插入数据


下面这段Java代码演示了如果通过put data API来向名为UserTable的表中插入数据,在如下的示例代码中,我们插入了两列数据,主键列分别为U0001和U0002。

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删除表
    }
}


展开
收起
yzpc2003 2012-03-22 14:20:22 12452 0
6 条回答
写回答
取消 提交回答
  • Re通过JavaSDK进行操作
    这个有什么用,有远程界面操作爽吗?
    2012-11-05 15:03:55
    赞同 展开评论 打赏
  • Re通过JavaSDK进行操作
    好东西
    2012-11-04 20:22:48
    赞同 展开评论 打赏
  • Re通过JavaSDK进行操作
    这些数据来自哪里啊
    2012-11-01 16:32:08
    赞同 展开评论 打赏
  • Re通过JavaSDK进行操作
    看一下 凑个热闹来
    2012-06-12 21:10:48
    赞同 展开评论 打赏
  • Re通过JavaSDK进行操作
    应该是够了吧








    皮鞋 炒外汇 商务正装
    2012-06-12 15:05:08
    赞同 展开评论 打赏
  • 好啊啊啊啊啊啊啊啊啊啊
    2012-03-23 11:51:44
    赞同 展开评论 打赏
滑动查看更多
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载