|
| 1 | +/** |
| 2 | + * Created by zhenhong.gzh on 16/7/13. |
| 3 | + */ |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import com.aliyun.odps.Odps; |
| 13 | +import com.aliyun.odps.PartitionSpec; |
| 14 | +import com.aliyun.odps.TableSchema; |
| 15 | +import com.aliyun.odps.account.Account; |
| 16 | +import com.aliyun.odps.account.AliyunAccount; |
| 17 | +import com.aliyun.odps.data.ArrayRecord; |
| 18 | +import com.aliyun.odps.data.RecordReader; |
| 19 | +import com.aliyun.odps.data.RecordWriter; |
| 20 | +import com.aliyun.odps.data.SimpleStruct; |
| 21 | +import com.aliyun.odps.data.Struct; |
| 22 | +import com.aliyun.odps.tunnel.TableTunnel; |
| 23 | +import com.aliyun.odps.tunnel.TableTunnel.UploadSession; |
| 24 | +import com.aliyun.odps.tunnel.TableTunnel.DownloadSession; |
| 25 | +import com.aliyun.odps.tunnel.TunnelException; |
| 26 | +import com.aliyun.odps.type.StructTypeInfo; |
| 27 | + |
| 28 | +public class TunnelComplexTypeSample { |
| 29 | + |
| 30 | + private static String accessId = "<your access id>"; |
| 31 | + private static String accessKey = "<your access Key>"; |
| 32 | + private static String odpsUrl = "<your odps endpoint>"; |
| 33 | + private static String project = "<your project>"; |
| 34 | + |
| 35 | + private static String table = "<your table name>"; |
| 36 | + |
| 37 | + // partitions of a partitioned table, eg: "pt=\'1\',ds=\'2\'" |
| 38 | + // if the table is not a partitioned table, do not need it |
| 39 | + private static String partition = "<your partition spec>"; |
| 40 | + |
| 41 | + public static void main(String args[]) { |
| 42 | + Account account = new AliyunAccount(accessId, accessKey); |
| 43 | + Odps odps = new Odps(account); |
| 44 | + odps.setEndpoint(odpsUrl); |
| 45 | + odps.setDefaultProject(project); |
| 46 | + |
| 47 | + try { |
| 48 | + TableTunnel tunnel = new TableTunnel(odps); |
| 49 | + PartitionSpec partitionSpec = new PartitionSpec(partition); |
| 50 | + |
| 51 | + // ---------- Upload Data --------------- |
| 52 | + // create upload session for table |
| 53 | + // the table schema is {"col0": ARRAY<BIGINT>, "col1": MAP<STRING, BIGINT>, "col2": STRUCT<name:STRING,age:BIGINT>} |
| 54 | + UploadSession uploadSession = tunnel.createUploadSession(project, table, partitionSpec); |
| 55 | + // get table schema |
| 56 | + TableSchema schema = uploadSession.getSchema(); |
| 57 | + |
| 58 | + // open record writer |
| 59 | + RecordWriter recordWriter = uploadSession.openRecordWriter(0); |
| 60 | + ArrayRecord record = (ArrayRecord) uploadSession.newRecord(); |
| 61 | + |
| 62 | + // prepare data |
| 63 | + List arrayData = Arrays.asList(1, 2, 3); |
| 64 | + Map<String, Long> mapData = new HashMap<String, Long>(); |
| 65 | + mapData.put("a", 1L); |
| 66 | + mapData.put("c", 2L); |
| 67 | + |
| 68 | + List<Object> structData = new ArrayList<Object>(); |
| 69 | + structData.add("Lily"); |
| 70 | + structData.add(18); |
| 71 | + |
| 72 | + // set data to record |
| 73 | + record.setArray(0, arrayData); |
| 74 | + record.setMap(1, mapData); |
| 75 | + record.setStruct(2, new SimpleStruct((StructTypeInfo) schema.getColumn(2).getTypeInfo(), |
| 76 | + structData)); |
| 77 | + |
| 78 | + // write the record |
| 79 | + recordWriter.write(record); |
| 80 | + |
| 81 | + // close writer |
| 82 | + recordWriter.close(); |
| 83 | + |
| 84 | + // commit uploadSession, the upload finish |
| 85 | + uploadSession.commit(new Long[]{0L}); |
| 86 | + System.out.println("upload success!"); |
| 87 | + |
| 88 | + // ---------- Download Data --------------- |
| 89 | + // create download session for table |
| 90 | + // the table schema is {"col0": ARRAY<BIGINT>, "col1": MAP<STRING, BIGINT>, "col2": STRUCT<name:STRING,age:BIGINT>} |
| 91 | + DownloadSession downloadSession = tunnel.createDownloadSession(project, table, partitionSpec); |
| 92 | + schema = downloadSession.getSchema(); |
| 93 | + |
| 94 | + // open record reader, read one record here for example |
| 95 | + RecordReader recordReader = downloadSession.openRecordReader(0, 1); |
| 96 | + |
| 97 | + // read the record |
| 98 | + ArrayRecord record1 = (ArrayRecord)recordReader.read(); |
| 99 | + |
| 100 | + // get array field data |
| 101 | + List field0 = record1.getArray(0); |
| 102 | + List<Long> longField0 = record1.getArray(Long.class, 0); |
| 103 | + |
| 104 | + // get map field data |
| 105 | + Map field1 = record1.getMap(1); |
| 106 | + Map<String, Long> typedField1 = record1.getMap(String.class, Long.class, 1); |
| 107 | + |
| 108 | + // get struct field data |
| 109 | + Struct field2 = record1.getStruct(2); |
| 110 | + |
| 111 | + System.out.println("download success!"); |
| 112 | + } catch (TunnelException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + } catch (IOException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments