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));
}
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"));
}
}
}
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);
}
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;
}
}
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();
}
Aggregations