当前位置 : 主页 > 编程语言 > c++ >

表格存储 相关的使用示例

来源:互联网 收集:自由互联 发布时间:2021-07-03
表格存储 client 创建 package com.mimidai.common.utils.table;import com.alicloud.openservices.tablestore.ClientConfiguration;import com.alicloud.openservices.tablestore.SyncClient;import com.alicloud.openservices.tablestore.model.Alway
表格存储 client 创建
package com.mimidai.common.utils.table;

import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import com.mimidai.common.utils.PropertiesUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建表格存储client Created by EVE on 2017-06-22.
 */
@Configuration
//@PropertySource("classpath:application-develop.properties")
public class TableClientConfig {

    private static Logger logger = LoggerFactory.getLogger("log.hbase.TableClientConfig");
    //  @Value("${TableClientConfig.endPoint}")
    //  private String endPoint;
    //  @Value("${TableClientConfig.accessId}")
    //  private String accessId;
    //  @Value("${TableClientConfig.accessKey}")
    //  private String accessKey;
    //  @Value("${TableClientConfig.instanceName}")
    //  private String instanceName;

    //生产配置  start
    //内网地址
    // private String endPoint = "http://192.168.3.17:80/";
    // private String accessId = "XXXX";
    // private String accessKey = "XXXXXXXXXXXXXXX";
    // private String instanceName = "xxx";
    //生产配置  end

    //测试 环境 start
    //内网地址
    // private String endPoint = "http://xxxxxxxxx/";
    //外网地址
    // private static final String endPoint = "http://xxx-test.cn-beijing.ots.aliyuncs.com";
    // private String accessId = "XXXXXXXXXXXX";
    // private String accessKey = "XXXXXXXXXXXXXXXXXXX";
    // private String instanceName = "test";

    private static final String endPoint = PropertiesUtils.getProperties().getProperty("table_client_end_point");
    private static final String accessId = PropertiesUtils.getProperties().getProperty("table_client_access_id");
    private static final String accessKey = PropertiesUtils.getProperties().getProperty("table_client_access_key");
    private static final String instanceName = PropertiesUtils.getProperties().getProperty
            ("table_client_instance_name");


    //测试  换将 end
    @Bean
    public SyncClient createClient() {
        logger.info("开始调用createClient方法");
        // ClientConfiguration提供了很多配置项,以下只列举部分。
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        // 设置建立连接的超时时间。
        clientConfiguration.setConnectionTimeoutInMillisecond(5000);
        // 设置socket超时时间。
        clientConfiguration.setSocketTimeoutInMillisecond(5000);
        // 设置重试策略,若不设置,采用默认的重试策略。
        clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy());
        logger.info("返回创建表格存储cleint");
        return new SyncClient(endPoint, accessId, accessKey, instanceName, clientConfiguration);
    }

}
表格存储 接口
package com.mimidai.common.dao.table;

import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.mimidai.common.dto.SolrSmsDetails;
import com.mimidai.common.utils.persistence.Page;

import java.util.List;

/**
 * Created by EVE on 2017-06-22.
 */
public interface TableSolrSmsDetailsDao {

    void add(List
 
   solrSmsDetailses);

    Page
  
    query(SolrSmsDetails solrSmsDetails, Page
   
     page); List
    
      query(List
     
       solrSmsDetails); // Boolean updateRow(List
      
        solrSmsDetails); void updateRow(List
       
         solrSmsDetails, String column); PrimaryKey createPrimaryKey(List
        
          solrSmsDetails); // Boolean putRow(List
         
           solrSmsDetails); void putRow(List
          
            solrSmsDetails, String column); List
           
             query(List
            
              solrSmsDetails, String column); }
            
           
          
         
        
       
      
     
    
   
  
 
