|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +from pyspark.sql import DataFrame |
| 4 | +from py4j.java_gateway import java_import |
| 5 | + |
| 6 | + |
| 7 | +class PySpliceContext: |
| 8 | + """ |
| 9 | + This class implements a SpliceMachineContext object (similar to the SparkContext object) |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, JDBC_URL, sparkSQLContext, _unitTesting=False): |
| 13 | + """ |
| 14 | + :param JDBC_URL: (string) The JDBC URL Connection String for your Splice Machine Cluster |
| 15 | + :param sparkSQLContext: (sparkContext) A SparkContext Object for executing Spark Queries |
| 16 | + """ |
| 17 | + self.jdbcurl = JDBC_URL |
| 18 | + self._unitTesting = _unitTesting |
| 19 | + |
| 20 | + if not _unitTesting: # Private Internal Argument to Override Using JVM |
| 21 | + self.sparkSQLContext = sparkSQLContext |
| 22 | + self.jvm = self.sparkSQLContext._sc._jvm |
| 23 | + java_import(self.jvm, "com.splicemachine.spark.splicemachine.*") |
| 24 | + java_import(self.jvm, "org.apache.spark.sql.execution.datasources.jdbc.{JDBCOptions, JdbcUtils}") |
| 25 | + java_import(self.jvm, "scala.collection.JavaConverters._") |
| 26 | + self.context = self.jvm.com.splicemachine.spark.splicemachine.SplicemachineContext(self.jdbcurl) |
| 27 | + |
| 28 | + else: |
| 29 | + from .utils import FakeJContext |
| 30 | + self.sparkSQLContext = sparkSQLContext |
| 31 | + self.jvm = '' |
| 32 | + self.context = FakeJContext(self.jdbcurl) |
| 33 | + |
| 34 | + def getConnection(self): |
| 35 | + """ |
| 36 | + Return a connection to the database |
| 37 | + """ |
| 38 | + return self.context.getConnection() |
| 39 | + |
| 40 | + def tableExists(self, schemaTableName): |
| 41 | + """ |
| 42 | + Check whether or not a table exists |
| 43 | +
|
| 44 | + :param schemaTableName: (string) Table Name |
| 45 | + """ |
| 46 | + return self.context.tableExists(schemaTableName) |
| 47 | + |
| 48 | + def dropTable(self, schemaTableName): # works |
| 49 | + """ |
| 50 | + Drop a specified table. |
| 51 | +
|
| 52 | + :param schemaTableName (optional): (string) schemaName.tableName |
| 53 | + """ |
| 54 | + return self.context.dropTable(schemaTableName) |
| 55 | + |
| 56 | + def df(self, sql): |
| 57 | + """ |
| 58 | + Return a Spark Dataframe from the results of a Splice Machine SQL Query |
| 59 | +
|
| 60 | + :param sql: (string) SQL Query (eg. SELECT * FROM table1 WHERE column2 > 3) |
| 61 | + :return: A Spark DataFrame containing the results |
| 62 | + """ |
| 63 | + if self._unitTesting: |
| 64 | + return self.context.df(sql) |
| 65 | + return DataFrame(self.context.df(sql), self.sparkSQLContext) |
| 66 | + |
| 67 | + def insert(self, dataFrame, schemaTableName): |
| 68 | + """ |
| 69 | + Insert a RDD into a table (schema.table). The schema is required since RDD's do not have schema. |
| 70 | +
|
| 71 | + :param dataFrame: (RDD) The dataFrame you would like to insert |
| 72 | + :param schemaTableName: (string) The table in which you would like to insert the RDD |
| 73 | + """ |
| 74 | + return self.context.insert(dataFrame._jdf, schemaTableName) |
| 75 | + |
| 76 | + def delete(self, dataFrame, schemaTableName): |
| 77 | + """ |
| 78 | + Delete records in a dataframe based on joining by primary keys from the data frame. |
| 79 | + Be careful with column naming and case sensitivity. |
| 80 | +
|
| 81 | + :param dataFrame: (RDD) The dataFrame you would like to delete |
| 82 | + :param schemaTableName: (string) Splice Machine Table |
| 83 | + """ |
| 84 | + return self.context.delete(dataFrame._jdf, schemaTableName) |
| 85 | + |
| 86 | + def update(self, dataFrame, schemaTableName): |
| 87 | + """ |
| 88 | + Update data from a dataframe for a specified schemaTableName (schema.table). |
| 89 | + The keys are required for the update and any other columns provided will be updated in the rows. |
| 90 | +
|
| 91 | + :param dataFrame: (RDD) The dataFrame you would like to update |
| 92 | + :param schemaTableName: (string) Splice Machine Table |
| 93 | + :return: |
| 94 | + """ |
| 95 | + return self.context.update(dataFrame._jdf, schemaTableName) |
| 96 | + |
| 97 | + def getSchema(self, schemaTableName): |
| 98 | + """ |
| 99 | + Return the schema via JDBC. |
| 100 | +
|
| 101 | + :param schemaTableName: (string) Table name |
| 102 | + """ |
| 103 | + return self.context.getSchema(schemaTableName) |
| 104 | + |
0 commit comments