You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@carbondata.apache.org by ja...@apache.org on 2018/11/05 02:11:03 UTC

carbondata git commit: [CARBONDATA-3071] Add CarbonSession Java Example

Repository: carbondata
Updated Branches:
  refs/heads/master 0798c0036 -> 5a0bc6e71


[CARBONDATA-3071] Add CarbonSession Java Example

This closes #2810


Project: http://git-wip-us.apache.org/repos/asf/carbondata/repo
Commit: http://git-wip-us.apache.org/repos/asf/carbondata/commit/5a0bc6e7
Tree: http://git-wip-us.apache.org/repos/asf/carbondata/tree/5a0bc6e7
Diff: http://git-wip-us.apache.org/repos/asf/carbondata/diff/5a0bc6e7

Branch: refs/heads/master
Commit: 5a0bc6e71484b3956242b1cdf2882c674094548c
Parents: 0798c00
Author: Li Jiang <tu...@gmail.com>
Authored: Thu Oct 11 13:36:08 2018 +0800
Committer: Jacky Li <ja...@qq.com>
Committed: Mon Nov 5 10:10:45 2018 +0800

----------------------------------------------------------------------
 .../examples/sql/CarbonSessionExample.java      | 137 +++++++++++++++++++
 1 file changed, 137 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/carbondata/blob/5a0bc6e7/examples/spark2/src/main/java/org/apache/carbondata/examples/sql/CarbonSessionExample.java
----------------------------------------------------------------------
diff --git a/examples/spark2/src/main/java/org/apache/carbondata/examples/sql/CarbonSessionExample.java b/examples/spark2/src/main/java/org/apache/carbondata/examples/sql/CarbonSessionExample.java
new file mode 100644
index 0000000..37a12e4
--- /dev/null
+++ b/examples/spark2/src/main/java/org/apache/carbondata/examples/sql/CarbonSessionExample.java
@@ -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());
+    }
+  }
+}