99import org .junit .Test ;
1010
1111import com .aliyun .odps .Column ;
12+ import com .aliyun .odps .Instance ;
13+ import com .aliyun .odps .Odps ;
1214import com .aliyun .odps .TableSchema ;
15+ import com .aliyun .odps .commons .transport .OdpsTestUtils ;
16+ import com .aliyun .odps .task .SQLTask ;
17+ import com .aliyun .odps .tunnel .TableTunnel ;
1318import com .aliyun .odps .type .ArrayTypeInfo ;
1419import com .aliyun .odps .type .MapTypeInfo ;
1520import com .aliyun .odps .type .StructTypeInfo ;
1621import com .aliyun .odps .type .TypeInfo ;
1722import com .aliyun .odps .type .TypeInfoFactory ;
23+ import com .google .common .collect .ImmutableMap ;
1824
1925/**
2026 * @author dingxin ([email protected] ) 2127 */
2228public class ReorderableRecordTest {
29+ @ Test
30+ public void testE2E () throws Exception {
31+ Odps odps = OdpsTestUtils .newDefaultOdps ();
32+ String tableName = "test_ReorderableRecordTest" ;
33+ odps .tables ().delete (tableName , true );
34+
35+ TableSchema schema = TableSchema .builder ()
36+ .withStringColumn ("c1" )
37+ .withBigintColumn ("c2" )
38+ .withColumn (new Column ("c3" , getOrderedStructType ())).build ();
39+ odps .tables ().newTableCreator (tableName , schema ).withLifeCycle (1L )
40+ .withHints (ImmutableMap .of ("odps.sql.type.system.odps2" , "true" )).ifNotExists ().create ();
41+
42+ TableTunnel .StreamUploadSession session = odps .tableTunnel ()
43+ .buildStreamUploadSession (odps .getDefaultProject (), tableName ).build ();
44+
45+ Record record = new ReorderableRecord (session .getSchema ());
46+ Struct misOrderedStruct = getMisOrderedStruct ();
47+ record .set ("C3" , misOrderedStruct );
48+ record .set ("C2" , 123L );
49+ record .set ("C1" , "test" );
50+
51+ TableTunnel .StreamRecordPack recordPack = session .newRecordPack ();
52+ for (int i = 0 ; i < 10 ; i ++) {
53+ recordPack .append (record );
54+ }
55+ recordPack .flush ();
56+
57+ Instance instance = SQLTask .run (odps , "select c3 from " + tableName + ";" );
58+ System .out .println (instance .waitForTerminatedAndGetResult ().getString ());
59+ }
60+
2361
2462 @ Test
2563 public void testSetStruct () {
@@ -141,33 +179,30 @@ private Struct getMisOrderedStruct() {
141179 List <TypeInfo > personFieldTypes = new ArrayList <>();
142180 List <String > personFieldNames = new ArrayList <>();
143181
144- // Person 结构体字段:name (String), age (Int)
182+ // Person 结构体字段:name (String), age (Int)
145183 personFieldTypes .add (TypeInfoFactory .BIGINT ); // money
146184 personFieldTypes .add (TypeInfoFactory .INT ); // age
147185 personFieldTypes .add (TypeInfoFactory .STRING ); // name
148186 personFieldNames .add ("money" );
149187 personFieldNames .add ("age" );
150188 personFieldNames .add ("name" );
151189
152- StructTypeInfo
153- personStructType =
190+ StructTypeInfo personStructType =
154191 TypeInfoFactory .getStructTypeInfo (personFieldNames , personFieldTypes );
192+ ReorderableStruct person = new ReorderableStruct (personStructType );
193+ person .setFieldValue ("money" , 1234L );
194+ person .setFieldValue ("age" , 25 );
195+ person .setFieldValue ("name" , "Jason" );
155196
156- List <Object > value = new ArrayList <>();
157- value .add (1234L );
158- value .add (25 );
159- value .add ("Jason" );
160- Struct person = new SimpleStruct (personStructType , value );
161-
162- // 2. 创建 Array 类型(元素类型为 Person Struct)
197+ // 2. 创建 Array 类型(元素类型为 Person Struct)
163198 ArrayTypeInfo personArrayType = TypeInfoFactory .getArrayTypeInfo (personStructType );
164199
165200 List <Struct > personArray = new ArrayList <>();
166201 personArray .add (person );
167202 personArray .add (person );
168203 personArray .add (person );
169204
170- // 3. 创建 Map 类型(键为 String,值为 Person Array)
205+ // 3. 创建 Map 类型(键为 String,值为 Person Array)
171206 MapTypeInfo personMapType = TypeInfoFactory .getMapTypeInfo (
172207 TypeInfoFactory .STRING , // key type: String
173208 personArrayType // value type: Array<Person>
@@ -178,11 +213,11 @@ private Struct getMisOrderedStruct() {
178213 personMap .put ("key2" , personArray );
179214 personMap .put ("key3" , personArray );
180215
181- // 4. 创建外层 Struct 类型(包含 Map 和 Array 的嵌套)
216+ // 4. 创建外层 Struct 类型(包含 Map 和 Array 的嵌套)
182217 List <TypeInfo > outerFieldTypes = new ArrayList <>();
183218 List <String > outerFieldNames = new ArrayList <>();
184219
185- // 外层结构体字段:config (Array<Person>) metadata (Map<String, Array<Person>>),
220+ // 外层结构体字段:config (Array<Person>) metadata (Map<String, Array<Person>>),
186221 outerFieldTypes .add (personArrayType ); // config
187222 outerFieldTypes .add (personMapType ); // metadata
188223 outerFieldNames .add ("config" );
0 commit comments