全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
MaxCompute

简单下载示例

更新时间:2017-11-09 17:19:41

  1. import java.io.IOException;
  2. import java.util.Date;
  3. import com.aliyun.odps.Column;
  4. import com.aliyun.odps.Odps;
  5. import com.aliyun.odps.PartitionSpec;
  6. import com.aliyun.odps.TableSchema;
  7. import com.aliyun.odps.account.Account;
  8. import com.aliyun.odps.account.AliyunAccount;
  9. import com.aliyun.odps.data.Record;
  10. import com.aliyun.odps.data.RecordReader;
  11. import com.aliyun.odps.tunnel.TableTunnel;
  12. import com.aliyun.odps.tunnel.TableTunnel.DownloadSession;
  13. import com.aliyun.odps.tunnel.TunnelException;
  14. public class DownloadSample {
  15. private static String accessId = "<your access id>";
  16. private static String accessKey = "<your access Key>";
  17. private static String odpsUrl = "http://service.odps.aliyun.com/api";
  18. private static String tunnelUrl = "http://dt.cn-shanghai.maxcompute.aliyun-inc.com";
  19. //设置tunnelUrl,若需要走内网时必须设置,否则默认公网。此处给的是华东2经典网络Tunnel Endpoint,其他region可以参考文档《访问域名和数据中心》。
  20. private static String project = "<your project>";
  21. private static String table = "<your table name>";
  22. private static String partition = "<your partition spec>";
  23. public static void main(String args[]) {
  24. Account account = new AliyunAccount(accessId, accessKey);
  25. Odps odps = new Odps(account);
  26. odps.setEndpoint(odpsUrl);
  27. odps.setDefaultProject(project);
  28. TableTunnel tunnel = new TableTunnel(odps);
  29. tunnel.setEndpoint(tunnelUrl);//tunnelUrl设置
  30. PartitionSpec partitionSpec = new PartitionSpec(partition);
  31. try {
  32. DownloadSession downloadSession = tunnel.createDownloadSession(project, table,
  33. partitionSpec);
  34. System.out.println("Session Status is : "
  35. + downloadSession.getStatus().toString());
  36. long count = downloadSession.getRecordCount();
  37. System.out.println("RecordCount is: " + count);
  38. RecordReader recordReader = downloadSession.openRecordReader(0,
  39. count);
  40. Record record;
  41. while ((record = recordReader.read()) != null) {
  42. consumeRecord(record, downloadSession.getSchema());
  43. }
  44. recordReader.close();
  45. } catch (TunnelException e) {
  46. e.printStackTrace();
  47. } catch (IOException e1) {
  48. e1.printStackTrace();
  49. }
  50. }
  51. private static void consumeRecord(Record record, TableSchema schema) {
  52. for (int i = 0; i < schema.getColumns().size(); i++) {
  53. Column column = schema.getColumn(i);
  54. String colValue = null;
  55. switch (column.getType()) {
  56. case BIGINT: {
  57. Long v = record.getBigint(i);
  58. colValue = v == null ? null : v.toString();
  59. break;
  60. }
  61. case BOOLEAN: {
  62. Boolean v = record.getBoolean(i);
  63. colValue = v == null ? null : v.toString();
  64. break;
  65. }
  66. case DATETIME: {
  67. Date v = record.getDatetime(i);
  68. colValue = v == null ? null : v.toString();
  69. break;
  70. }
  71. case DOUBLE: {
  72. Double v = record.getDouble(i);
  73. colValue = v == null ? null : v.toString();
  74. break;
  75. }
  76. case STRING: {
  77. String v = record.getString(i);
  78. colValue = v == null ? null : v.toString();
  79. break;
  80. }
  81. default:
  82. throw new RuntimeException("Unknown column type: "
  83. + column.getType());
  84. }
  85. System.out.print(colValue == null ? "null" : colValue);
  86. if (i != schema.getColumns().size())
  87. System.out.print("\t");
  88. }
  89. System.out.println();
  90. }
  91. }

本示例中,为了方便测试,数据通过 System.out.println 直接打印出来,在实际使用时,您可改写为直接输出到文本文件。

本文导读目录