package test;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
public class TestSparkSql {
public static void main(String[] args) {
SparkSession spark = SparkSession
.builder()
.appName("Java Spark Hive Example")
.master("local[1]")
// .config("spark.sql.warehouse.dir", warehouseLocation)
.enableHiveSupport()
.getOrCreate();
spark.sql("drop table if exists src");
spark.sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)");
spark.sql("insert into src values (1,\"张三\")");
spark.sql("insert into src values (2,\"李四\")");
spark.sql("select * from src").show();
Dataset<Row> rowDataset = spark.sql("select * from src");
rowDataset.foreach(row->{
int age = (int) row.get(0);
String name = row.getString(1);
System.out.println(age+"-->"+name);
});
}
}
java Sparksql 中遍历dataset的代码案例
最新推荐文章于 2024-06-02 01:43:53 发布