java从本地读文件并上传Hbase

来源:互联网 发布:linux gzip压缩命令 编辑:程序博客网 时间:2024/06/07 20:20

有兴趣使用的请点下载链接
数据文件与源代码下载

1 从本地文件读数据,按行操作

String filePath = "/root/input_2";File file=new File(filePath);InputStreamReader in_stream = new    InputStreamReader(new FileInputStream(file));  BufferedReader in = new BufferedReader(in_stream);String s;while ((s=in.readLine())!=null ) {    System.out.println(s);}

2 Hbase 创建表并put

public class HBaseTest {public static void main(String[] args) throws MasterNotRunningException,ZooKeeperConnectionException, IOException {    // create table descriptor    String tableName= "mytable";    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));    // create column descriptor    HColumnDescriptor cf = new HColumnDescriptor("mycf");    htd.addFamily(cf);    // configure HBase    Configuration configuration = HBaseConfiguration.create();    HBaseAdmin hAdmin = new HBaseAdmin(configuration);    hAdmin.createTable(htd);    hAdmin.close();    // put "mytable","abc","mycf:a","789"    HTable table = new HTable(configuration,tableName);    Put put = new Put("abc".getBytes());    put.add("mycf".getBytes(),"a".getBytes(),"789".getBytes());    table.put(put);    table.close();    System.out.println("put successfully");}}

3 hbase shell 表示

create 'mytable', 'mycf’创建表, column family#put 'mytable', 'abc', 'mycf:a', '123'0 row(s) in 0.0580 seconds#put 'mytable', 'def', 'mycf:b', '456'0 row(s) in 0.0060 seconds#scan 'mytable'ROW COLUMN+CELLabc column=mycf:a, timestamp=1427731972925, value=123def column=mycf:b, time

数据文件

示例(取第一行):
YhzBzMbflE zfsNGv 172.544

  • 本数据文件以空格隔开
  • 取第一列为key,取第二列为value,第三列不使用
    数据文件截图:
    数据文件截图
    综上,从本地文件读取数据,按字符流处理文件,并按行上传key,value到hbase.
    表名tableName = “HB_MEM_”
    colum family=”cf”
    对于示例数据(YhzBzMbflE zfsNGv 172.544)
    put ‘HB_MEM_’, ‘YhzBzMbflE ‘, ‘cf:value, ‘zfsNGv ’
    用如下代码实现:
/** * Created by Jiao on 2017/4/3. */import java.io.IOException;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.net.URI;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.text.NumberFormat;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTablePool;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Put;public class project {    public static void main(String[] args) throws IOException {        //writeback to hbase        Configuration HBASE_CONFIG = new Configuration();//      HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.0.104");        String tableName = "HB_MEM_";        String family="cf";        HBaseAdmin hBaseAdmin = new HBaseAdmin(HBASE_CONFIG);        if (hBaseAdmin.tableExists(tableName)) { //check            hBaseAdmin.disableTable(tableName);            hBaseAdmin.deleteTable(tableName);            System.out.println(tableName + " is exist,detele....");        }        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));        HColumnDescriptor cf= new HColumnDescriptor(family);        htd.addFamily(cf);        hBaseAdmin.createTable(htd);        hBaseAdmin.close();        HTable HBasetable = new HTable(HBASE_CONFIG,TableName.valueOf(tableName));        String filePath = "/root/input_2";        File file=new File(filePath);        InputStreamReader in_stream = new InputStreamReader(new FileInputStream(file));          BufferedReader in = new BufferedReader(in_stream);        String s;        int i=0;        while ((s=in.readLine())!=null ) {            String[] words = s.split(" ");            String key = words[0];        String value=words[1];        Put put = new Put(key.getBytes());            put.add(family.getBytes(), "value".getBytes(), value.getBytes());            System.out.println("Save to Hbase! key:"+key+" "+"value:"+value);            HBasetable.put(put);        }        HBasetable.close();        System.out.println("put successful!!!");    }}

4 验证是否存入Hbase:

在hadoop hbase已经安装成功的情况下:
1. 命令行输入以下命令,打开hbase shell 再用list命令列出文件列表

#hbase shell
#list

hbase shell列出所存储的表

2.浏览器打开相应ip的60010端口,Hbase UI

浏览器输入:http://104.128.92.12:60010/
此处104.128.91.12是所使用的IP号,60010是端口

这里写图片描述

阅读全文
0 0
原创粉丝点击