Mysql configuration data in the table, look up data in other databases, and data into Json format and uploaded to the Hbase

1. Read the configuration information in a database table in two rows

2. Query disease information field configuration according to the configuration information

3. The data from the database query spliced ​​into Json format

4. The resulting data is inserted hbase, a single strip is inserted, and inserting bulk incremental insertion method zeng

The first step in the preparation method of connecting mysql mysql on and off, and when the information mysql query, the first to use the method to connect to the mysql.

The configuration information in the database, define List <String> variable format, configuration information is stored in the database, using information PrepareStament query the database, and then the results of the query are added to the list in the second step can be obtained. 

public List<String> getMysqlInfo(String id){
     ResultSet rs=null;
     List<String> list1=new ArrayList<String>();
     try {
         String sql="select * from MYSQL_TO_HBASE where id= ? ";
         ps=connection.prepareStatement(sql);
         ps.setString(1,id);
         rs =ps.executeQuery();  

The third step of the configuration information query to find information on the history of the disease, and into Json format data, add data to the List <List <String >> leix types of variables, two List, the first personal identification information stored List ID number, the second List stored into the history of disease information json format, when data needs to be removed into json staff identity card number with no history of disease information while using fastjson entity class object data into json data.

public List<List<String>> getDisease(String id){
     ResultSet rse=null;
     List<String> list= new ArrayList<String>();  //存放转为json格式的疾病史信息
     List<String> list1= new ArrayList<String>();   //存放有疾病史信息的身份证号码
     List<List<String>> list3= new ArrayList<List<String>>(); //存放最终得到的 <身份证号, 疾病史信息>
     try {
         statement=connection.createStatement();   //根据配置表信息的值查询 疾病史信息
         String sql1="select "+getMysqlInfo(id).get(8)+" from 数据库名称."+getMysqlInfo(id).get(6)+"";
         rse =statement.executeQuery(sql1);

The above part of the code information to obtain a human disease, however, a person can have multiple disease information, disease information to an entity encapsulated into a class, and adds the ID number, as the entity classes.

List<DisNT_Entity> list2= new ArrayList<DisNT_Entity>();//存放一条疾病史信息
String sfzhm=rse.getString(1);
String jbmc1=rse.getString(3);
String jbqznl1=rse.getString(4);

The name of the disease and the age of onset of disease which is applied to a solid object, date of onset of the disease process, using the ID number in the start date of birth Age plus manner to give the start time.

Integer.valueOf(sfzhm.substring(6, 10)) + Integer.valueOf(jbqznl1) + "-" + sfzhm.substring(10, 12)+ "-" +sfzhm.substring(12,14)

Determine whether a person has the disease to add the ID number using the flag, each time you create an entity class object seasonal flag = true. When present flag = true, the information will be obtained from the disease mysql json into the data format.

if(flag){
    String listJson=JSON.toJSON(list2).toString();
    list.add(listJson);
    list1.add(sfzhm);
}

Finally, add the two together list3 list data can be.

The fourth step of the preparation hbase connection method, closed, single insert, bulk inserting method, the data query hbase

//单条插入
public void insterRow(String tableName, String rowkey, String colFamily, String col, String val)
        throws IOException {
    Table table = connection.getTable(TableName.valueOf(tableName));
    Put put = new Put(Bytes.toBytes(rowkey));
    put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
    table.put(put);
    table.close();
}
//批量插入
public void insterRows(List<Put> putList,String tableName) {
    Table table;
    try {
        table = connection.getTable(TableName.valueOf(tableName));
        table.put(putList);
        table.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The fifth step with Mysql written methods and Hbase the method, batch query whether there Hbase in the identity card number of the user information, and then stitching the old information and information with a string, call the bulk insert method to insert information into the Hbase.

List<List<String>> list=mu.getDisease(id);
//查询已有标签
List<String> reList = hu.getDatas(list.get(0), "tags");
//批量插入
for(int k=0;k<list.get(0).size();k++){
    String jsonTmp1 = "";
    String jsonTmp2 = "";
    String jsonTmp3 = "";
    /*拼接Hbase中已有的字段内容和list中的数据
    Hbase中已有相同rowkey,则将字段赋值给jsonTemp1到倒数第二个字符,并添加逗号,将最后一个字符的]转为,以符合json格 * */
    if (null != reList.get(k) && !"".equals(reList.get(k))) {
        jsonTmp1 = reList.get(k).substring(0,reList.get(k).length()-1)+",";
        jsonTmp2 =  list.get(1).get(k).substring(1);
        jsonTmp3 = jsonTmp1 + jsonTmp2;
    }else {
        jsonTmp3 = list.get(1).get(k);
    }
    /*
    将list中第一个list的内容添加到put,即身份证号码
    将所有的疾病信息即jsonTmp3添加到put
    * */
    Put put = new Put(Bytes.toBytes(list.get(0).get(k)));
    put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("tags"), Bytes.toBytes(jsonTmp3));
    putlist.add(put);
}
//调用HbaseUtils中的批量插入方法,将数据插入到Hbase中
hu.insterRows(putlist,tablename);
Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/84504028