mysql =>hbase put方式

  public final static String FAMILY="cf01";
public static void main(String[] args) throws Exception {
Configuration config=new Configuration();
//设置端口号
config.set("hbase.zookeeper.property.clientPort","2181");
//设置zookeeper集群地址
config.set("hbase.zookeeper.quorum","172.18.76.168,172.18.76.169,172.18.76.170");
Connection connection = ConnectionFactory.createConnection(config);
//建立连接
Connection conn= ConnectionFactory.createConnection(config);
MySqlToHBase mh=new MySqlToHBase();
java.sql.Connection dbConn=null;
Statement st=null;
//sql语句
String strQuery="select * from payorder limit 10";
;
dbConn=mh.connectDB();//连接mysql
//找到表
Table tableName =conn.getTable(TableName.valueOf("t7"));
List<Put> list=new ArrayList<Put>();
Put put=null;
try{
st= (Statement) dbConn.createStatement();
ResultSet rs=st.executeQuery(strQuery);
long beginTime=System.currentTimeMillis();//开始
System.out.println( "beginTime:---"+beginTime);
while (rs.next()) {
String id=rs.getString("id");
Integer paytype = rs.getInt("paytype");
Double paymoney = rs.getDouble("paymoney");
String applytime = rs.getString("applytime");
String paytime = rs.getString("paytime");
Integer paystate = rs.getInt("paystate");
String payid=rs.getString("payid");
String tradeno=rs.getString("tradeno");
String canceltime = rs.getString("canceltime");
String userid=rs.getString("userid");
String openid=rs.getString("openid");
String remark=rs.getString("remark");
String alisign=rs.getString("alisign");
String alierrormsg=rs.getString("alierrormsg");
String wxerrormsg=rs.getString("wxerrormsg");
String alipayaccount=rs.getString("alipayaccount");
Boolean isrefund=rs.getBoolean("isrefund");
Boolean isend=rs.getBoolean("isend");
String ctype=rs.getString("ctype");
String tztime = rs.getString("tztime");
String auth_code=rs.getString("auth_code");
String cashierid=rs.getString("cashierid");
Integer ordertype=rs.getInt("ordertype");
String desknum=rs.getString("desknum");
String attach = rs.getString("attach");
String mid=rs.getString("mid");
String sub_openid=rs.getString("sub_openid");
Integer coupon_fee=rs.getInt("coupon_fee");
String aliuerid=rs.getString("aliuerid");
String paytime_gf = rs.getString("paytime_gf");
String paytime_bj = rs.getString("paytime_bj");
String merchantid = rs.getString("merchantid");
String yipayreqno=rs.getString("yipayreqno");
String yierrormsg=rs.getString("yierrormsg");
Double remoney = rs.getDouble("remoney");
Double realmoney = rs.getDouble("realmoney");
String remoneystr=rs.getString("remoneystr");
String vipinfoid=rs.getString("vipinfoid");
String callback=rs.getString("callback");
String uniqueid=rs.getString("uniqueid");
Integer lamic_coupon_fee=rs.getInt("lamic_coupon_fee");
String couponCode=rs.getString("couponCode");
Integer sub_type=rs.getInt("sub_type");
//rowkey的拼接
String rowKey = id+"_"+applytime;//作为行健
System.out.println(rowKey);
put = new Put(Bytes.toBytes(rowKey));
//数据的写入
if(id!=null&&id!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("id"), Bytes.toBytes(id));
}
if(paytype!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paytype"), Bytes.toBytes(paytype));
}
if(paymoney!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paymoney"), Bytes.toBytes(paymoney));
}
if(applytime!=null&&applytime!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("applytime"), Bytes.toBytes(applytime));
}
if(paytime!=null&&paytime!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paytime"), Bytes.toBytes(paytime));
}

if(paystate!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paystate"), Bytes.toBytes(paystate));
}
if(payid!=null&&payid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("payid"), Bytes.toBytes(payid));
}
if(tradeno!=null && tradeno!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("tradeno"), Bytes.toBytes(tradeno));
}
if(canceltime!=null && canceltime!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("canceltime"), Bytes.toBytes(canceltime));
}
if(userid!=null && userid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("userid"), Bytes.toBytes(userid));
}
if(openid!=null && openid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("openid"), Bytes.toBytes(openid));
}
if(remark!=null && remark!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("remark"), Bytes.toBytes(remark));
}
if(alisign!=null &&alisign!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("alisign"), Bytes.toBytes(alisign));
}
if(alierrormsg!=null &&alierrormsg!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("alierrormsg"), Bytes.toBytes(alierrormsg));
}
if(wxerrormsg!=null &&wxerrormsg!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("wxerrormsg"), Bytes.toBytes(wxerrormsg));
}
if(alipayaccount!=null &&alipayaccount!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("alipayaccount"), Bytes.toBytes(alipayaccount));
}
if(isrefund!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("isrefund"), Bytes.toBytes(isrefund));
}
if(isend!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("isend"), Bytes.toBytes(isend));
}
if(ctype!=null&&ctype!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("ctype"), Bytes.toBytes(ctype));
}
if(tztime!=null&&tztime!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("tztime"), Bytes.toBytes(tztime));
}
if(auth_code!=null&&auth_code!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("auth_code"), Bytes.toBytes(auth_code));
}
if(cashierid!=null&&cashierid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("cashierid"), Bytes.toBytes(cashierid));
}
if(ordertype!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("ordertype"), Bytes.toBytes(ordertype));
}
if(desknum!=null&&desknum!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("desknum"), Bytes.toBytes(desknum));
}
if(attach!=null&&attach!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("attach"), Bytes.toBytes(attach));
}
if(mid!=null&&mid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("mid"), Bytes.toBytes(mid));
}
if(sub_openid!=null&&sub_openid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("sub_openid"), Bytes.toBytes(sub_openid));
}
if(coupon_fee!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("coupon_fee"), Bytes.toBytes(coupon_fee));
}
if(aliuerid!=null&&aliuerid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("aliuerid"), Bytes.toBytes(aliuerid));
}
if(paytime_gf!=null&&paytime_gf!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paytime_gf"), Bytes.toBytes(paytime_gf));
}
if(paytime_bj!=null&&paytime_bj!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("paytime_bj"), Bytes.toBytes(paytime_bj));
}
if(merchantid!=null&&merchantid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("merchantid"), Bytes.toBytes(merchantid));
}
if(yipayreqno!=null&&yipayreqno!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("yipayreqno"), Bytes.toBytes(yipayreqno));
}
if(yierrormsg!=null&&yierrormsg!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("yierrormsg"), Bytes.toBytes(yierrormsg));
}
if(remoney!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("remoney"), Bytes.toBytes(remoney));
}
if(realmoney!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("realmoney"), Bytes.toBytes(realmoney));
}
if(remoneystr!=null&&remoneystr!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("remoneystr"), Bytes.toBytes(remoneystr));
}
if(vipinfoid!=null&&vipinfoid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("vipinfoid"), Bytes.toBytes(vipinfoid));
}
if(callback!=null&&callback!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("callback"), Bytes.toBytes(callback));
}

