巨衫数据库 SequoiaDB-v3 SCDD认证答案(仅供参考)

第一章

1、创建表并插入数据

stmt = conn.createStatement();// Write sql

String sql = "CREATE TABLE employee_quiz " +

    "(" +

    "empno INT AUTO_INCREMENT PRIMARY KEY, " +

    "ename VARCHAR(128), " +

    "age INT" +

    ")";// Execute sql

stmt.executeUpdate(sql);

stmt = conn.createStatement();

// Write sql

String sql2 = "INSERT INTO employee_quiz VALUES (10001, 'George', 48),(10002, 'Bezalel', 21),(10006 ,'Anneke',19)";

// Execute sql to insert data into employee

stmt.executeUpdate(sql2);

2更新数据

stmt = conn.createStatement();// Write sql

String sql = "UPDATE employee_quiz SET age = 34 WHERE empno = '10001'";// Execute sql

stmt.executeUpdate(sql);

3删除数据

stmt = conn.createStatement();// Write sql

String sql = "DELETE FROM employee_quiz WHERE empno = '10006'";// Execute sql

stmt.executeUpdate(sql);

4创建视图

stmt = conn.createStatement();// Execute sql

stmt.executeUpdate("CREATE OR REPLACE VIEW employee_bak AS SELECT * FROM employee_quiz;");

5使用函数插入数据

stmt = conn.createStatement();

// Write sql

String sql2 = "INSERT INTO user VALUES (1,NOW(),PASSWORD(‘123456’))";

// Execute sql to insert data into employee

stmt.executeUpdate(sql2);

第二章

1建立表与索引

Sequoiadb db = new Sequoiadb("sdbserver1", 11810,"sdbadmin", "sdbadmin");

db.createCollectionSpace("company ");

CollectionSpace cs = db.getCollectionSpace("company ");

DBCollection cl = cs.createCollection("employee");

BasicBSONObject column = new BasicBSONObject();// Index contains fields

column.put("empno", 1);// Create a unique index.

cl.createIndex("idx_empno", column, true, false);

插入数据

List records = new ArrayList<>();

BasicBSONObject r1 = new BasicBSONObject();

r1.put("empno", 10001);

r1.put("ename", "Georgi");

r1.put("age", 48);

records.add(r1);

BasicBSONObject r2 = new BasicBSONObject();

r2.put("empno", 10002);

r2.put("ename", "Bezalel");

r2.put("age", 21);

records.add(r2);

cl.insert(records);

匹配条件并更新

BSONObject matcher = new BasicBSONObject();

BSONObject et = new BasicBSONObject();

et.put("$et", 10001);

matcher.put("empno", et);// Set the modified value.

BSONObject modifier = new BasicBSONObject();

BSONObject value = new BasicBSONObject();

value.put("age", 34);

modifier.put("$set", value);// Execute update operation.

cl.update(matcher, modifier, null);

插入列表型数据

BasicBSONObject record = new BasicBSONObject();// Add attributes for the personnel.

record.put("ename", "Mike");

record.put("empno", 10007);// Add an array type attribute for the personnel favorite.// favorite:[basketball,football]

BasicBSONList favorite = new BasicBSONList();

favorite.add("skiing");

favorite.add("swimming");// Insert the favorite array into the properties of the personnel record.

record.put("favorite", favorite);// Insert this record into the employee collection.

cl.insert(record);

第三章

1

获取链接

AWSCredentials credentials =

            new BasicAWSCredentials("ABCDEFGHIJKLMNOPQRST",

                "abcdefghijklmnopqrstuvwxyz0123456789ABCD");

    String endPoint = "http://127.0.0.1:8002";

    AwsClientBuilder.EndpointConfiguration endpointConfiguration =

        new AwsClientBuilder.EndpointConfiguration(endPoint, null);

    //Create the S3 connection object

    s3 = AmazonS3ClientBuilder.standard()

        .withEndpointConfiguration(endpointConfiguration)

        .withCredentials(

        new AWSStaticCredentialsProvider(credentials)

        ).build();

创建存储桶

s3.createBucket("novelbucket");

2

统一放在一个方法里即可

把以下代码,粘贴到LobQuiz.java的public void createCS方法的TODO和TODO END之间,运行即可;

Sequoiadb db = new Sequoiadb("sdbserver1", 11810, "", "");

CollectionSpace cs = db.createCollectionSpace("school");//Get collection object

        BasicBSONObject mainOptions = new BasicBSONObject();

        mainOptions.put("ShardingKey",new BasicBSONObject("date",1));

        mainOptions.put("ShardingType", "range");//Claim the main collection

        mainOptions.put("IsMainCL", true);

        mainOptions.put("LobShardingKeyFormat","YYYYMMDD");

        DBCollection maincl = cs.createCollection("student",mainOptions);

        BasicBSONObject subOptions = new BasicBSONObject();//Set the partition key

        subOptions.put("ShardingKey",new BasicBSONObject("sid",1));//Set the partition method

        subOptions.put("ShardingType", "hash");

        DBCollection subCL1 = cs.

                createCollection("student_202004", subOptions);

        DBCollection subCL2 = cs.

                createCollection("student_202005", subOptions);

        BasicBSONObject attachOptions = new BasicBSONObject();

        attachOptions.put("LowBound", new BasicBSONObject("date", "20200401"));

        attachOptions.put("UpBound", new BasicBSONObject("date", "20200501"));//Get the main table object

        BasicBSONObject attachOptions2 = new BasicBSONObject();

        attachOptions2.put("LowBound", new BasicBSONObject("date", "20200501"));

        attachOptions2.put("UpBound", new BasicBSONObject("date", "20200601"));//Get the main table object

        maincl.attachCollection("school.student_202004", attachOptions);

        maincl.attachCollection("school.student_202005", attachOptions2);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date1 = sdf.parse("2020-04-05");

        ObjectId lobID = maincl.createLobID(date1);

        DBLob lob = maincl.createLob(lobID);

        FileInputStream fileInputStream = new FileInputStream("/home/sdbadmin/sequoiadb.txt" );//Write data to the Lob

        lob.write(fileInputStream);//Close the Lob

        lob.close();

        DBCursor dbCursor = maincl.listLobs();

        while(dbCursor.hasNext()){

            BSONObject record = dbCursor.getNext();

            System.out.println(record.toString());

        }

MappingCollection.java

Connection connection2 = DriverManager.getConnection(

                "jdbc:hive2://sdbserver1:10000/quiz",// Hive JDBC connection url

                "sdbadmin",// Hive JDBC connection user name

                ""// Hive JDBC connection password (authentication is not enabled by default)

        );

        // Create Statement

        Statement statement = connection2.createStatement();

        // Drop the existing employee table

        String dropTable =

                "DROP TABLE IF EXISTS quiz.employee1";

        // Execute the SQL statement of drop table

        statement.execute(dropTable);

        String dropTable2 =

                "DROP TABLE IF EXISTS quiz.employee2";

        // Execute the SQL statement of drop table

        statement.execute(dropTable2);

        // Create a mapping table for the employee collection

        String mappingTable =

                "CREATE TABLE quiz.employee1 " +

                        "USING com.sequoiadb.spark  " +

                        "OPTIONS( " +

                        "host 'sdbserver1:11810', " +

                        "collectionspace 'quiz', " +

                        "collection 'employee1', " +

                        "user 'sdbadmin'," +

                        "password 'sdbadmin'" +

                        ")";

        String mappingTable2 =

                "CREATE TABLE quiz.employee2 " +

                        "USING com.sequoiadb.spark  " +

                        "OPTIONS( " +

                        "host 'sdbserver1:11810', " +

                        "collectionspace 'quiz', " +

                        "collection 'employee2', " +

                        "user 'sdbadmin'," +

                        "password 'sdbadmin'" +

                        ")";

        // Execute the SQL statement of create mapping table

        statement.execute(mappingTable);

        statement.execute(mappingTable2);

CreateDatabase.java

