Search in sources :

Example 11 with InvalidLoadOptionException

use of org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException in project carbondata by apache.

the class CarbonReaderTest method testWriteAndReadFilesWithReaderBuildFail.

@Test
public void testWriteAndReadFilesWithReaderBuildFail() throws IOException, InterruptedException {
    String path1 = "./testWriteFiles";
    String path2 = "./testWriteFiles2";
    FileUtils.deleteDirectory(new File(path1));
    FileUtils.deleteDirectory(new File(path2));
    IndexStoreManager.getInstance().clearIndexCache(AbsoluteTableIdentifier.from(path1), false);
    IndexStoreManager.getInstance().clearIndexCache(AbsoluteTableIdentifier.from(path2), false);
    Field[] fields = new Field[] { new Field("c1", "string"), new Field("c2", "int") };
    Schema schema = new Schema(fields);
    CarbonWriterBuilder builder = CarbonWriter.builder();
    CarbonWriter carbonWriter = null;
    try {
        carbonWriter = builder.outputPath(path1).uniqueIdentifier(12345).withCsvInput(schema).writtenBy("CarbonReaderTest").build();
    } catch (InvalidLoadOptionException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    carbonWriter.write(new String[] { "MNO", "100" });
    carbonWriter.close();
    Field[] fields1 = new Field[] { new Field("p1", "string"), new Field("p2", "int") };
    Schema schema1 = new Schema(fields1);
    CarbonWriterBuilder builder1 = CarbonWriter.builder();
    CarbonWriter carbonWriter1 = null;
    try {
        carbonWriter1 = builder1.outputPath(path2).uniqueIdentifier(12345).withCsvInput(schema1).writtenBy("CarbonReaderTest").build();
    } catch (InvalidLoadOptionException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    carbonWriter1.write(new String[] { "PQR", "200" });
    carbonWriter1.close();
    try {
        CarbonReader reader = CarbonReader.builder(path1, "_temp").projection(new String[] { "c1", "c3" }).build();
        Assert.fail();
    } catch (Exception e) {
        System.out.println("Success");
        Assert.assertTrue(true);
    }
    CarbonReader reader1 = CarbonReader.builder(path2, "_temp1").projection(new String[] { "p1", "p2" }).build();
    while (reader1.hasNext()) {
        Object[] row1 = (Object[]) reader1.readNextRow();
        System.out.println(row1[0]);
        System.out.println(row1[1]);
    }
    reader1.close();
    FileUtils.deleteDirectory(new File(path1));
    FileUtils.deleteDirectory(new File(path2));
}
Also used : InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException) Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) File(java.io.File) IOException(java.io.IOException) InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException) Test(org.junit.Test)

Example 12 with InvalidLoadOptionException

use of org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException in project carbondata by apache.

the class ImageTest method testWriteWithNull.

@Test
public void testWriteWithNull() throws IOException, InvalidLoadOptionException {
    String imagePath = "./src/test/resources/image/carbondatalogo.jpg";
    int num = 1;
    int rows = 10;
    String path = "./target/binary";
    try {
        FileUtils.deleteDirectory(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Field[] fields = new Field[5];
    fields[0] = new Field("name", DataTypes.STRING);
    fields[1] = new Field("age", DataTypes.INT);
    fields[2] = new Field("image1", DataTypes.BINARY);
    fields[3] = new Field("image2", DataTypes.BINARY);
    fields[4] = new Field("image3", DataTypes.BINARY);
    byte[] originBinary = null;
    // read and write image data
    for (int j = 0; j < num; j++) {
        CarbonWriter writer = CarbonWriter.builder().outputPath(path).withCsvInput(new Schema(fields)).writtenBy("SDKS3Example").withPageSizeInMb(1).withLoadOption("bad_records_action", "force").build();
        for (int i = 0; i < rows; i++) {
            // read image and encode to Hex
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imagePath));
            originBinary = new byte[bis.available()];
            while ((bis.read(originBinary)) != -1) {
            }
            // write data
            writer.write(new Object[] { "robot" + (i % 10), i, originBinary, originBinary, 1 });
            bis.close();
        }
        try {
            writer.close();
        } catch (Exception e) {
            assert (e.getMessage().contains("Binary only support String and byte[] data type"));
        }
    }
}
Also used : Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) DecoderException(org.apache.commons.codec.DecoderException) IOException(java.io.IOException) InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException) Test(org.junit.Test)

Example 13 with InvalidLoadOptionException

use of org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException in project carbondata by apache.

the class CarbonReaderTest method testReadMapType.

