Github user tdas commented on a diff in the pull request:
https://github.com/apache/spark/pull/6006#discussion_r30830692
--- Diff: sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala ---
@@ -1270,9 +1271,59 @@ class SQLContext(@transient val sparkContext:
SparkContext)
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
- // End of eeprecated methods
+ // End of deprecated methods
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
+
+
+ // Register a succesfully instantiatd context to the singleton. This
should be at the end of
+ // the class definition so that the singleton is updated only if there
is no exception in the
+ // construction of the instance.
+ SQLContext.setLastInstantiatedContext(self)
}
+/**
+ * This SQLContext object contains utility functions to create a singleton
SQLContext instance,
+ * or to get the last created SQLContext instance.
+ */
+object SQLContext {
+
+ private val INSTANTIATION_LOCK = new Object()
+
+ /**
+ * Reference to the last created SQLContext.
+ */
+ @transient private val lastInstantiatedContext = new
AtomicReference[SQLContext]()
+ /**
+ * Get the singleton SQLContext if it exists or create a new one using
the given configuration.
+ * This function can be used to create a singleton SQLContext object
that can be shared across
+ * the JVM.
+ */
+ def getOrCreate(sparkContext: SparkContext): SQLContext = {
+ INSTANTIATION_LOCK.synchronized {
+ if (lastInstantiatedContext.get() == null) {
+ new SQLContext(sparkContext)
+ }
+ }
+ lastInstantiatedContext.get()
+ }
+
+ private[sql] def getLastInstantiatedContext(): Option[SQLContext] = {
--- End diff --
Aah, was using it for tests but changed tests in the middle.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]