阅读743 返回首页    go 微信


简单下载示例__SDK示例_批量数据通道_大数据计算服务-阿里云

  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 = "https://service.odps.aliyun.com/api";
  18. private static String project = "<your project>";
  19. private static String table = "<your table name>";
  20. private static String partition = "<your partition spec>";
  21. public static void main(String args[]) {
  22. Account account = new AliyunAccount(accessId, accessKey);
  23. Odps odps = new Odps(account);
  24. odps.setEndpoint(odpsUrl);
  25. odps.setDefaultProject(project);
  26. TableTunnel tunnel = new TableTunnel(odps);
  27. PartitionSpec partitionSpec = new PartitionSpec(partition);
  28. try {
  29. DownloadSession downloadSession = tunnel.createDownloadSession(project, table,
  30. partitionSpec);
  31. System.out.println("Session Status is : "
  32. + downloadSession.getStatus().toString());
  33. long count = downloadSession.getRecordCount();
  34. System.out.println("RecordCount is: " + count);
  35. RecordReader recordReader = downloadSession.openRecordReader(0,
  36. count);
  37. Record record;
  38. while ((record = recordReader.read()) != null) {
  39. consumeRecord(record, downloadSession.getSchema());
  40. }
  41. recordReader.close();
  42. } catch (TunnelException e) {
  43. e.printStackTrace();
  44. } catch (IOException e1) {
  45. e1.printStackTrace();
  46. }
  47. }
  48. private static void consumeRecord(Record record, TableSchema schema) {
  49. for (int i = 0; i < schema.getColumns().size(); i++) {
  50. Column column = schema.getColumn(i);
  51. String colValue = null;
  52. switch (column.getType()) {
  53. case BIGINT: {
  54. Long v = record.getBigint(i);
  55. colValue = v == null ? null : v.toString();
  56. break;
  57. }
  58. case BOOLEAN: {
  59. Boolean v = record.getBoolean(i);
  60. colValue = v == null ? null : v.toString();
  61. break;
  62. }
  63. case DATETIME: {
  64. Date v = record.getDatetime(i);
  65. colValue = v == null ? null : v.toString();
  66. break;
  67. }
  68. case DOUBLE: {
  69. Double v = record.getDouble(i);
  70. colValue = v == null ? null : v.toString();
  71. break;
  72. }
  73. case STRING: {
  74. String v = record.getString(i);
  75. colValue = v == null ? null : v.toString();
  76. break;
  77. }
  78. default:
  79. throw new RuntimeException("Unknown column type: "
  80. + column.getType());
  81. }
  82. System.out.print(colValue == null ? "null" : colValue);
  83. if (i != schema.getColumns().size())
  84. System.out.print("t");
  85. }
  86. System.out.println();
  87. }
  88. }

最后更新:2016-07-12 15:31:25

  上一篇:go 简单上传示例__SDK示例_批量数据通道_大数据计算服务-阿里云
  下一篇:go 多线程上传示例__SDK示例_批量数据通道_大数据计算服务-阿里云