当前位置: 首页>>代码示例>>Java>>正文


Java SparkSession.sql方法代码示例

本文整理汇总了Java中org.apache.spark.sql.SparkSession.sql方法的典型用法代码示例。如果您正苦于以下问题:Java SparkSession.sql方法的具体用法?Java SparkSession.sql怎么用?Java SparkSession.sql使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.spark.sql.SparkSession的用法示例。


在下文中一共展示了SparkSession.sql方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: execute

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
@Override
public Object execute(SparkSession sparkSession, ActionStatement actionStatement, CredentialProvider credentialManager) {

    String filePath = actionStatement.getParamValues().get(0).getValue().toString();
    String saveModeStr = actionStatement.getParamValues().get(1).getValue().toString();
    String dfTableName = actionStatement.getParamValues().get(2).getValue().toString();

    SaveMode saveMode = SaveMode.valueOf(saveModeStr);

    String sql = String.format("select * from %s", dfTableName);
    logger.info(String.format("Running sql [%s] to get data and then save it", sql));
    Dataset<Row> df = sparkSession.sql(sql);

    logger.info(String.format("Saving to csv %s, saveMode: %s", filePath, saveMode));
    df.coalesce(1).write().mode(saveMode).option("header", "false").csv(filePath);
    logger.info(String.format("Saved to csv %s, saveMode: %s", filePath, saveMode));
    return null;
}
 
开发者ID:uber,项目名称:uberscriptquery,代码行数:19,代码来源:WriteCsvFileActionStatementExecutor.java

示例2: execute

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
@Override
public Object execute(SparkSession sparkSession, ActionStatement actionStatement, CredentialProvider credentialManager) {

    String filePath = actionStatement.getParamValues().get(0).getValue().toString();
    String saveModeStr = actionStatement.getParamValues().get(1).getValue().toString();
    String dfTableName = actionStatement.getParamValues().get(2).getValue().toString();

    SaveMode saveMode = SaveMode.valueOf(saveModeStr);

    String sql = String.format("select * from %s", dfTableName);
    logger.info(String.format("Running sql [%s] to get data and then save it", sql));
    Dataset<Row> df = sparkSession.sql(sql);

    logger.info(String.format("Saving to json %s, saveMode: %s", filePath, saveMode));
    df.coalesce(1).write().mode(saveMode).json(filePath);
    logger.info(String.format("Saved to json %s, saveMode: %s", filePath, saveMode));
    return null;
}
 
开发者ID:uber,项目名称:uberscriptquery,代码行数:19,代码来源:WriteJsonFileActionStatementExecutor.java

示例3: execute

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
@Override
public Object execute(SparkSession sparkSession, ActionStatement actionStatement, CredentialProvider credentialManager) {

    String filePath = actionStatement.getParamValues().get(0).getValue().toString();
    String saveModeStr = actionStatement.getParamValues().get(1).getValue().toString();
    String dfTableName = actionStatement.getParamValues().get(2).getValue().toString();

    SaveMode saveMode = SaveMode.valueOf(saveModeStr);

    String sql = String.format("select * from %s", dfTableName);
    logger.info(String.format("Running sql [%s] to get data and then save it", sql));
    Dataset<Row> df = sparkSession.sql(sql);

    logger.info(String.format("Saving to parquet %s, saveMode: %s", filePath, saveMode));
    df.coalesce(1).write().mode(saveMode).parquet(filePath);
    logger.info(String.format("Saved to parquet %s, saveMode: %s", filePath, saveMode));
    return null;
}
 
开发者ID:uber,项目名称:uberscriptquery,代码行数:19,代码来源:WriteParquetFileActionStatementExecutor.java

示例4: main

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
	System.setProperty("hadoop.home.dir", "C:\\softwares\\Winutils");
	SparkSession sparkSession = SparkSession.builder()
			.master("local")
			.appName("CSV Read Example")
			.config("spark.sql.warehouse.dir", "file:////C:/Users/sgulati/spark-warehouse")
			.getOrCreate();
	
	Dataset<Row> csv = sparkSession.read().format("com.databricks.spark.csv").option("header","true")
			.load("C:\\Users\\sgulati\\Documents\\my_docs\\book\\testdata\\emp.csv");
	
	csv.createOrReplaceTempView("test");
	Dataset<Row> sql = sparkSession.sql("select * from test");
	sql.collectAsList();
}
 