if(uniqueid!=null&&uniqueid!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("uniqueid"), Bytes.toBytes(uniqueid));
}
if(lamic_coupon_fee!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("lamic_coupon_fee"), Bytes.toBytes(lamic_coupon_fee));
}
if(couponCode!=null&&couponCode!=""){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("couponCode"), Bytes.toBytes(couponCode));
}
if(sub_type!=null){
put.addColumn( Bytes.toBytes("cf01"), Bytes.toBytes("sub_type"), Bytes.toBytes(sub_type));
}
list.add(put);
}
tableName.put(list);

}catch (Exception e){
e.printStackTrace();
}
finally {
try{
if(st!=null){
st.close();
}
if(dbConn !=null){
dbConn.close();
}
if(tableName!=null){
tableName.close();
}
long endTime=System.currentTimeMillis();//开始
System.out.println( "endTime:---"+endTime);
}catch(Exception e){
e.printStackTrace();
}
}

}


/**
* 获取mysql连接
* @return
* @throws Exception
*/
/* private static java.sql.Connection connectDB() throws Exception{
String userName="root";
String password="Root@lamic2017";
String url="jdbc:mysql://172.18.42.233:3306/wypay?useUnicode=true&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn= DriverManager.getConnection(url,userName,password);
return conn;
}*/
private static java.sql.Connection connectDB() throws Exception{
String userName="qa_r";
String password="lamic2017@123456";
String url="jdbc:mysql://rm-wz9pxw655nnj4j1t4.mysql.rds.aliyuncs.com/tmpay?useUnicode=true&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn= DriverManager.getConnection(url,userName,password);
return conn;
}

猜你喜欢

转载自www.cnblogs.com/henyu/p/9025874.html