本文整理汇总了Java中org.apache.spark.sql.SparkSession.stop方法的典型用法代码示例。如果您正苦于以下问题:Java SparkSession.stop方法的具体用法?Java SparkSession.stop怎么用?Java SparkSession.stop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.spark.sql.SparkSession
的用法示例。
在下文中一共展示了SparkSession.stop方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession.builder()
.master("local[8]")
.appName("PCAExpt")
.getOrCreate();
// Load and parse data
String filePath = "/home/kchoppella/book/Chapter09/data/covtypeNorm.csv";
// Loads data.
Dataset<Row> inDataset = spark.read()
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", true)
.load(filePath);
ArrayList<String> inputColsList = new ArrayList<String>(Arrays.asList(inDataset.columns()));
//Make single features column for feature vectors
inputColsList.remove("class");
String[] inputCols = inputColsList.parallelStream().toArray(String[]::new);
//Prepare dataset for training with all features in "features" column
VectorAssembler assembler = new VectorAssembler().setInputCols(inputCols).setOutputCol("features");
Dataset<Row> dataset = assembler.transform(inDataset);
PCAModel pca = new PCA()
.setK(16)
.setInputCol("features")
.setOutputCol("pcaFeatures")
.fit(dataset);
Dataset<Row> result = pca.transform(dataset).select("pcaFeatures");
System.out.println("Explained variance:");
System.out.println(pca.explainedVariance());
result.show(false);
// $example off$
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:39,代码来源:PCAExpt.java
示例2: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession.builder()
.master("local[8]")
.appName("KMeansExpt")
.getOrCreate();
// Load and parse data
String filePath = "/home/kchoppella/book/Chapter09/data/covtypeNorm.csv";
// Loads data.
Dataset<Row> inDataset = spark.read()
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", true)
.load(filePath);
ArrayList<String> inputColsList = new ArrayList<String>(Arrays.asList(inDataset.columns()));
//Make single features column for feature vectors
inputColsList.remove("class");
String[] inputCols = inputColsList.parallelStream().toArray(String[]::new);
//Prepare dataset for training with all features in "features" column
VectorAssembler assembler = new VectorAssembler().setInputCols(inputCols).setOutputCol("features");
Dataset<Row> dataset = assembler.transform(inDataset);
KMeans kmeans = new KMeans().setK(27).setSeed(1L);
KMeansModel model = kmeans.fit(dataset);
// Evaluate clustering by computing Within Set Sum of Squared Errors.
double WSSSE = model.computeCost(dataset);
System.out.println("Within Set Sum of Squared Errors = " + WSSSE);
// Shows the result.
Vector[] centers = model.clusterCenters();
System.out.println("Cluster Centers: ");
for (Vector center: centers) {
System.out.println(center);
}
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:43,代码来源:KMeansExpt.java
示例3: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession.builder()
.master("local[8]")
.appName("BisectingKMeansExpt")
.getOrCreate();
// Load and parse data
String filePath = "/home/kchoppella/book/Chapter09/data/covtypeNorm.csv";
// Loads data.
Dataset<Row> inDataset = spark.read()
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", true)
.load(filePath);
//Make single features column for feature vectors
ArrayList<String> inputColsList = new ArrayList<String>(Arrays.asList(inDataset.columns()));
inputColsList.remove("class");
String[] inputCols = inputColsList.parallelStream().toArray(String[]::new);
//Prepare dataset for training with all features in "features" column
VectorAssembler assembler = new VectorAssembler().setInputCols(inputCols).setOutputCol("features");
Dataset<Row> dataset = assembler.transform(inDataset);
// Trains a bisecting k-means model.
BisectingKMeans bkm = new BisectingKMeans().setK(27).setSeed(1);
BisectingKMeansModel model = bkm.fit(dataset);
// Evaluate clustering by computing Within Set Sum of Squared Errors.
double WSSSE = model.computeCost(dataset);
System.out.println("Within Set Sum of Squared Errors = " + WSSSE);
// Shows the result.
Vector[] centers = model.clusterCenters();
System.out.println("Cluster Centers: ");
for (Vector center: centers) {
System.out.println(center);
}
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:44,代码来源:BisectingKMeansExpt.java
示例4: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession.builder()
.master("local[8]")
.appName("KMeansWithPCAExpt")
.getOrCreate();
// Load and parse data
String filePath = "/home/kchoppella/book/Chapter09/data/covtypeNorm.csv";
// Loads data.
Dataset<Row> inDataset = spark.read()
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", true)
.load(filePath);
ArrayList<String> inputColsList = new ArrayList<String>(Arrays.asList(inDataset.columns()));
//Make single features column for feature vectors
inputColsList.remove("class");
String[] inputCols = inputColsList.parallelStream().toArray(String[]::new);
//Prepare dataset for training with all features in "features" column
VectorAssembler assembler = new VectorAssembler().setInputCols(inputCols).setOutputCol("features");
Dataset<Row> dataset = assembler.transform(inDataset);
PCAModel pca = new PCA()
.setK(16)
.setInputCol("features")
.setOutputCol("pcaFeatures")
.fit(dataset);
Dataset<Row> result = pca.transform(dataset).select("pcaFeatures");
System.out.println("Explained variance:");
System.out.println(pca.explainedVariance());
result.show(false);
KMeans kmeans = new KMeans().setK(27).setSeed(1L);
KMeansModel model = kmeans.fit(dataset);
// Evaluate clustering by computing Within Set Sum of Squared Errors.
double WSSSE = model.computeCost(dataset);
System.out.println("Within Set Sum of Squared Errors = " + WSSSE);
// $example off$
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:47,代码来源:KMeansWithPCAExpt.java
示例5: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession.builder()
.master("local[8]")
.appName("GaussianMixtureModelExpt")
.getOrCreate();
// Load and parse data
String filePath = "/home/kchoppella/book/Chapter09/data/covtypeNorm.csv";
// Loads data.
Dataset<Row> inDataset = spark.read()
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", true)
.load(filePath);
ArrayList<String> inputColsList = new ArrayList<String>(Arrays.asList(inDataset.columns()));
//Make single features column for feature vectors
inputColsList.remove("class");
String[] inputCols = inputColsList.parallelStream().toArray(String[]::new);
//Prepare dataset for training with all features in "features" column
VectorAssembler assembler = new VectorAssembler().setInputCols(inputCols).setOutputCol("features");
Dataset<Row> dataset = assembler.transform(inDataset);
PCAModel pca = new PCA()
.setK(16)
.setInputCol("features")
.setOutputCol("pcaFeatures")
.fit(dataset);
Dataset<Row> result = pca.transform(dataset).select("pcaFeatures").withColumnRenamed("pcaFeatures", "features");
String outPath = "/home/kchoppella/book/Chapter09/data/gmm_params.csv";
try {
BufferedWriter writer = Files.newBufferedWriter(Paths.get(outPath));
// Cluster the data into multiple classes using KMeans
int numClusters = 27;
GaussianMixtureModel gmm = new GaussianMixture()
.setK(numClusters).
fit(result);
int numIterations = gmm.getK();
// Output the parameters of the mixture model
for (int i = 0; i < numIterations; i++) {
String msg = String.format("Gaussian %d:\nweight=%f\nmu=%s\nsigma=\n%s\n\n",
i,
gmm.weights()[i],
gmm.gaussians()[i].mean(),
gmm.gaussians()[i].cov());
System.out.printf(msg);
writer.write(msg + "\n");
writer.flush();
}
}
catch (IOException iox) {
System.out.println("Write Exception: \n");
iox.printStackTrace();
}
finally {
}
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:68,代码来源:GaussianMixtureModelExpt.java
示例6: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
SparkSession spark = SparkSession
.builder().master("local").config("spark.sql.warehouse.dir", "file:///C:/Users/sumit.kumar/Downloads/bin/warehouse")
.appName("JavaEstimatorTransformerParamExample")
.getOrCreate();
Logger rootLogger = LogManager.getRootLogger();
rootLogger.setLevel(Level.WARN);
// $example on$
// Prepare training data.
List<Row> dataTraining = Arrays.asList(
RowFactory.create(1.0, Vectors.dense(0.0, 1.1, 0.1)),
RowFactory.create(0.0, Vectors.dense(2.0, 1.0, -1.0)),
RowFactory.create(0.0, Vectors.dense(2.0, 1.3, 1.0)),
RowFactory.create(1.0, Vectors.dense(0.0, 1.2, -0.5))
);
StructType schema = new StructType(new StructField[]{
new StructField("label", DataTypes.DoubleType, false, Metadata.empty()),
new StructField("features", new VectorUDT(), false, Metadata.empty())
});
Dataset<Row> training = spark.createDataFrame(dataTraining, schema);
// Create a LogisticRegression instance. This instance is an Estimator.
LogisticRegression lr = new LogisticRegression();
// Print out the parameters, documentation, and any default values.
System.out.println("LogisticRegression parameters:\n" + lr.explainParams() + "\n");
// We may set parameters using setter methods.
lr.setMaxIter(10).setRegParam(0.01);
// Learn a LogisticRegression model. This uses the parameters stored in lr.
LogisticRegressionModel model1 = lr.fit(training);
// Since model1 is a Model (i.e., a Transformer produced by an Estimator),
// we can view the parameters it used during fit().
// This prints the parameter (name: value) pairs, where names are unique IDs for this
// LogisticRegression instance.
System.out.println("Model 1 was fit using parameters: " + model1.parent().extractParamMap());
// We may alternatively specify parameters using a ParamMap.
ParamMap paramMap = new ParamMap()
.put(lr.maxIter().w(20)) // Specify 1 Param.
.put(lr.maxIter(), 30) // This overwrites the original maxIter.
.put(lr.regParam().w(0.1), lr.threshold().w(0.55)); // Specify multiple Params.
// One can also combine ParamMaps.
ParamMap paramMap2 = new ParamMap()
.put(lr.probabilityCol().w("myProbability")); // Change output column name
ParamMap paramMapCombined = paramMap.$plus$plus(paramMap2);
// Now learn a new model using the paramMapCombined parameters.
// paramMapCombined overrides all parameters set earlier via lr.set* methods.
LogisticRegressionModel model2 = lr.fit(training, paramMapCombined);
System.out.println("Model 2 was fit using parameters: " + model2.parent().extractParamMap());
// Prepare test documents.
List<Row> dataTest = Arrays.asList(
RowFactory.create(1.0, Vectors.dense(-1.0, 1.5, 1.3)),
RowFactory.create(0.0, Vectors.dense(3.0, 2.0, -0.1)),
RowFactory.create(1.0, Vectors.dense(0.0, 2.2, -1.5))
);
Dataset<Row> test = spark.createDataFrame(dataTest, schema);
// Make predictions on test documents using the Transformer.transform() method.
// LogisticRegression.transform will only use the 'features' column.
// Note that model2.transform() outputs a 'myProbability' column instead of the usual
// 'probability' column since we renamed the lr.probabilityCol parameter previously.
Dataset<Row> results = model2.transform(test);
Dataset<Row> rows = results.select("features", "label", "myProbability", "prediction");
for (Row r: rows.collectAsList()) {
System.out.println("(" + r.get(0) + ", " + r.get(1) + ") -> prob=" + r.get(2)
+ ", prediction=" + r.get(3));
}
// $example off$
spark.stop();
}
开发者ID:PacktPublishing,项目名称:Apache-Spark-2x-for-Java-Developers,代码行数:76,代码来源:JavaEstimatorTransformerParamExample.java
示例7: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
String query = null;
String queryOverwrite = null;
// Read program arguments
for (int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-query")) {
query = args[i + 1];
} else if (args[i].equalsIgnoreCase("-queryOverwrite")) {
queryOverwrite = args[i + 1];
}
}
if (query == null) {
query = "message = 'Hello World'; \n"
+ "result = select cast(unix_timestamp() as timestamp) as time, '${message}' as message; \n"
+ "printTable(result);";
} else if (query.toLowerCase().startsWith("http://") || query.toLowerCase().startsWith("https://")) {
System.out.println("Fetching query from http: " + query);
query = HttpUtils.get(query, null).getBody();
}
System.out.println("Query: " + query);
System.out.println("Query Overwrite: " + queryOverwrite);
// Start Spark Session
String master = "local[1]";
String appName = "QueryExampleJob";
SparkConf sparkConf = new SparkConf()
.setMaster(master)
.setAppName(appName);
SparkSession sparkSession = SparkSession
.builder()
.config(sparkConf).getOrCreate();
// Load query from hdfs if it is hdfs url
if (query.toLowerCase().startsWith("hdfs://")) {
System.out.println("Fetching query from hdfs: " + query);
Tuple2<String, String>[] tuples = (Tuple2<String, String>[]) sparkSession.sparkContext().wholeTextFiles(query, 1).collect();
query = tuples[0]._2();
System.out.println("Query: " + query);
}
// Run query
QueryEngine engine = new QueryEngine();
engine.executeScript(query, queryOverwrite, sparkSession, false);
sparkSession.stop();
}
示例8: main
import org.apache.spark.sql.SparkSession; //导入方法依赖的package包/类
public static void main(String[] args) {
// Prepare data as the input for the script.
// This is just for demonstration. In real scenario, you will have your own data in database, hadoop, or other places.
String connectionString = prepareJdbcData();
String jsonFile = prepareJsonFile();
// Start Spark Session
String master = "local[1]";
String appName = "QueryExecutionExample";
SparkConf sparkConf = new SparkConf()
.setMaster(master)
.setAppName(appName);
SparkSession sparkSession = SparkSession
.builder()
.config(sparkConf).getOrCreate();
// Set log level to avoid too many outputs
sparkSession.sparkContext().setLogLevel("ERROR");
// Script to run
String script =
String.format("clients = sql jdbc set connectionString='%s'; select clientId, clientName from dim_client; \n", connectionString)
+ String.format("orders = file json file://%s; \n", jsonFile)
+ "result = select clientName,productName,totalCount from orders join clients on clients.clientId = orders.clientId; \n"
+ String.format("writeJdbc('%s', 'jdbcTable', 'clientName,productName', 'clientName,productName', '', 'Append', result);", connectionString);
System.out.println("-----------------------------------------------");
System.out.println("Running script");
System.out.println("-----------------------------------------------");
System.out.println(script);
System.out.println("-----------------------------------------------");
// Run Script
QueryEngine engine = new QueryEngine();
engine.executeScript(script, sparkSession);
// The data should be written into the database by writeJdbc action.
// Let's query it and print it out.
DataSetResult dataResult = SqlUtils.executeJdbcQuery(connectionString, "select * from jdbcTable");
System.out.println("-----------------------------------------------");
System.out.println("Data in jdbc table after executing script");
System.out.println("-----------------------------------------------");
dataResult.print();
System.out.println("-----------------------------------------------");
sparkSession.stop();
}