表格存储 接口的实现类
package com.mimidai.common.utils.table;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.BatchWriteRowRequest;
import com.alicloud.openservices.tablestore.model.Column;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.GetRowRequest;
import com.alicloud.openservices.tablestore.model.GetRowResponse;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.Row;
import com.alicloud.openservices.tablestore.model.RowPutChange;
import com.alicloud.openservices.tablestore.model.RowUpdateChange;
import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;
import com.alicloud.openservices.tablestore.model.UpdateRowRequest;
import com.mimidai.common.dao.table.TableSolrSmsDetailsDao;
import com.mimidai.common.dao.user.UserPhoneCheckDao;
import com.mimidai.common.dao.user.UserPhoneCheckHistoryDao;
import com.mimidai.common.dto.SolrSmsDetails;
import com.mimidai.common.pay.MD5Util;
import com.mimidai.common.utils.persistence.Page;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * core1 短信接口 表结构如下 主键				details_queryTime1		details_queryTime	…. userId	phone	json(最多2M) json
 * (最多2M) ... userId	phone
 * json(最多2M)	            json(最多2M)        ... ...
 *
 * Created by EVE on 2017-06-22.
 */
@Repository
public class TableSolrSmsDetailsDaoImpl implements TableSolrSmsDetailsDao {

    @Autowired
    @Qualifier("createClient")
    private SyncClient client;
    private final String TABLENAME = "smsDetails";
    @Autowired
    private UserPhoneCheckDao userPhoneCheckDao;
    @Autowired
    private UserPhoneCheckHistoryDao userPhoneCheckHistoryDao;
    //日志
    private static final Logger logger = LoggerFactory.getLogger("log.tableStore.TableSolrSmsDetailsDaoImpl");

    /**
     * 逻辑变更  暂时注释 请勿删除
     *
     * 添加非空用户到表格存储 每次只查一个用户 一个电话 相当于(user+phone唯一)
     * 添加逻辑 需要判断
     * 1.该条信息是否在表格存储中已经存在,如果存在,新传入的solrSmsDetailses 只包含增量数据,需要做增量操作(先查再将两处结果集合并)
     * 2.该条信息之前没有,直接添加即可
     */
    // @Override
    // public void add(List
 