try {

            // Load Hive JDBC driver

            Class.forName("org.apache.hive.jdbc.HiveDriver");

            // Create Hive JDBC connection

            Connection connection = DriverManager.getConnection(

                    url,// Hive JDBC connection url

                    "sdbadmin",// Hive JDBC connection user name

                    ""// Hive JDBC connection password (authentication is not enabled by default)

            );

            Statement statement = null;

// Initialize ResultSet

            ResultSet resultSet = null;

            try {

                // SQL statement of create database

                String createDatabaseSQL = "CREATE DATABASE IF NOT EXISTS quiz";

                // Create Statement

                statement = connection.createStatement();

                // Execute the SQL statement of create database

                statement.execute(createDatabaseSQL);

                statement = null;

// Initialize ResultSet

                resultSet = null;

                // TODO END

            } catch (SQLException e) {

                e.printStackTrace();

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

}

CountEmployee.java

Connection connection2 = DriverManager.getConnection(

                "jdbc:hive2://sdbserver1:10000/quiz",// Hive JDBC connection url

                "sdbadmin",// Hive JDBC connection user name

                ""// Hive JDBC connection password (authentication is not enabled by default)

        );

        // Create Statement

        Statement statement = connection2.createStatement();

        // Drop the existing employee table

        String mappingTable2 =

                "create table quiz.employee_count "+

                        "USING com.sequoiadb.spark  " +

                        "OPTIONS( " +

                        "host 'sdbserver1:11810', " +

                        "collectionspace 'quiz', " +

                        "collection 'employee_count', " +

                        "user 'sdbadmin'," +

                        "password 'sdbadmin'" +

                        ")"+

                        "as select sex,count(*) as num from quiz.employee1 group by sex";

        statement.execute(mappingTable2);

BulkInsert.java

Connection connection2 = DriverManager.getConnection(

                "jdbc:hive2://sdbserver1:10000/quiz",// Hive JDBC connection url

                "sdbadmin",// Hive JDBC connection user name

                ""// Hive JDBC connection password (authentication is not enabled by default)

        );

        // Create Statement

        Statement statement = connection2.createStatement();

        // Drop the existing employee table

        String inserts = "insert into employee1 select * from employee2";

        // Execute the SQL statement of create mapping table

        statement.execute(inserts);

        // TODO END

}

五 直接粘贴即可

package com.sequoiadb.lesson.flink.quiz;

import com.chaoc.flink.streaming.connectors.SequoiadbSink;

import com.chaoc.flink.streaming.connectors.SequoiadbSource;

import com.chaoc.flink.streaming.connectors.option.SequoiadbOption;

import com.sequoiadb.lesson.flink.common.FlinkQuizCheck;

import com.sequoiadb.lesson.flink.common.Init;

import org.apache.flink.api.common.JobExecutionResult;

import org.apache.flink.api.common.functions.MapFunction;

import org.apache.flink.api.java.functions.KeySelector;

import org.apache.flink.api.java.tuple.Tuple;

import org.apache.flink.api.java.tuple.Tuple2;

import org.apache.flink.api.java.tuple.Tuple3;

import org.apache.flink.streaming.api.datastream.*;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import org.apache.flink.streaming.api.functions.windowing.WindowFunction;

import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

import org.apache.flink.util.Collector;

import org.bson.BSONObject;

import org.bson.BasicBSONObject;

import org.bson.types.BSONDecimal;

import java.util.Iterator;

/**

 * 考试

 */

public class AssignmentMain {

    /**

     * 请在此处添加数据源

     *

     * 通过使用 SequoiadbSource 将巨杉数据库中的集合作为数据源添加到执行环境

     * @param env 流执行环境

     * @return

     */

    private static DataStream source(StreamExecutionEnvironment env) {

        DataStreamSource dataSource = null;

        // TODO code 1

        SequoiadbOption option = SequoiadbOption.bulider()

                .host("localhost:11810")

                .username("sdbadmin")

                .password("sdbadmin")

                .collectionSpaceName("VIRTUAL_BANK")

                .collectionName("TRANSACTION_FLOW")

                .build();

        // Add a data source to the current environment (SequoiadbSource needs to build a stream through the time field "create_time")

        dataSource = env.addSource(new SequoiadbSource(option, "create_time"));

        // TODO END

        return dataSource;

    }

    /**

     * 请在此处实现数据类型转换

     *

     * 通过 map 算子将 BSONObject 转换为 Tuple3 类型

     * @param transData 原始数据集

     * @return

     */

    private static DataStream> map(DataStream transData) {

        DataStream> resultData = null;

        // TODO code 2

        resultData = transData.map(new MapFunction

                Tuple3>() {

            /**

             * Execute on every event

             * @param object Original event

             * @return

             * @throws Exception

             */

            @Override

            public Tuple3 map(BSONObject object)

                    throws Exception {

                // Extract the required fields

                return Tuple3.of(object.get("trans_name").toString(),                                  ((BSONDecimal) object.get("money")).toBigDecimal().doubleValue(), 1);

            }

        });

        // TODO END

        return resultData;

    }

    /**

     * 请在此处按照 Tuple3 中的第一个字段进行分组

     *

     * 通过 keyBy 算子完成分组操作

     * @param moneyData 类型转换后的数据集

     * @return

     */

    private static KeyedStream, Tuple> keyBy(DataStream

            Double, Integer>> moneyData) {

        KeyedStream, Tuple> resultData = null;

        // TODO code 3

        resultData = moneyData.keyBy(0);

        // TODO END

        return resultData;

    }

