如何使用Spark Java API将DataFrame保存到Cassandra表中

时间:2017-07-19 12:26:07

标签: java apache-spark apache-spark-sql spark-dataframe spark-cassandra-connector

我想使用sparkJava API将数据框保存到cassandra表中

我想在以下代码中添加保存部分 我想将人们的数据框保存到cassandra表中并在该cassandra表上进行查询

import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructType;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.SQLContext;
import com.datastax.spark.connector.cql.CassandraConnector;
import com.datastax.spark.connector.japi.CassandraRow;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrame;

public class SimpleApp {
  public static void main(String[] args) {
    SparkConf conf = new SparkConf().setAppName("Simple Application");
    conf.setMaster("local");
    conf.set("spark.cassandra.connection.host", "localhost");
    JavaSparkContext sc = new JavaSparkContext(conf);
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    DataFrame people = sqlContext.read().json("/root/people.json");
    people.printSchema();
    people.registerTempTable("people");
    **//I want to save this TempTable or people dataframe into cassandra table and make teenagers SQL query on that cassandra table** 
    DataFrame teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19");
    teenagers.show();


 }
}

0 个答案:

没有答案