   solrSmsDetailses) {
    //     logger.debug("进入tableStore(core1)添加方法add");
    //     if (CollectionUtils.isEmpty(solrSmsDetailses) || solrSmsDetailses.get(0).getPhone() == null ||
    // solrSmsDetailses.get(0).getUserId() == null) {
    //         logger.warn("进入tableStore(core1)添加方法add参数存在空值,直接返回");
    //         return;
    //     }
    //     logger.debug("tableStore(core1)添加方法add调用查询query方法");
    //     List
  
    tableSmsDetailsList = query(solrSmsDetailses); // if (CollectionUtils.isNotEmpty(tableSmsDetailsList)) { // long startTime = System.currentTimeMillis(); // List list = compare(tableSmsDetailsList, solrSmsDetailses); // logger.info("tableStore(core1)添加方法add调用更新方法updateRow,查询tableSmsDetailsList={},传参solrSmsDetailses={}, // 去重list={}", tableSmsDetailsList.size(), solrSmsDetailses.size(), list.size()); // updateRow(list); // long endTime = System.currentTimeMillis(); // float excTime = (float) (endTime - startTime) / 1000; // logger.info("tableStore(core1)添加方法add调用更新方法updateRow方法执行成功!,excTime={}", excTime, getUserIdAndPhone // (list)); // } else { // long startTime = System.currentTimeMillis(); // logger.debug("tableStore(core1)添加方法add调用新增方法putRow,solrSmsDetailses={}", solrSmsDetailses.size()); // long endTime = System.currentTimeMillis(); // float excTime = (float) (endTime - startTime) / 1000; // putRow(solrSmsDetailses); // logger.info("tableStore(core1)添加方法add调用新增方法putRow执行成功!excTime={}", excTime, getUserIdAndPhone // (solrSmsDetailses)); // } // } /** * 根据每次申请认证时间 (user_phone_check表获取时间) queryTime1 每次均取该用户最大id的queryTime1 上一次申请认证时间(user_phone_check_history表获取时间) * queryTime2 取该用户最大id的queryTime 两次时间列的数据进行对比 筛选出前一次没有的数据 插入到对应列 (details_queryTime) * * 判断 插入/更新 user_phone_check_history表中无该用户信息(状态为done) 即为插入 user_phone_check_history表中有该用户信息(状态为done), * 根据queryTime去重之后再插入 * * user_phone_check 中有三个状态 done(运营商返回数据) processing(正在认证,这个状态保持一天以后才会保存到history) failed 认证失败 */ @Override public void add(List
   
     solrSmsDetailses) { logger.debug("进入tableStore(core1)添加方法add"); if (CollectionUtils.isEmpty(solrSmsDetailses) || solrSmsDetailses.get(0).getPhone() == null || solrSmsDetailses.get(0).getUserId() == null) { logger.warn("进入tableStore(core1)添加方法add参数存在空值,直接返回"); return; } logger.debug("tableStore(core1)添加方法add调用查询query方法"); long startTime = System.currentTimeMillis(); logger.debug("tableStore(core1)添加方法add调用新增方法putRow,solrSmsDetailses={}", solrSmsDetailses.size()); Long userId = solrSmsDetailses.get(0).getUserId(); String phone = solrSmsDetailses.get(0).getPhone(); Date date1 = userPhoneCheckHistoryDao.getUserPhoneCheckHistoryQueryTime(userId, phone); Date date2 = userPhoneCheckDao.findNewQueryTimeByUserIdPhone(userId, phone); if (date2 == null) { logger.warn("user_phone_check表中query_time为空,该条数据存在异常,userId={}", userId); return; } if (date1 == null) { String queryTime2 = date2.getTime() + ""; String column = "details_" + queryTime2; logger.info("用户userId={}第一次保存信息到表格存储,本次信息全量存储", userId); updateRow(solrSmsDetailses, column); logger.info("用户userId={}第一次保存信息到表格存储,本次信息全量存储完成", userId); } else { Long queryTime = date1.getTime(); String queryTime2 = date2.getTime() + ""; String column = "details_" + queryTime2; List
    
      solrSmsDetailsList = new ArrayList<>(); logger.info("用户userId=" + userId + "之前已有信息在表格存储中保存,本次将做增量(" + logTimeConvert(date1) + "到" + logTimeConvert(date2) + ")存储到列族column=" + column); for (SolrSmsDetails solrSmsDetails : solrSmsDetailses) { Long dateTime = toCoverDate(solrSmsDetails.getDate()).getTime(); //保存从上一次历史数据到本次存储时间之内的数据 if (dateTime > queryTime) { solrSmsDetailsList.add(solrSmsDetails); } } // putRow(solrSmsDetailsList, column); if (CollectionUtils.isEmpty(solrSmsDetailsList)) { logger.warn("当前用户无最新短信信息需要保存"); } else { logger.info("用户userId=" + userId + "Md5UserId=" + MD5Util.md5Hex(userId + "") + "电话phone={}" + phone + "开始调用updateRow方法 " + "solrSmsDetailsList={}" + solrSmsDetailsList.size()); updateRow(solrSmsDetailsList, column); logger.info("用户userId=" + userId + "调用updateRow方法完成"); } } long endTime = System.currentTimeMillis(); float excTime = (float) (endTime - startTime) / 1000; logger.info("tableStore(core1)添加方法add调用新增方法putRow执行成功!excTime={}", excTime + "用户信息:" + getUserIdAndPhone (solrSmsDetailses)); } /** * 分页查询,由于查询单人,只需要使用getRow即可该方法已实现分页,无需在后续再分页 * * @return page */ @Override public Page
     
       query(SolrSmsDetails solrSmsDetails, Page
      
        page) { logger.debug("进入tableStore(core1)查询Page方法query"); long startTime = System.currentTimeMillis(); // 首先对入参进行判断 若为空直接返回 不再查询 if (page == null) { logger.warn("表格存储短信详单(core1)PageQuery查询入参存在空值,不予查询"); return null; } // 每页放置多少条 int pageSize = page.getPageSize(); // 当前页数 int pageNo = page.getPageNo(); // 用于记录查询记录数 int pageCount = 0; // 用于截取List int fromIndex = 0; int toIndex; // 将该对象填装到List中 List
       
         solrSmsDetailsList = new ArrayList<>(); solrSmsDetailsList.add(solrSmsDetails); logger.debug("开始tableStore分页查询方法query调用,入参solrSmsDetailsList={}", solrSmsDetailsList.size()); List
        
          smsDetailsList = query(solrSmsDetailsList); logger.debug("tableStore分页查询方法query调用完成,查询结果smsDetailsList={}是否为空", CollectionUtils.isEmpty(smsDetailsList)); // 获取 details列详细信息 if (CollectionUtils.isNotEmpty(smsDetailsList)) { logger.debug("tableStore分页查询方法query调用query方法有数据smsDetailsList={}", smsDetailsList.size(), "返回,开始进行分页"); pageCount = smsDetailsList.size(); if (pageNo * pageSize > pageCount) { logger.warn("选择当前页大于总记录,总记录数=", pageCount, "当前页=", pageNo, "pageSize=", pageSize, "将返回一个空集合"); // 传递一个空的结果list page.setResult(new ArrayList<>()); // 未查到值,那么总记录数就是0 page.setTotalCount(pageCount); return page; } // 在方法中进行分页,对查出的全部数据进行分页处理 //fromIndex = (pageNo - 1) * pageSize; if (pageCount > 0) { fromIndex = (pageNo - 1) * pageSize < pageCount ? (pageNo - 1) * pageSize : (pageCount - pageSize > 0 ? pageCount : 0); } // toIndex = (pageNo * pageSize > pageCount ? pageCount : pageNo * pageSize); toIndex = smsDetailsList.size(); smsDetailsList = smsDetailsList.subList(fromIndex, toIndex); page.setResult(smsDetailsList); page.setTotalCount(pageCount); long endTime = System.currentTimeMillis(); float excTime = (float) (endTime - startTime) / 1000; logger.info("tableStore分页查询方法query调用query方法有数据返回,分页参数设置成功返回smsDetailsList={},执行时间excTime={}", smsDetailsList.size(), excTime, getUserIdAndPhone(smsDetailsList)); return page; } else { logger.info("tableStore分页查询方法query调用query方法无数据返回,将向后端返回一个空集合", getUserIdAndPhone(smsDetailsList)); // 传递一个空的结果list page.setResult(new ArrayList<>()); // 未查到值,那么总记录数就是0 page.setTotalCount(pageCount); return page; } } /** * 查询通用方法 * * @return resultList */ @Override public List
         
           query(List
          
            solrSmsDetails) { return getSolrSmsDetails(solrSmsDetails, "details"); } @Override public List
           
             query(List
            
              solrSmsDetails, String columnName) { return getSolrSmsDetails(solrSmsDetails, columnName); } private List
             
               getSolrSmsDetails(List
              
                solrSmsDetails, String COLUMNNAME) { logger.debug("进入tableStore(core1)查询List方法query"); long startTime = System.currentTimeMillis(); if (CollectionUtils.isEmpty(solrSmsDetails) || solrSmsDetails.get(0).getPhone() == null || solrSmsDetails.get (0).getUserId() == null) { logger.warn("表格存储短信详单(core1)查询ListQuery入参存在空值,不予查询"); return null; } List
               
                 resultList = new ArrayList<>(); PrimaryKey primaryKey = createPrimaryKey(solrSmsDetails); // 读取短信表 该用户-电话对应的该行数据 SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLENAME, primaryKey); // 设置读取最新版本 criteria.setMaxVersions(1); Row row; try { // 根据条件建立请求 GetRowRequest getRowRequest = new GetRowRequest(criteria); // 返回请求数据 GetRowResponse getRowResponse = client.getRow(getRowRequest); row = getRowResponse.getRow(); logger.info("tableStore(core1)查询方法query成功返回row", getUserIdAndPhone(solrSmsDetails)); } catch (Exception e) { e.printStackTrace(); logger.warn(getUserIdAndPhone(solrSmsDetails), "tableStore(core1)查询异常:" + e); return null; } // 获取 details列详细信息 if (row != null) { List
                
                  detailsList = row.getColumn(COLUMNNAME); // 实际情况应该是detailsList长度为1的List for (Column column : detailsList) { // Gson gson = new Gson(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); // 将查询结果转化为List
                 
                   List
                  
                    columnList = gson.fromJson(column.getValue().toString(), new TypeToken
                   
                    >() {}.getType()); resultList.addAll(columnList); } } long endTime = System.currentTimeMillis(); float excTime = (float) (endTime - startTime) / 1000; logger.info("tableStore(core1)查询方法query填装List成功,resultList={}是否为空,query方法体执行时间excTime:", CollectionUtils .isEmpty(resultList), excTime, getUserIdAndPhone(solrSmsDetails)); return resultList; } /** * 更新通用方法 */ // @Override // public Boolean updateRow(List
                    
                      solrSmsDetails) { // logger.debug("进入tableStore(core1)更新通用方法updateRow"); // // 构造主键 // PrimaryKey primaryKey = createPrimaryKey(solrSmsDetails); // // Gson gson = new Gson(); // Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); // try { // RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLENAME, primaryKey); // // 删除该列 // // rowUpdateChange.deleteColumns(COLUMNNAME); // rowUpdateChange.put(COLUMNNAME, ColumnValue.fromString(gson.toJson(solrSmsDetails))); // logger.debug("开始执行tableStore(core1)更新通用方法updateRow"); // client.updateRow(new UpdateRowRequest(rowUpdateChange)); // logger.debug("tableStore(core1)更新通用方法updateRow调用成功", getUserIdAndPhone(solrSmsDetails)); // return true; // } catch (Exception e) { // e.printStackTrace(); // logger.info(getUserIdAndPhone(solrSmsDetails), "tableStore(core1)更新通用方法updateRow调用失败" + e); // return false; // } // } @Override public void updateRow(List
                     
                       solrSmsDetails, String column) { logger.debug("进入tableStore(core1)更新通用方法updateRow"); // 构造主键 PrimaryKey primaryKey = createPrimaryKey(solrSmsDetails); // Gson gson = new Gson(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); try { RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLENAME, primaryKey); // 删除该列 // rowUpdateChange.deleteColumns(COLUMNNAME); rowUpdateChange.put(new Column(column, ColumnValue.fromString(gson.toJson(solrSmsDetails)))); logger.debug("开始执行tableStore(core1)更新通用方法updateRow"); client.updateRow(new UpdateRowRequest(rowUpdateChange)); logger.debug("tableStore(core1)更新通用方法updateRow调用成功", getUserIdAndPhone(solrSmsDetails)); } catch (Exception e) { e.printStackTrace(); logger.info(getUserIdAndPhone(solrSmsDetails), "tableStore(core1)更新通用方法updateRow调用失败" + e); } } /** * 用于生成构造主键 * * @return primaryKey */ @Override public PrimaryKey createPrimaryKey(List
                      
                        solrSmsDetails) { logger.debug("进入tableStore(core1)生成构造主键通用方法createPrimaryKey"); // 首先对入参进行判断 若为空直接返回 不再查询 if (CollectionUtils.isEmpty(solrSmsDetails) || solrSmsDetails.get(0).getUserId() == null || solrSmsDetails .get(0).getPhone() == null) { logger.warn("表格存储短信详单(core1)createPrimaryKey入参存在空值,不予查询"); return null; } // 获取入参 phone user_id 构造查询主键使用 Long userIds = solrSmsDetails.get(0).getUserId(); String userId = MD5Util.md5Hex(userIds + ""); String phone = solrSmsDetails.get(0).getPhone(); // 构造主键 PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("userId", PrimaryKeyValue.fromString(userId)); primaryKeyBuilder.addPrimaryKeyColumn("phone", PrimaryKeyValue.fromString(phone)); logger.info("tableStore(core1)生成构造主键通用方法createPrimaryKey:phone={},userId={},哈希之前userIds={}", phone, userId, userIds); return primaryKeyBuilder.build(); } // @Override // public Boolean putRow(List
                       
                         solrSmsDetails) { // logger.debug("进入tableStore(core1)新增方法putRow"); // BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest(); // // Gson gson = new Gson(); // Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); // try { // RowPutChange rowPutChange = new RowPutChange(TABLENAME, createPrimaryKey(solrSmsDetails)); // rowPutChange.addColumn(new Column(COLUMNNAME, ColumnValue.fromString(gson.toJson(solrSmsDetails)))); // batchWriteRowRequest.addRowChange(rowPutChange); // logger.debug("开始执行tableStore(core1)新增方法putRow", getUserIdAndPhone(solrSmsDetails)); // client.batchWriteRow(batchWriteRowRequest); // logger.info("tableStore(core1)新增方法putRow成功写入", getUserIdAndPhone(solrSmsDetails)); // return true; // } catch (Exception e) { // e.printStackTrace(); // logger.warn("tableStore(core1)新增方法putRow写入失败", getUserIdAndPhone(solrSmsDetails)); // return false; // } // } @Override public void putRow(List
                        
                          solrSmsDetails, String column) { logger.debug("进入tableStore(core1)新增方法putRow"); if (CollectionUtils.isEmpty(solrSmsDetails) || column == null || "".equals(column)) { logger.error("tableStore(core1)新增方法putRow存在空值,插入失败"); return; } BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); try { RowPutChange rowPutChange = new RowPutChange(TABLENAME, createPrimaryKey(solrSmsDetails)); rowPutChange.addColumn(new Column(column, ColumnValue.fromString(gson.toJson(solrSmsDetails)))); batchWriteRowRequest.addRowChange(rowPutChange); logger.debug("开始执行tableStore(core1)新增方法putRow", getUserIdAndPhone(solrSmsDetails)); client.batchWriteRow(batchWriteRowRequest); logger.info("tableStore(core1)新增方法putRow成功写入", getUserIdAndPhone(solrSmsDetails)); } catch (Exception e) { e.printStackTrace(); logger.warn("tableStore(core1)新增方法putRow写入失败", getUserIdAndPhone(solrSmsDetails)); } } /** * 将List2中重复数据筛出,非重复数据再装入list1 */ private List
                         
                           compare(List
                          
                            listOne, List
                           
                             listTwo) { logger.debug("tableStore(core1)添加方法add调用更新方法compare"); for (SolrSmsDetails aListTwo : listTwo) { if (listOne.contains(aListTwo)) { logger.info("当前表格存储已经存在userId={},phone={}", aListTwo.getUserId(), aListTwo.getPhone(), ",本次不再存储"); } else { //如果不存在,则将当前实体类添加到list集合中 listOne.add(aListTwo); } } return listOne; } private String getUserIdAndPhone(List
                            
                              list) { if (CollectionUtils.isEmpty(list)) { logger.warn("getUserIdAndPhone方法入参List为空"); return "getUserIdAndPhone方法入参List为空,请注意!!"; } String userId = list.get(0).getUserId().toString(); String Md5UserId = MD5Util.md5Hex(userId + ""); String phone = list.get(0).getPhone(); return " 用户ID= " + userId + " MD5之后userId= " + Md5UserId + " 电话phone= " + phone; } /** * 带TZ的时间转long */ private Long tzTimeConvert(String date) { SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //包含TZ再转 if (date.indexOf("T") > 0 && date.indexOf("Z") > 0) { //使用UTC 世界协调时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); //格林威治标准时间GMT //sdf.setTimeZone(TimeZone.getTimeZone("GMT")); try { Date date1 = sdf.parse(date); Date date2 = sdft.parse(sdft.format(date1)); return date2.getTime(); } catch (Exception e2) { e2.printStackTrace(); logger.error("tzTimeConvert时间转换失败Exception,date={}", date); return null; } } else { //正常的时间类型去做转化 try { Date date2 = sdft.parse(date); return date2.getTime(); } catch (ParseException e) { e.printStackTrace(); logger.error("tzTimeConvert时间转换失败ParseException,date={}", date); return null; } } } private String logTimeConvert(Date date) { SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH); try { Date date1 = sf1.parse(date.toString()); SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return sf2.format(date1); } catch (ParseException e) { //正常的时间类型去做转化 try { SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdft.format(date); } catch (Exception e1) { e1.printStackTrace(); logger.error("logTimeConvert时间转换失败,date={}", date); return date.toString(); } } } /** * 信数时间转换 */ private Date toCoverDate(String startTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse(startTime.replace("T", " ")); } catch (ParseException e) { try { date = sdf1.parse(startTime); } catch (ParseException e1) { try { date = sdf2.parse(startTime); } catch (ParseException e2) { logger.error("转换时间错误:{}", startTime, e2); } } } return date; } /** * 适用于批量写入操作 失败重试 * 由于当前场景仅为单行操作 暂不应用此方法 * * @param response * @param batchWriteRowRequest */ // private void isSuccess(BatchWriteRowResponse response, BatchWriteRowRequest // batchWriteRowRequest) { // if (response != null && !response.isAllSucceed()) { // for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) { // System.out.println("失败原因:" + rowResult.getError()); // } // /** // * 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试.这里只给出构造重试请求的部分. // * 推荐的重试方法是使用SDK的自定义重试策略功能, 支持对batch操作的部分行错误进行重试. 设定重试策略后, 调用接口处即不需要增加重试代码. // */ // BatchWriteRowRequest retryRequest = batchWriteRowRequest // .createRequestForRetry(response.getFailedRows()); // if (retryRequest != null && !retryRequest.isEmpty()) // client.batchWriteRow(retryRequest); // } // } /* * @Override * public void add(List
                             
                               solrSmsDetailses) { * BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest(); * BatchWriteRowResponse response = null; * PrimaryKeyBuilder pk1Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); * if (!CollectionUtils.isEmpty(solrSmsDetailses)) { * //由于每次只查一个用户+电话(userPhone为主键,只需要取List第一个即可) * Long userId = solrSmsDetailses.get(0).getUserId(); * String phone = solrSmsDetailses.get(0).getPhone(); * if (userId == null || phone == null || phone.length() == 0) { * // logger.info("用户:" + userId + "电话:" + phone + "存在空值,不予导入"); * System.out.println("用户:" + userId + "电话:" + phone + "存在空值,不予导入"); * return; * } else { * // logger.info("开始短信导入归档用户为:{},电话号码为:{}", userId, phone); * System.out.println("开始短信导入归档用户为:{}" + userId + "电话号码为:{}" + phone); * pk1Builder.addPrimaryKeyColumn("phone", PrimaryKeyValue.fromString(phone)); * pk1Builder.addPrimaryKeyColumn("userId", PrimaryKeyValue.fromLong(userId)); * RowPutChange rowPutChange = new RowPutChange(TABLENAME, pk1Builder.build()); * Gson gson = new Gson(); * rowPutChange.addColumn(new Column(COLUMNNAME, * ColumnValue.fromString(gson.toJson(solrSmsDetailses)))); * batchWriteRowRequest.addRowChange(rowPutChange); * response = client.batchWriteRow(batchWriteRowRequest); * } * } * isSuccess(response, batchWriteRowRequest); * } */ }
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
实体类
package com.mimidai.common.dto;

import com.google.gson.annotations.Expose;
import org.apache.solr.client.solrj.beans.Field;

/**
 * 短信详单
 *
 * @author liang
 */
public class SolrSmsDetails {

  @Field
  @Expose(serialize = false)
  private String id;
  @Field
  @Expose
  private Long userId;
  @Field
  @Expose
  private String phone;
  @Field
  @Expose
  private String otherPhone;
  @Field
  @Expose
  private Integer smsType;
  @Field
  @Expose
  private String date;
  @Field
  @Expose
  private Integer fee;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getOtherPhone() {
    return otherPhone;
  }

  public void setOtherPhone(String otherPhone) {
    this.otherPhone = otherPhone;
  }

  public Integer getSmsType() {
    return smsType;
  }

  public void setSmsType(Integer smsType) {
    this.smsType = smsType;
  }

  public String getDate() {
    return date;
  }

  public void setDate(String date) {
    this.date = date;
  }

  public Integer getFee() {
    return fee;
  }

  public void setFee(Integer fee) {
    this.fee = fee;
  }

  //重写equals方法
  //判断只需要该条 的 userId、phone、otherPhone、smsType相同即可判定此为相同
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
      return false;
    }
    SolrSmsDetails solrSmsDetails = (SolrSmsDetails) obj;
    if (userId != solrSmsDetails.userId) {
      return false;
    }
    if (phone != null ? !phone.equals(solrSmsDetails.phone) : solrSmsDetails.phone != null) {
      return false;
    }
    if (otherPhone != null ? !otherPhone.equals(solrSmsDetails.otherPhone)
        : solrSmsDetails.otherPhone != null) {
      return false;
    }
    if (smsType != null ? !smsType.equals(solrSmsDetails.smsType)
        : solrSmsDetails.smsType != null) {
      return false;
    }
    if (date != null ? !date.equals(solrSmsDetails.date)
        : solrSmsDetails.date != null) {
      return false;
    }
    if (fee != null ? !fee.equals(solrSmsDetails.fee) : solrSmsDetails.fee != null) {
      return false;
    }
    return true;
  }
}
网友评论