0

我正在使用 HBase 存储数据,但后来为了满足我的要求,我想将数据从 HBase 导出到 RDBM,如 mysql 或 postgres。我该怎么做?我通过这种方式尝试过,但找不到解决方案。请帮我。

这是我的示例代码:

/** 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;

    }



}
4

2 回答 2

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")));
}
于 2012-07-25T02:58:17.873 回答
0

使用 sqoop 将数据从 HBase 移动到数据库。

于 2012-07-26T06:12:45.250 回答