开发者ID:PacktPublishing,项目名称:Apache-Spark-2x-for-Java-Developers,代码行数:16,代码来源:SparkSessionHeloWorld.java

示例5: createAncestorsTable

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
/**
 * Creates a table of ancestor records partitioned by uri and version.
 *
 * @param spark the spark session
 * @param tableName the name of the ancestors table
 * @param location the location to store the table, or null to create a Hive-managed table
 * @throws IllegalArgumentException if the table name or location are malformed
 */
private static void createAncestorsTable(SparkSession spark, String tableName, String location) {

  if (!TABLE_NAME_PATTERN.matcher(tableName).matches()) {
    throw new IllegalArgumentException("Invalid table name: " + tableName);
  }

  // Hive will check for well-formed paths, so we just ensure a user isn't attempting to inject
  // additional SQL into the statement
  if (location != null && location.contains(";")) {
    throw new IllegalArgumentException("Invalid path for values table: " + location);
  }

  StringBuilder builder = new StringBuilder();

  if (location != null) {

    builder.append("CREATE EXTERNAL TABLE IF NOT EXISTS ");

  } else {

    builder.append("CREATE TABLE IF NOT EXISTS ");
  }

  builder.append(tableName);

  // Note the partitioned by columns are deliberately lower case here since Spark does not appear
  // to match columns to Hive partitions if they are not
  builder.append("(descendantSystem STRING, "
      + "descendantValue STRING, "
      + "ancestorSystem STRING,"
      + "ancestorValue String)\n"
      + "PARTITIONED BY (uri STRING, version STRING)\n");

  builder.append("STORED AS PARQUET\n");

  if (location != null) {
    builder.append("LOCATION '")
        .append(location)
        .append("'");
  }

  spark.sql(builder.toString());
}
 
开发者ID:cerner,项目名称:bunsen,代码行数:52,代码来源:Hierarchies.java

示例6: createValuesTable

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
/**
 * Creates a table of value records partitioned by valueseturi and valuesetversion.
 *
 * @param spark the spark session
 * @param tableName the name of the values table
 * @param location the location to store the table, or null to create a Hive-managed table
 * @throws IllegalArgumentException if the table name or location are malformed
 */
private static void createValuesTable(SparkSession spark, String tableName, String location) {

  if (!TABLE_NAME_PATTERN.matcher(tableName).matches()) {
    throw new IllegalArgumentException("Invalid table name: " + tableName);
  }

  // Hive will check for well-formed paths, so we just ensure a user isn't attempting to inject
  // additional SQL into the statement
  if (location != null && location.contains(";")) {
    throw new IllegalArgumentException("Invalid path for values table: " + location);
  }

  StringBuilder builder = new StringBuilder();

  if (location != null) {

    builder.append("CREATE EXTERNAL TABLE IF NOT EXISTS ");

  } else {

    builder.append("CREATE TABLE IF NOT EXISTS ");
  }

  builder.append(tableName);

  // Note the partitioned by columns are deliberately lower case here since Spark does not appear
  // to match columns to Hive partitions if they are not
  builder.append("(system STRING, "
      + "version STRING, "
      + "value STRING)\n"
      + "PARTITIONED BY (valueseturi STRING, valuesetversion STRING)\n");

  builder.append("STORED AS PARQUET\n");

  if (location != null) {
    builder.append("LOCATION '")
        .append(location)
        .append("'");
  }

  spark.sql(builder.toString());
}
 
开发者ID:cerner,项目名称:bunsen,代码行数:51,代码来源:ValueSets.java

示例7: createMappingTable

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
/**
 * Creates a table of mapping records partitioned by conceptmapuri and
 * conceptmapversion.
 *
 * @param spark the spark session
 * @param tableName the name of the mapping table
 * @param location the location to store the table, or null to create a Hive-managed table.
 * @throws IllegalArgumentException if the table name or location are malformed.
 */
