You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ma...@apache.org on 2016/12/27 18:30:07 UTC

[44/50] [abbrv] phoenix git commit: PHOENIX-3532 Pass tenantId parameter to PhoenixRDD when reading (Nico Pappagianis)

PHOENIX-3532 Pass tenantId parameter to PhoenixRDD when reading (Nico Pappagianis)


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

Branch: refs/heads/calcite
Commit: e45b5a706107e31bc6e5b8289725db097b2820eb
Parents: 8c636a4
Author: Josh Mahonin <jm...@gmail.com>
Authored: Mon Dec 19 11:15:15 2016 -0500
Committer: Josh Mahonin <jm...@gmail.com>
Committed: Mon Dec 19 11:15:15 2016 -0500

----------------------------------------------------------------------
 phoenix-spark/src/it/resources/tenantSetup.sql  |  1 +
 .../phoenix/spark/AbstractPhoenixSparkIT.scala  |  5 -
 .../spark/PhoenixSparkITTenantSpecific.scala    | 99 +++++++++++++-------
 .../org/apache/phoenix/spark/PhoenixRDD.scala   | 16 +++-
 .../phoenix/spark/SparkContextFunctions.scala   |  4 +-
 .../spark/SparkSqlContextFunctions.scala        |  6 +-
 6 files changed, 85 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/it/resources/tenantSetup.sql
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/it/resources/tenantSetup.sql b/phoenix-spark/src/it/resources/tenantSetup.sql
index 4a866dc..f62d843 100644
--- a/phoenix-spark/src/it/resources/tenantSetup.sql
+++ b/phoenix-spark/src/it/resources/tenantSetup.sql
@@ -15,3 +15,4 @@
 -- limitations under the License.
 
 CREATE VIEW IF NOT EXISTS TENANT_VIEW(TENANT_ONLY_COL VARCHAR) AS SELECT * FROM MULTITENANT_TEST_TABLE
+UPSERT INTO TENANT_VIEW (ORGANIZATION_ID, TENANT_ONLY_COL) VALUES ('defaultOrg', 'defaultData')

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala b/phoenix-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala
index f81438f..ecaedc7 100644
--- a/phoenix-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala
+++ b/phoenix-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala
@@ -46,11 +46,6 @@ class AbstractPhoenixSparkIT extends FunSuite with Matchers with BeforeAndAfterA
   // A global tenantId we can use across tests
   final val TenantId = "theTenant"
 
