我正在使用HBase存储数据,但后来为了满足我的要求,我想将数据从HBase导出到RDBM,如mysql或postgres。 我该怎么做?我通过这种方式尝试,但canot找到解决方案。请帮帮我。
这是我的示例代码:
/** my HbaseRawDataReader class **/
public class HbaseRawDataReader {
List<KeyValue> list = new ArrayList<KeyValue>();
static Configuration config = HBaseConfiguration.create();
public RadarTrack insertDBvo=new RadarTrack();// pojo class
public static void main(String args[])
{
HbaseRawDataReader hr=new HbaseRawDataReader();
// hr.getAllRecord(key, value);
}
public void getAllRecord () {
try{
// This instantiates an HTable object that connects you to the table trackrecord_table
HTable table = new HTable(config, "trackrecord_table");
Scan s = new Scan();
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("IpAdderss"));
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("Trackid"));
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("Latitude"));
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("Longitude"));
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("Velocity"));
s.addColumn(Bytes.toBytes("radar"), Bytes.toBytes("Course"));
ResultScanner rs = table.getScanner(s);
for(Result r:rs){
for(KeyValue vo : r.raw()){
// System.out.print(new String(vo.getRow()) + " ");
// System.out.print(new String(vo.getFamily()) + ":");
// System.out.print(new String(vo.getQualifier()) + " ");
// System.out.print(vo.getTimestamp() + " ");
// System.out.println(new String(vo.getValue()));
String IPAddress = vo.getQualifier().toString();
String track_ID = vo.getQualifier().toString();
String latitude = vo.getQualifier().toString();
String longitude = vo.getQualifier().toString();
String sog = vo.getQualifier().toString();
String cog = vo.getQualifier().toString();
insertDBvo.setDirection(Double.parseDouble(cog));
insertDBvo.setIpAddress(IPAddress);
insertDBvo.setLatitude(latitude);
insertDBvo.setLongitude(longitude);
insertDBvo.setVelocity(Double.parseDouble(sog));
insertDBvo.setTrackId(Integer.parseInt(track_ID));
CsdssDAOImpl.getInstance().insertToDB(insertDBvo);
}
}
} catch (IOException e){
e.printStackTrace();
}
}
}
/** for postgres connection***/
public class ConnectionUtil {
static Properties properties = new Properties();
static Connection connection = null;
public static Connection getConncection() {
if(null==connection){
// if(properties.isEmpty()){
try {
properties.load(ConnectionUtil.class.getClassLoader()
.getResourceAsStream("radar.properties"));
} catch (IOException e) {
e.printStackTrace();
}
// }
String driverName = properties.getProperty("drivers");
String url = properties.getProperty("url");
String userName = properties.getProperty("userName");
String password = properties.getProperty("password");
// Connection connection = null;
try {
// DriverManager
// driverManager=(DriverManager)Class.forName(properties.getProperty("driverName")).newInstance();
Class.forName(driverName);
connection = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
}
答案 0 :(得分:0)
尝试这样的事情
HTable table = new HTable(config, "trackrecord_table");
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for(Result r : rs) {
NavigableMap<byte[], byte[]> familyMap = r.getFamilyMap(Bytes.toBytes("radar");
String ipAdress = Bytes.toString(familyMap.get(Bytes.toBytes("IpAdderss")));
}
答案 1 :(得分:0)
使用sqoop将数据从HBase移动到数据库。