数据读写操作

  • 如何利用MapReduce框架实现HBase的数据读写操作?

    ``java,import java.io.IOException;,import org.apache.hadoop.conf.Configuration;,import org.apache.hadoop.hbase.HBaseConfiguration;,import org.apache.hadoop.hbase.TableName;,import org.apache.hadoop.hbase.client.Connection;,import org.apache.hadoop.hbase.client.ConnectionFactory;,import org.apache.hadoop.hbase.client.Get;,import org.apache.hadoop.hbase.client.Put;,import org.apache.hadoop.hbase.client.Table;,import org.apache.hadoop.hbase.util.Bytes;,,public class HBaseExample {, public static void main(String[] args) throws IOException {, Configuration config = HBaseConfiguration.create();, Connection connection = ConnectionFactory.createConnection(config);, Table table = connection.getTable(TableName.valueOf("test"));,, // 写入数据, Put put = new Put(Bytes.toBytes("row1"));, put.addColumn(Bytes.toBytes("col1"), Bytes.toBytes("qual1"), Bytes.toBytes("value1"));, table.put(put);,, // 读取数据, Get get = new Get(Bytes.toBytes("row1"));, Result result = table.get(get);, byte[] value = result.getValue(Bytes.toBytes("col1"), Bytes.toBytes("qual1"));, System.out.println("Value: " + Bytes.toString(value));,, table.close();, connection.close();, },},``,,这个程序首先连接到HBase,然后向表"test"中插入一行数据,接着从表中读取该行数据并打印出来。

    2024年8月17日
    031
免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入