@Test
public void testReadMapType() throws IOException, InterruptedException {
    String path = "./testWriteFiles";
    FileUtils.deleteDirectory(new File(path));
    String mySchema = "{ " + "  \"name\": \"address\", " + "  \"type\": \"record\", " + "  \"fields\": [ " + "    { " + "      \"name\": \"name\", " + "      \"type\": \"string\" " + "    }, " + "    { " + "      \"name\": \"age\", " + "      \"type\": \"int\" " + "    }, " + "    { " + "      \"name\": \"mapRecord\", " + "      \"type\": { " + "        \"type\": \"map\", " + "        \"values\": \"string\" " + "      } " + "    } " + "  ] " + "} ";
    String json = "{\"name\":\"bob\", \"age\":10, \"mapRecord\": {\"street\": \"k-lane\", \"city\": \"bangalore\"}}";
    try {
        WriteAvroComplexData(mySchema, json, path);
    } catch (InvalidLoadOptionException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    Field[] fields = new Field[3];
    fields[0] = new Field("name", DataTypes.STRING);
    fields[1] = new Field("age", DataTypes.INT);
    fields[2] = new Field("mapRecord", DataTypes.createMapType(DataTypes.STRING, DataTypes.STRING));
    CarbonReader reader = CarbonReader.builder(path, "_temp").build();
    // expected output
    String name = "bob";
    int age = 10;
    Object[] mapKeValue = new Object[2];
    mapKeValue[0] = new Object[] { "city", "street" };
    mapKeValue[1] = new Object[] { "bangalore", "k-lane" };
    int i = 0;
    while (reader.hasNext()) {
        Object[] row = (Object[]) reader.readNextRow();
        Assert.assertEquals(name, row[0]);
        Assert.assertArrayEquals(mapKeValue, (Object[]) row[1]);
        Assert.assertEquals(age, row[2]);
        i++;
    }
    reader.close();
    Assert.assertEquals(i, 100);
}
Also used : InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException) Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) File(java.io.File) Test(org.junit.Test)

Example 14 with InvalidLoadOptionException

use of org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException in project carbondata by apache.

the class AvroCarbonWriterTest method WriteAvroComplexData.

private void WriteAvroComplexData(String mySchema, String json, String[] sortColumns) throws IOException, InvalidLoadOptionException {
    // conversion to GenericData.Record
    Schema nn = new Schema.Parser().parse(mySchema);
    GenericData.Record record = TestUtil.jsonToAvro(json, mySchema);
    try {
        CarbonWriter writer = CarbonWriter.builder().outputPath(path).sortBy(sortColumns).withAvroInput(nn).writtenBy("AvroCarbonWriterTest").build();
        for (int i = 0; i < 100; i++) {
            writer.write(record);
        }
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : Schema(org.apache.avro.Schema) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema) GenericData(org.apache.avro.generic.GenericData) IOException(java.io.IOException) InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException)

Example 15 with InvalidLoadOptionException

use of org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException in project carbondata by apache.

the class CSVCarbonWriterTest method testWrongSchemaFieldsValidation.

@Test
public void testWrongSchemaFieldsValidation() throws IOException {
    CarbonWriter carbonWriter = null;
    String path = "./testWriteFiles";
    FileUtils.deleteDirectory(new File(path));
    // supply 3 size fields but actual Field array value given is 2
    Field[] fields = new Field[3];
    fields[0] = new Field("name", DataTypes.STRING);
    fields[1] = new Field("age", DataTypes.INT);
    try {
        carbonWriter = CarbonWriter.builder().outputPath(path).withCsvInput(new Schema(fields)).writtenBy("CSVCarbonWriterTest").build();
    } catch (InvalidLoadOptionException e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
    carbonWriter.write(new String[] { "babu", "1" });
    carbonWriter.close();
}
Also used : InvalidLoadOptionException(org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException) Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema) CarbonFile(org.apache.carbondata.core.datastore.filesystem.CarbonFile) File(java.io.File) Test(org.junit.Test)

Aggregations

InvalidLoadOptionException (org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException)18 File (java.io.File)10 IOException (java.io.IOException)10 Field (org.apache.carbondata.core.metadata.datatype.Field)8 Test (org.junit.Test)8 StructField (org.apache.carbondata.core.metadata.datatype.StructField)6 ColumnSchema (org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema)4 HashMap (java.util.HashMap)3 BufferedInputStream (java.io.BufferedInputStream)2 FileFilter (java.io.FileFilter)2 FileInputStream (java.io.FileInputStream)2 Schema (org.apache.avro.Schema)2 CarbonFile (org.apache.carbondata.core.datastore.filesystem.CarbonFile)2 CarbonColumn (org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn)2 DataLoadMetrics (org.apache.carbondata.core.util.DataLoadMetrics)2 DecoderException (org.apache.commons.codec.DecoderException)2 FilenameFilter (java.io.FilenameFilter)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1