hologres-connector-examples/hologres-connector-spark-examples/src/main/java/com/alibaba/hologres/spark/example/SparkDataFrameToHoloExample.java [32:90]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    public static void main(String[] args) throws Exception {
        Options options = new Options();
        options.addOption("e", "endpoint", true, "Hologres endpoint");
        options.addOption("u", "username", true, "Username");
        options.addOption("p", "password", true, "Password");
        options.addOption("d", "database", true, "Database");
        options.addOption("t", "tablename", true, "Table name");

        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine = parser.parse(options, args);
        String endPoint = commandLine.getOptionValue("endpoint");
        String userName = commandLine.getOptionValue("username");
        String password = commandLine.getOptionValue("password");
        String database = commandLine.getOptionValue("database");
        String tableName = commandLine.getOptionValue("tablename");

        SparkSession sparkSession =
                SparkSession.builder()
                        .master("local")
                        .appName("SparkDataFrameToHoloExample")
                        .config("spark.default.parallelism", 1)
                        .getOrCreate();

        List<Row> data =
                Arrays.asList(
                        RowFactory.create(
                                123L,
                                "Adam",
                                new BigDecimal("123.11"),
                                new Timestamp(System.currentTimeMillis())),
                        RowFactory.create(
                                234L,
                                "Bob",
                                new BigDecimal("000.11"),
                                new Timestamp(System.currentTimeMillis())));

        List<StructField> asList =
                Arrays.asList(
                        DataTypes.createStructField("user_id", DataTypes.LongType, true),
                        DataTypes.createStructField("user_name", DataTypes.StringType, true),
                        DataTypes.createStructField("price", new DecimalType(38, 2), true),
                        DataTypes.createStructField(
                                "sale_timestamp", DataTypes.TimestampType, true));

        StructType schema = DataTypes.createStructType(asList);

        Dataset<Row> df = sparkSession.createDataFrame(data, schema);

        df.write()
            .format("hologres")
            .option("username", userName)
            .option("password", password)
            .option("endpoint", endPoint)
            .option("database", database)
            .option("table", tableName)
            .mode(SaveMode.Append)
            .save();

        sparkSession.stop();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



hologres-connector-examples/hologres-connector-spark-examples/src/main/java/com/alibaba/hologres/spark/example/SparkWriteDataFrameToHoloExample.java [35:93]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    public static void main(String[] args) throws Exception {
        Options options = new Options();
        options.addOption("e", "endpoint", true, "Hologres endpoint");
        options.addOption("u", "username", true, "Username");
        options.addOption("p", "password", true, "Password");
        options.addOption("d", "database", true, "Database");
        options.addOption("t", "tablename", true, "Table name");

        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine = parser.parse(options, args);
        String endPoint = commandLine.getOptionValue("endpoint");
        String userName = commandLine.getOptionValue("username");
        String password = commandLine.getOptionValue("password");
        String database = commandLine.getOptionValue("database");
        String tableName = commandLine.getOptionValue("tablename");

        SparkSession sparkSession =
                SparkSession.builder()
                        .master("local")
                        .appName("SparkDataFrameToHoloExample")
                        .config("spark.default.parallelism", 1)
                        .getOrCreate();

        List<Row> data =
                Arrays.asList(
                        RowFactory.create(
                                123L,
                                "Adam",
                                new BigDecimal("123.11"),
                                new Timestamp(System.currentTimeMillis())),
                        RowFactory.create(
                                234L,
                                "Bob",
                                new BigDecimal("000.11"),
                                new Timestamp(System.currentTimeMillis())));

        List<StructField> asList =
                Arrays.asList(
                        DataTypes.createStructField("user_id", DataTypes.LongType, true),
                        DataTypes.createStructField("user_name", DataTypes.StringType, true),
                        DataTypes.createStructField("price", new DecimalType(38, 2), true),
                        DataTypes.createStructField(
                                "sale_timestamp", DataTypes.TimestampType, true));

        StructType schema = DataTypes.createStructType(asList);

        Dataset<Row> df = sparkSession.createDataFrame(data, schema);

        df.write()
                .format("hologres")
                .option("username", userName)
                .option("password", password)
                .option("endpoint", endPoint)
                .option("database", database)
                .option("table", tableName)
                .mode(SaveMode.Append)
                .save();

        sparkSession.stop();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



