Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.sql.avro;
0019 
0020 import org.junit.After;
0021 import org.junit.Before;
0022 import org.junit.Test;
0023 
0024 import org.apache.spark.sql.Dataset;
0025 import org.apache.spark.sql.QueryTest$;
0026 import org.apache.spark.sql.Row;
0027 import org.apache.spark.sql.test.TestSparkSession;
0028 
0029 import static org.apache.spark.sql.avro.functions.to_avro;
0030 import static org.apache.spark.sql.avro.functions.from_avro;
0031 
0032 
0033 public class JavaAvroFunctionsSuite {
0034   private transient TestSparkSession spark;
0035 
0036   @Before
0037   public void setUp() {
0038     spark = new TestSparkSession();
0039   }
0040 
0041   @After
0042   public void tearDown() {
0043     spark.stop();
0044   }
0045 
0046   @Test
0047   public void testToAvroFromAvro() {
0048     Dataset<Long> rangeDf = spark.range(10);
0049     Dataset<Row> df = rangeDf.select(
0050       rangeDf.col("id"), rangeDf.col("id").cast("string").as("str"));
0051 
0052     Dataset<Row> avroDF =
0053       df.select(
0054         to_avro(df.col("id")).as("a"),
0055         to_avro(df.col("str")).as("b"));
0056 
0057     String avroTypeLong = "{\"type\": \"int\", \"name\": \"id\"}";
0058     String avroTypeStr = "{\"type\": \"string\", \"name\": \"str\"}";
0059 
0060     Dataset<Row> actual = avroDF.select(
0061       from_avro(avroDF.col("a"), avroTypeLong),
0062       from_avro(avroDF.col("b"), avroTypeStr));
0063 
0064     QueryTest$.MODULE$.checkAnswer(actual, df.collectAsList());
0065   }
0066 }