配置hadoop location同eclipse hadoop开发。
创建mapreduce project项目,build path引入HBase(同eclipse中HBase开发)和mysql(mysql connection)的jar包。
右键新建class类,编辑代码:
package dfs;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class hbase {
private static HTable connectHBase(String tablename) throws IOException{
HTable table=null;
Configuration conf=HBaseConfiguration.create();
table=new HTable(conf,tablename);
return table;
}
private static Connection connectDB() throws Exception {
String userName="root";
String password="12345";
String url="jdbc:mysql://127.0.0.1/test";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url,userName,password);
return conn;
}
public static void main(String[] args){
Connection dbConn=null;
HTable htable=null;
Statement stmt=null;
String query="select * from one";
try{
dbConn=connectDB();
htable=connectHBase("test");
byte[] family=Bytes.toBytes("n");
stmt=dbConn.createStatement();
ResultSet rs=stmt.executeQuery(query);
long ts=System.currentTimeMillis();
while(rs.next()){
String id=rs.getString("id");
String name=rs.getString("name");
String city=rs.getString("city");
String rowkey=id+name+city;
Put p=new Put(Bytes.toBytes(rowkey));
for(int i=4;i<=6;i++){
String columnI="v"+i;
String valueI=rs.getString(i);
p.add(family,Bytes.toBytes(columnI),ts,Bytes.toBytes(valueI));
}
htable.put(p);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt !=null){
stmt.close();
}
if(dbConn !=null){
dbConn.close();
}
if(htable!=null){
htable.close();
}
}catch(Exception e){
}
}
}
}
保存,右键.java,run as ——>java application.
在终端查看Mysql和Hbase中的test表内容。
mysql:
迁移后的hbase: