mysql中的数据导入到hbase中,并关联phoenix

1.在hbase上创建表:

hbase>create 'ES','f1'

2.phoenix上创建表:

 jdbc:phoenix:es01> create table ES(ids varchar primary key ,"f1"."class_name" varchar,"f1"."exam_id" varchar,"f1"."examinee_name" varchar,"f1"."examinee_num" varchar,"f1"."part_question_mark" varchar,"f1"."question_difficulty" varchar,"f1"."question_id" varchar,"f1"."question_type" varchar,"f1"."score" varchar,"f1"."start_time" varchar,"f1"."category_name" varchar);

3.将mysql中的数据导入到hbae中:

/**
 * 将mysql中的数据导入到hbase中
 */
public class MySQL2HBASE {
    private static Table table;

    static {
        try {
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "es01:2181,es02:2181,es03:2181");
            Connection conn = ConnectionFactory.createConnection(configuration);
            table = conn.getTable(TableName.valueOf("ES"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JDBCHelper jdbc = JDBCHelper.getInstance();
        jdbc.executeQuery("select * from basic_xiangqing", new Object[]{}, new JDBCHelper.QueryCallback() {
            @Override
            public void process(ResultSet res) throws Exception {
                int i = 0;
                while (res.next()) {
                    int exam_id = res.getInt(1);
                    String start_time = res.getString(2);
                    String class_name = res.getString(3);
                    String examinee_num = res.getString(4);
                    String examinee_name = res.getString(5);
                    String question_id = res.getString(6);
                    String category_name = res.getString(7);
                    String question_difficulty = res.getString(8);
                    String question_type = res.getString(9);
                    String part_question_mark = res.getString(10);
                    String score = res.getString(11);
                    String rowKey = examinee_num + "," + exam_id + "," + question_id;
                    Put put = new Put(Bytes.toBytes(rowKey));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("exam_id"),Bytes.toBytes(exam_id));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("start_time"),Bytes.toBytes(start_time));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("class_name"),Bytes.toBytes(class_name));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("examinee_num"),Bytes.toBytes(examinee_num));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("examinee_name"),Bytes.toBytes(examinee_name));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("question_id"),Bytes.toBytes(question_id));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("category_name"),Bytes.toBytes(category_name));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("question_difficulty"),Bytes.toBytes(question_difficulty));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("question_type"),Bytes.toBytes(question_type));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("part_question_mark"),Bytes.toBytes(part_question_mark));
                    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("score"),Bytes.toBytes(score));
                    table.put(put);
                    Thread.sleep(10);
                    System.out.println(i);
                    i++;
                }
                table.close();
            }
        });
    }
}

你可能感兴趣的:(大数据)