Skip to content

Commit

Permalink
[CARBONDATA-3071] Add CarbonSession Java Example
Browse files Browse the repository at this point in the history
This closes #2810
  • Loading branch information
lijiang2 authored and jackylk committed Nov 5, 2018
1 parent 0798c00 commit 5a0bc6e
Showing 1 changed file with 137 additions and 0 deletions.
@@ -0,0 +1,137 @@
/*
* 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.
*/

package org.apache.carbondata.examples.sql;

import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.util.CarbonProperties;
import org.apache.carbondata.examples.util.ExampleUtils;
import org.apache.log4j.PropertyConfigurator;
import org.apache.spark.sql.SparkSession;

import java.io.File;
import java.io.IOException;

public class CarbonSessionExample {

public static void main(String[] args) {
File file = new File(CarbonSessionExample.class.getResource("/").getPath() + "../..");
try {
String rootPath = file.getCanonicalPath();
System.setProperty("path.target", rootPath + "/examples/spark2/target");
PropertyConfigurator.configure(rootPath + "/examples/spark2/src/main/resources/log4j.properties");
CarbonProperties.getInstance().addProperty(CarbonCommonConstants.ENABLE_QUERY_STATISTICS, "true");
SparkSession carbon = ExampleUtils.createCarbonSession("JavaCarbonSessionExample", 1);
carbon.sparkContext().setLogLevel("INFO");

carbon.sql("DROP TABLE IF EXISTS carbonsession_table");
carbon.sql("DROP TABLE IF EXISTS stored_as_carbondata_table");

carbon.sql(
"CREATE TABLE carbonsession_table( " +
"shortField SHORT, " +
"intField INT, " +
"bigintField LONG, " +
"doubleField DOUBLE, " +
"stringField STRING, " +
"timestampField TIMESTAMP, " +
"decimalField DECIMAL(18,2), " +
"dateField DATE, " +
"charField CHAR(5), " +
"floatField FLOAT " +
") " +
"STORED AS carbondata" +
"TBLPROPERTIES('DICTIONARY_INCLUDE'='dateField, charField')"
);

String path = rootPath + "/examples/spark2/src/main/resources/data.csv";
carbon.sql(
"LOAD DATA LOCAL INPATH " + "\'" + path + "\' " +
"INTO TABLE carbonsession_table " +
"OPTIONS('HEADER'='true', 'COMPLEX_DELIMITER_LEVEL_1'='#')"
);

carbon.sql(
"SELECT charField, stringField, intField " +
"FROM carbonsession_table " +
"WHERE stringfield = 'spark' AND decimalField > 40"
).show();

carbon.sql(
"SELECT * " +
"FROM carbonsession_table WHERE length(stringField) = 5"
).show();

carbon.sql(
"SELECT * " +
"FROM carbonsession_table " +
"WHERE date_format(dateField, \'yyyy-MM-dd \') = \'2015-07-23\'"
).show();

carbon.sql("SELECT count(stringField) FROM carbonsession_table").show();

carbon.sql(
"SELECT sum(intField), stringField " +
"FROM carbonsession_table " +
"GROUP BY stringField"
).show();

carbon.sql(
"SELECT t1.*, t2.* " +
"FROM carbonsession_table t1, carbonsession_table t2 " +
"WHERE t1.stringField = t2.stringField"
).show();

carbon.sql(
"WITH t1 AS ( " +
"SELECT * FROM carbonsession_table " +
"UNION ALL " +
"SELECT * FROM carbonsession_table" +
") " +
"SELECT t1.*, t2.* " +
"FROM t1, carbonsession_table t2 " +
"WHERE t1.stringField = t2.stringField"
).show();

carbon.sql(
"SELECT * " +
"FROM carbonsession_table " +
"WHERE stringField = 'spark' and floatField > 2.8"
).show();

carbon.sql(
"CREATE TABLE stored_as_carbondata_table( " +
"name STRING, " +
"age INT" +
") " +
"STORED AS carbondata"
);

carbon.sql("INSERT INTO stored_as_carbondata_table VALUES ('Bob',28) ");
carbon.sql("SELECT * FROM stored_as_carbondata_table").show();

carbon.sql("DROP TABLE IF EXISTS carbonsession_table");
carbon.sql("DROP TABLE IF EXISTS stored_as_carbondata_table");

carbon.close();
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}

0 comments on commit 5a0bc6e

Please sign in to comment.