-  // TENANT_VIEW schema
-  val OrgId = "ORGANIZATION_ID"
-  val TenantCol = "TENANT_ONLY_COL"
-  val ViewName = "TENANT_VIEW"
-
   var conn: Connection = _
   var sc: SparkContext = _
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkITTenantSpecific.scala
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkITTenantSpecific.scala b/phoenix-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkITTenantSpecific.scala
index a1c1e22..77b41af 100644
--- a/phoenix-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkITTenantSpecific.scala
+++ b/phoenix-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkITTenantSpecific.scala
@@ -19,7 +19,10 @@ import org.apache.spark.sql.SQLContext
 import scala.collection.mutable.ListBuffer
 
 /**
-  * Sub-class of PhoenixSparkIT used for tenant-specific test
+  * Sub-class of PhoenixSparkIT used for tenant-specific tests
+  *
+  * Note: All schema related variables (table name, column names, default data, etc) are coupled with
+  * phoenix-spark/src/it/resources/tenantSetup.sql
   *
   * Note: If running directly from an IDE, these are the recommended VM parameters:
   * -Xmx1536m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=512m
@@ -27,10 +30,24 @@ import scala.collection.mutable.ListBuffer
   */
 class PhoenixSparkITTenantSpecific extends AbstractPhoenixSparkIT {
 
-  val SelectStatement = "SELECT " + OrgId + "," + TenantCol + " FROM " + ViewName
-  val DataSet = List(("testOrg1", "data1"), ("testOrg2", "data2"), ("testOrg3", "data3"))
+  // Tenant-specific schema info
+  val OrgIdCol = "ORGANIZATION_ID"
+  val TenantOnlyCol = "TENANT_ONLY_COL"
+  val TenantTable = "TENANT_VIEW"
+
+  // Data set for tests that write to Phoenix
+  val TestDataSet = List(("testOrg1", "data1"), ("testOrg2", "data2"), ("testOrg3", "data3"))
 
+  /**
+    * Helper method used by write tests to verify content written.
+    * Assumes the caller has written the TestDataSet (defined above) to Phoenix
+    * and that 1 row of default data exists (upserted after table creation in tenantSetup.sql)
+    */
   def verifyResults(): Unit = {
+    // Contains the default data upserted into the tenant-specific table in tenantSetup.sql and the data upserted by tests
+    val VerificationDataSet = List(("defaultOrg", "defaultData")) ::: TestDataSet
+
+    val SelectStatement = "SELECT " + OrgIdCol + "," + TenantOnlyCol + " FROM " + TenantTable
     val stmt = conn.createStatement()
     val rs = stmt.executeQuery(SelectStatement)
 
@@ -39,62 +56,78 @@ class PhoenixSparkITTenantSpecific extends AbstractPhoenixSparkIT {
       results.append((rs.getString(1), rs.getString(2)))
     }
     stmt.close()
-
-    results.toList shouldEqual DataSet
+    results.toList shouldEqual VerificationDataSet
   }
 
-  test("Can persist a dataframe using 'DataFrame.saveToPhoenix' on tenant-specific view") {
+  /*****************/
+  /** Read tests **/
+  /*****************/
+
+  test("Can read from tenant-specific table as DataFrame") {
     val sqlContext = new SQLContext(sc)
-    import sqlContext.implicits._
+    val df = sqlContext.phoenixTableAsDataFrame(
+      TenantTable,
+      Seq(OrgIdCol, TenantOnlyCol),
+      zkUrl = Some(quorumAddress),
+      tenantId = Some(TenantId),
+      conf = hbaseConfiguration)
+
+    // There should only be 1 row upserted in tenantSetup.sql
+    val count = df.count()
+    count shouldEqual 1L
+  }
 
-    val dataSet = List(("testOrg1", "data1"), ("testOrg2", "data2"), ("testOrg3", "data3"))
+  test("Can read from tenant-specific table as RDD") {
+    val rdd = sc.phoenixTableAsRDD(
+      TenantTable,
+      Seq(OrgIdCol, TenantOnlyCol),
+      zkUrl = Some(quorumAddress),
+      tenantId = Some(TenantId),
+      conf = hbaseConfiguration)
+
+    // There should only be 1 row upserted in tenantSetup.sql
+    val count = rdd.count()
+    count shouldEqual 1L
+  }
 
-    val df = sc.parallelize(dataSet).toDF(OrgId, TenantCol)
+  /*****************/
+  /** Write tests **/
+  /*****************/
 
-    // Save to tenant-specific view
-    df.saveToPhoenix("TENANT_VIEW", zkUrl = Some(quorumAddress), tenantId = Some(TenantId))
+  test("Can write a DataFrame using 'DataFrame.saveToPhoenix' to tenant-specific view") {
+    val sqlContext = new SQLContext(sc)
+    import sqlContext.implicits._
+
+    val df = sc.parallelize(TestDataSet).toDF(OrgIdCol, TenantOnlyCol)
+    df.saveToPhoenix(TenantTable, zkUrl = Some(quorumAddress), tenantId = Some(TenantId))
 
     verifyResults
   }
 
-  test("Can persist a dataframe using 'DataFrame.write' on tenant-specific view") {
-
+  test("Can write a DataFrame using 'DataFrame.write' to tenant-specific view") {
     val sqlContext = new SQLContext(sc)
     import sqlContext.implicits._
 
-    val dataSet = List(("testOrg1", "data1"), ("testOrg2", "data2"), ("testOrg3", "data3"))
-
-    val df = sc.parallelize(dataSet).toDF(OrgId, TenantCol)
+    val df = sc.parallelize(TestDataSet).toDF(OrgIdCol, TenantOnlyCol)
 
     df.write
       .format("org.apache.phoenix.spark")
       .mode("overwrite")
-      .option("table", "TENANT_VIEW")
-      .option(PhoenixRuntime.TENANT_ID_ATTRIB, "theTenant")
+      .option("table", TenantTable)
+      .option(PhoenixRuntime.TENANT_ID_ATTRIB, TenantId)
       .option("zkUrl", PhoenixSparkITHelper.getUrl)
       .save()
 
     verifyResults
   }
 
-  test("Can save to Phoenix tenant-specific view") {
+  test("Can write an RDD to Phoenix tenant-specific view") {
     val sqlContext = new SQLContext(sc)
-
-    // This view name must match the view we create in phoenix-spark/src/it/resources/tenantSetup.sql
-    val ViewName = "TENANT_VIEW"
-
-    // Columns from the TENANT_VIEW schema
-    val OrgId = "ORGANIZATION_ID"
-    val TenantCol = "TENANT_ONLY_COL"
-
-    // Data matching the schema for TENANT_VIEW created in tenantSetup.sql
-    val dataSet = List(("testOrg1", "data1"), ("testOrg2", "data2"), ("testOrg3", "data3"))
-
     sc
-      .parallelize(dataSet)
+      .parallelize(TestDataSet)
       .saveToPhoenix(
-        ViewName,
-        Seq(OrgId, TenantCol),
+        TenantTable,
+        Seq(OrgIdCol, TenantOnlyCol),
         hbaseConfiguration,
         tenantId = Some(TenantId)
       )

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/main/scala/org/apache/phoenix/spark/PhoenixRDD.scala
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/PhoenixRDD.scala b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/PhoenixRDD.scala
index cfaefa1..505de1b 100644
--- a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/PhoenixRDD.scala
+++ b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/PhoenixRDD.scala
@@ -18,7 +18,7 @@ import java.sql.DriverManager
 import org.apache.hadoop.conf.Configuration
 import org.apache.hadoop.hbase.{HBaseConfiguration, HConstants}
 import org.apache.hadoop.io.NullWritable
-import org.apache.phoenix.jdbc.{PhoenixDriver, PhoenixEmbeddedDriver}
+import org.apache.phoenix.jdbc.PhoenixDriver
 import org.apache.phoenix.mapreduce.PhoenixInputFormat
 import org.apache.phoenix.mapreduce.util.PhoenixConfigurationUtil
 import org.apache.phoenix.schema.types._
@@ -26,14 +26,17 @@ import org.apache.phoenix.util.ColumnInfo
 import org.apache.spark._
 import org.apache.spark.annotation.DeveloperApi
 import org.apache.spark.rdd.RDD
-import org.apache.spark.sql.{DataFrame, Row, SQLContext}
 import org.apache.spark.sql.types._
+import org.apache.spark.sql.{DataFrame, Row, SQLContext}
 
 import scala.collection.JavaConverters._
 
 class PhoenixRDD(sc: SparkContext, table: String, columns: Seq[String],
-                 predicate: Option[String] = None, zkUrl: Option[String] = None,
-                 @transient conf: Configuration, dateAsTimestamp: Boolean = false)
+                 predicate: Option[String] = None,
+                 zkUrl: Option[String] = None,
+                 @transient conf: Configuration, dateAsTimestamp: Boolean = false,
+                 tenantId: Option[String] = None
+                )
   extends RDD[PhoenixRecordWritable](sc, Nil) with Logging {
 
   // Make sure to register the Phoenix driver
@@ -97,6 +100,11 @@ class PhoenixRDD(sc: SparkContext, table: String, columns: Seq[String],
       }
     }
 
+    tenantId match {
+      case Some(tid) => ConfigurationUtil.setTenantId(config, tid)
+      case _ =>
+    }
+
     config
   }
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkContextFunctions.scala
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkContextFunctions.scala b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkContextFunctions.scala
index a3cd8f0..476ce8a 100644
--- a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkContextFunctions.scala
+++ b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkContextFunctions.scala
@@ -32,10 +32,10 @@ class SparkContextFunctions(@transient val sc: SparkContext) extends Serializabl
    */
 
   def phoenixTableAsRDD(table: String, columns: Seq[String], predicate: Option[String] = None,
-                        zkUrl: Option[String] = None, conf: Configuration = new Configuration())
+                        zkUrl: Option[String] = None, tenantId: Option[String] = None, conf: Configuration = new Configuration())
                         : RDD[Map[String, AnyRef]] = {
 
     // Create a PhoenixRDD, but only return the serializable 'result' map
-    new PhoenixRDD(sc, table, columns, predicate, zkUrl, conf).map(_.result)
+    new PhoenixRDD(sc, table, columns, predicate, zkUrl, conf, tenantId = tenantId).map(_.result)
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e45b5a70/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkSqlContextFunctions.scala
----------------------------------------------------------------------
diff --git a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkSqlContextFunctions.scala b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkSqlContextFunctions.scala
index cc3f378..a0842c9 100644
--- a/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkSqlContextFunctions.scala
+++ b/phoenix-spark/src/main/scala/org/apache/phoenix/spark/SparkSqlContextFunctions.scala
@@ -29,11 +29,13 @@ class SparkSqlContextFunctions(@transient val sqlContext: SQLContext) extends Se
     property will be used
  */
   def phoenixTableAsDataFrame(table: String, columns: Seq[String],
-                               predicate: Option[String] = None, zkUrl: Option[String] = None,
+                               predicate: Option[String] = None,
+                               zkUrl: Option[String] = None,
+                               tenantId: Option[String] = None,
                                conf: Configuration = new Configuration): DataFrame = {
 
     // Create the PhoenixRDD and convert it to a DataFrame
-    new PhoenixRDD(sqlContext.sparkContext, table, columns, predicate, zkUrl, conf)
+    new PhoenixRDD(sqlContext.sparkContext, table, columns, predicate, zkUrl, conf, tenantId = tenantId)
       .toDataFrame(sqlContext)
   }
 }
\ No newline at end of file