Project: Apache CarbonData (App Definition and Development / Database)
File: carbon-as-spark-datasource-guide.md

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> The CarbonData fileformat is now integrated as Spark datasource for read and write operation without using CarbonSession. This is useful for users who wants to use carbondata as spark's data source. Note: You can only apply the functions/features supported by spark datasource APIs, functionalities supported would be similar to Parquet. The carbon session features are not supported. The result is displayed as byte array format when select query on binary column in spark-sql. Now you can create Carbon table using Spark's datasource DDL syntax. ``` CREATE [TEMPORARY] TABLE [IF NOT EXISTS] [dbname.]tablename [(colname1 coltype1 [COMMENT col_comment1], ...)] USING CARBON [OPTIONS (key1=val1, key2=val2, ...)] [PARTITIONED BY (colname1, colname2, ...)] [CLUSTERED BY (colname3, colname4, ...) INTO num_buckets BUCKETS] [LOCATION path] [COMMENT table_comment] [TBLPROPERTIES (key1=val1, key2=val2, ...)] [AS select_statement] ``` | Property | Default Value | Description | |--|--|| | table_blocksize | 1024 | Size of blocks to write onto hdfs. For more details, see . | | tableblockletsize | 64 | Size of blocklet to write. | | tablepagesize_inmb | 0 | Size of each page in carbon table, if page size crosses this value before 32000 rows, page will be cut to that many rows. Helps in keep page size to fit cache size | | localdictionarythreshold | 10000 | Cardinality upto which the local dictionary can be generated. For more details, see . | | localdictionaryenable | false | Enable local dictionary generation. For more details, see . | | sort_columns | all dimensions are sorted | Columns to include in sort and its order of sort. For more details, see . | | sortscope | localsort | Sort scope of the load.Options include no sort, local sort, batch sort, and global sort. For more details, see . | | longstringcolumns | null | Comma separated string/char/varchar columns which are more than 32k length. For more details, see . | NOTE: please set longstringcolumns for varchar

``` CREATE TABLE CARBONTABLE (NAME STRING) USING CARBON OPTIONS('tableblocksize'='256') ``` Carbon format can be used in dataframe also. Following are the ways to use carbon format in dataframe. Write carbon using dataframe ``` df.write.format("carbon").save(path) ``` Read carbon using dataframe ``` val df = spark.read.format("carbon").load(path) ``` ``` import org.apache.spark.sql.SparkSession val spark = SparkSession .builder() .appName("Spark SQL basic example") .config("spark.some.config.option", "some-value") .getOrCreate() // For implicit conversions like converting RDDs to DataFrames import spark.implicits._ val df = spark.sparkContext.parallelize(1 to 10 10 1000) .map(x => (r.nextInt(100000), "name" + x % 8, "city" + x % 50, BigDecimal.apply(x % 60))) .toDF("ID", "name", "city", "age") // Write to carbon format df.write.format("carbon").save("/user/person_table") // Read carbon using dataframe val dfread = spark.read.format("carbon").load("/user/person_table") dfread.show() ``` In addition to the above , following properties are supported using dataframe. | Property | Default Value | Description | |--|-|--| | bucket_number | NA | Number of buckets to be created. For more details, see . | | bucket_columns | NA | Columns which are to be placed in buckets. For more details, see . | | streaming | false | Whether the table is a streaming table. For more details, see . | | timestampformat | yyyy-MM-dd HH:mm:ss | For specifying the format of TIMESTAMP data type column. For more details, see . | | dateformat | yyyy-MM-dd | For specifying the format of DATE data type column. For more details, see . | | SPATIALINDEX | NA | Used to configure Spatial Index name. This name is appended to `SPATIALINDEX` in the subsequent sub-property configurations. `xxx` in the below sub-properties refer to index name. Generated spatial index column is not allowed in any properties except in `SORT_COLUMNS` table property.For more details, see . | | SPATIAL_INDEX.xxx.type | NA | Type of algorithm for processing spatial data. Currently, supports 'geohash' and 'geosot'. | | SPATIAL_INDEX.xxx.sourcecolumns | NA | longitude and latitude column names as in the table. These columns are used to generate index value for each row. | | SPATIAL_INDEX.xxx.originLatitude | NA | Latitude of origin. | | SPATIAL_INDEX.xxx.gridSize | NA | Grid size of raster data in metres. Currently, spatial index supports raster data. | | SPATIAL_INDEX.xxx.conversionRatio | NA | Conversion factor. It allows user to translate longitude and latitude to long. For example, if the data to load is longitude = 13.123456, latitude = 101.12356. User can configure conversion ratio sub-property value as 1000000, and change data to load as longitude = 13123456 and latitude = 10112356. Operations on long is much faster compared to floating-point numbers. | | SPATIAL_INDEX.xxx.class | NA | Optional user custom implementation class. Value is fully qualified class name. | Reference :