SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package nl.topicus.spannertest.simple;
  2.  
  3. import java.util.Properties;
  4.  
  5. import org.apache.spark.sql.Dataset;
  6. import org.apache.spark.sql.Row;
  7. import org.apache.spark.sql.SparkSession;
  8. import org.apache.spark.sql.jdbc.JdbcDialect;
  9. import org.apache.spark.sql.jdbc.JdbcDialects;
  10.  
  11. public class SparkTest
  12. {
  13.  
  14.     public static void main(String[] args)
  15.     {
  16.         String url = "jdbc:cloudspanner://localhost";
  17.         Properties props = new Properties();
  18.         props.setProperty("Project", "adroit-valve-654321");
  19.         props.setProperty("Instance", "spark-instance");
  20.         props.setProperty("Database", "spark");
  21.         props.setProperty("PvtKeyPath", "C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner-key.json");
  22.  
  23.         testSpark(url, props);
  24.     }
  25.  
  26.     private static void testSpark(String sparkURL, Properties properties)
  27.     {
  28.         try (SparkSession spark = SparkSession.builder().appName("Java Spark Google Cloud Spanner basic example")
  29.                 .master("local").getOrCreate())
  30.         {
  31.             // Register a specific dialect for Cloud Spanner as Cloud Spanner uses backticks (`) for identifier quoting
  32.             JdbcDialects.registerDialect(new JdbcDialect()
  33.             {
  34.                 private static final long serialVersionUID = 1L;
  35.  
  36.                 @Override
  37.                 public boolean canHandle(String url)
  38.                 {
  39.                     return url.toLowerCase().startsWith("jdbc:cloudspanner:");
  40.                 }
  41.  
  42.                 @Override
  43.                 public String quoteIdentifier(String column)
  44.                 {
  45.                     return "`" + column + "`";
  46.                 }
  47.             });
  48.             Dataset<Row> datasetAccount = spark.read().jdbc(sparkURL, "ACCOUNT", properties);
  49.             datasetAccount.show();
  50.  
  51.             Dataset<Row> datasetEmployee = spark.read().jdbc(sparkURL, "EMPLOYEE", "ID", 1l, 1000l, 10, properties);
  52.             datasetEmployee.show(2000);
  53.         }
  54.     }
  55.  
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top