private static void createMappingTable(SparkSession spark,
    String tableName,
    String location) {

  if (!TABLE_NAME_PATTERN.matcher(tableName).matches()) {
    throw new IllegalArgumentException("Invalid table name: " + tableName);
  }

  // Hive will check for well-formed paths, so we just ensure
  // a user isn't attempting to inject additional SQL into the statement.
  if (location != null && location.contains(";")) {
    throw new IllegalArgumentException("Invalid path for mapping table: "
        + location);
  }

  StringBuilder builder = new StringBuilder();

  if (location != null) {

    builder.append("CREATE EXTERNAL TABLE IF NOT EXISTS ");

  } else {

    builder.append("CREATE TABLE IF NOT EXISTS ");
  }

  builder.append(tableName);

  // Note the partitioned by columns are deliberately lower case here,
  // since Spark does not appear to match columns to
  // Hive partitions if they are not.
  builder.append("(sourceValueSet STRING, "
      + "targetValueSet STRING, "
      + "sourceSystem STRING, "
      + "sourceValue STRING, "
      + "targetSystem STRING, "
      + "targetValue STRING, "
      + "equivalence STRING)\n"
      + "PARTITIONED BY (conceptmapuri STRING, conceptmapversion STRING)\n");

  builder.append("STORED AS PARQUET\n");

  if (location != null) {
    builder.append("LOCATION '")
        .append(location)
        .append("'");
  }

  spark.sql(builder.toString());
}
 
开发者ID:cerner,项目名称:bunsen,代码行数:60,代码来源:ConceptMaps.java

示例8: main

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) throws StreamingQueryException {
	System.setProperty("hadoop.home.dir", "C:\\softwares\\Winutils");
	SparkSession sparkSession = SparkSession.builder().master("local[*]").appName("structured Streaming Example")
			.config("spark.sql.warehouse.dir", "file:////C:/Users/sgulati/spark-warehouse").getOrCreate();

	Dataset<Row> inStream = sparkSession.readStream().format("socket").option("host", "10.204.136.223")
			.option("port", 9999).load();

	Dataset<FlightDetails> dsFlightDetails = inStream.as(Encoders.STRING()).map(x -> {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.readValue(x, FlightDetails.class);

	}, Encoders.bean(FlightDetails.class));
	
	
	dsFlightDetails.createOrReplaceTempView("flight_details");
	
	Dataset<Row> avdFlightDetails = sparkSession.sql("select flightId, avg(temperature) from flight_details group by flightId");
	
	StreamingQuery query = avdFlightDetails.writeStream()
			  .outputMode("complete")
			  .format("console")
			  .start();

			query.awaitTermination();
	

}
 
开发者ID:PacktPublishing,项目名称:Apache-Spark-2x-for-Java-Developers,代码行数:29,代码来源:StructuredStreamingExample.java

示例9: saveAsDatabase

import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
/**
 * Saves an RDD of bundles as a database, where each table
 * has the resource name. This offers a simple way to load and query
 * bundles in a system, although users with more sophisticated ETL
 * operations may want to explicitly write different entities.
 *
 * <p>
 * Note this will access the given RDD of bundles once per resource name,
 * so consumers with enough memory should consider calling
 * {@link JavaRDD#cache()} so that RDD is not recomputed for each.
 * </p>
 *
 * @param spark the spark session
 * @param bundles an RDD of FHIR Bundles
 * @param database the name of the database to write to
 * @param resourceNames names of resources to be extracted from the bundle and written
 */
public static void saveAsDatabase(SparkSession spark,
    JavaRDD<Bundle> bundles,
    String database,
    String... resourceNames) {

  spark.sql("create database if not exists " + database);

  for (String resourceName : resourceNames) {

    Dataset ds = extractEntry(spark, bundles, resourceName);

    ds.write().saveAsTable(database + "." + resourceName.toLowerCase());
  }
}
 
开发者ID:cerner,项目名称:bunsen,代码行数:32,代码来源:Bundles.java


注:本文中的org.apache.spark.sql.SparkSession.sql方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。