    /**

     * 请在此处完成开窗

     *

     * 通过 countWindow 算子对数据集进行开窗,此处请使用滑动计数窗口,窗口大小为 100,滑动步长为 50

     * @param keyedData 分组后的数据集

     * @return

     */

    private static WindowedStream, Tuple, GlobalWindow> countWindow(

            KeyedStream, Tuple> keyedData) {

        WindowedStream, Tuple, GlobalWindow> resultData = null;

        // TODO code 4

        resultData = keyedData.countWindow(100, 50);

        // TODO END

        return resultData;

    }

    /**

     * 请在此处统计每个窗口内的每种交易的交易总额和交易次数

     *

     * 通过 apply 算子迭代遍历所有事件完成交易总额与交易次数的统计

     * @param countWindow 开窗后的数据集

     * @return

     */

    private static DataStream> reduce(WindowedStream,

            Tuple, GlobalWindow> countWindow) {

        DataStream> resultData = null;

        // TODO code 5

        resultData = countWindow.apply(new WindowFunction, Tuple2, Tuple, GlobalWindow>() {

            /**

             * Execute when the window meets the conditions, which similar to the flatMap operator

             * @param tuple Group field value. Since the subscript was used for grouping, the specific data type cannot be obtained, so the Tuple abstract representation is used here.

             * @param globalWindow Global window reference

             * @param iterable References to all data sets in the current window

             * @param collector Result collector

             * @throws Exception

             */

            @Override

            public void apply(Tuple tuple, GlobalWindow globalWindow, Iterable> iterable,

                              Collector> collector) throws Exception {

                double sum = 0;

                Iterator> iterator = iterable.iterator();

                while (iterator.hasNext()) {

                    sum += iterator.next().f1;

                }

                collector.collect(Tuple2.of(tuple.getField(0), sum));

            }

        });

        // TODO END

        return resultData;

    }

    /**

     * 请将数据格式转换为 BSONObject

     *

     * 通过 map 算子将 Tuple2 转换为 BSONObject

     * @param dataStream 聚合后的结果数据集

     * @return

     */

    private static DataStream toBson(DataStream> dataStream) {

        DataStream bsonData = null;

        // TODO code 6

        bsonData = dataStream.map(new MapFunction, BSONObject>() {

            @Override

            public BSONObject map(Tuple2 value) throws Exception {

                BasicBSONObject obj = new BasicBSONObject();

                obj.append("trans_name", value.f0);

                obj.append("total_sum", value.f1);

                return obj;

            }

        });

        // TODO END

        return bsonData;

    }

    /**

     * 请向结果 DataStream 中添加 Sink

     *

     * 通过添加 SequoiadbSink 将结果写入到 SequoiaDB

     * @param dataStream 类型转换为 BSONObject 的数据集

     * @return

     */

    private static DataStreamSink sink(DataStream dataStream) {

        DataStreamSink streamSink = null;

        // TODO code 7

        SequoiadbOption option = SequoiadbOption.bulider()

                .host("localhost:11810")

                .username("sdbadmin")

                .password("sdbadmin")

                .collectionSpaceName("VIRTUAL_BANK")

                .collectionName("ASSIGNMENT")

                .build();

        streamSink = dataStream.addSink(new SequoiadbSink(option));

        // TODO END

        return streamSink;

    }

    /**

     * 程序主干,严禁修改

     *

     * @param args 参数

     * @throws Exception

     */

    public static void main(String[] args) throws Exception {

        Init.initCollection(true, "ASSIGNMENT_CHECK", "ASSIGNMENT");

        // 获取执行环境

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 通过数据源读入数据

        DataStream transSource = source(env);

        // 对数据进行map转换

        DataStream> moneyData = map(transSource);

        FlinkQuizCheck.metricTuple3(moneyData);

        // 数据分组

        KeyedStream, Tuple> keyedData = keyBy(moneyData);

        FlinkQuizCheck.metricKeyed(keyedData);

        // 通过window进行分组

        WindowedStream, Tuple, GlobalWindow> countWindow = countWindow(keyedData);

        FlinkQuizCheck.metricWindow(countWindow);

        // 聚合求结果

        DataStream> sumData = reduce(countWindow);

        // 转换结果为BsonObject

        DataStream bsonDataStream = toBson(sumData);

        // 将结果写入sequoiadb

        sink(bsonDataStream);

        // 执行流作业

        JobExecutionResult executionResult = env.execute("flink window");

        FlinkQuizCheck.metricSave(executionResult);

    }

}

你可能感兴趣的:(数据库,sequoiadb,sql)