简体   繁体   English

如何将数据加载到HBase

[英]How to load data into hbase

I would like to load data into Hbase so I tried to a simple example from book HBASE: Definitive guide. 我想将数据加载到Hbase中,因此我尝试了本书HBASE:权威指南中的一个简单示例。 HbaseHelper.java was loaded. HbaseHelper.java已加载。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
// ^^ PutExample
import util.HBaseHelper; 
// vv PutExample

import java.io.IOException;

public class PutExample {

  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create(); // co PutExample-1-CreateConf Create the required configuration.

    // ^^ PutExample
    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable1");
    helper.createTable("testtable1", "colfam1");
    // vv PutExample
    HTable table = new HTable(conf, "testtable1"); // co PutExample-2-NewTable Instantiate a new client.

    Put put = new Put(Bytes.toBytes("row1")); // co PutExample-3-NewPut Create put with specific row.

    put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
      Bytes.toBytes("val1")); // co PutExample-4-AddCol1 Add a column, whose name is "colfam1:qual1", to the put.
    put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
      Bytes.toBytes("val2")); // co PutExample-4-AddCol2 Add another column, whose name is "colfam1:qual2", to the put.

    table.put(put); // co PutExample-5-DoPut Store row with column into the HBase table.
  }
}

I did javac -classpath hbase-version.jar:hadoop.jar:zookeeper.jar:log4j.jar:commons-logging.jar:commons-lang.jar PutData.java (they are in a same directory) but it cannot be complied successfully. 我做了javac -classpath hbase-version.jar:hadoop.jar:zookeeper.jar:log4j.jar:commons-logging.jar:commons-lang.jar PutData.java(它们位于同一目录中),但无法遵从成功。 It is said: 据说:

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I tried some other similar method to put data. 我尝试了其他类似的方法来放置数据。 The errors are same. 错误是相同的。 How can I do it in any possible way? 如何以任何可能的方式做到这一点?

try this code, first add hbase/conf to eclipse -- in java built path-->source-->add link-->your hbase conf directory 试试这个代码,首先将hbase / conf添加到eclipse-在Java构建的路径中->源->添加链接->您的hbase conf目录

import java.io.IOException; 导入java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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 insertDataManually
{
 public static void main(String[] args) throws IOException
 {
  org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();

  HTable table = new HTable(config, "car");
  Put p = new Put(Bytes.toBytes("row1"));
  p.add(Bytes.toBytes("vi"), Bytes.toBytes("make"),Bytes.toBytes("bmw"));
  p.add(Bytes.toBytes("vi"),Bytes.toBytes("model"),Bytes.toBytes("2012"));
  table.put(p);
  Get g = new Get(Bytes.toBytes("row1"));
  Result r = table.get(g);
  byte [] value = r.getValue(Bytes.toBytes("vi"),Bytes.toBytes("make"));
  byte [] value1 = r.getValue(Bytes.toBytes("vi"),Bytes.toBytes("model"));
  String valueStr = Bytes.toString(value);
  String valueStr1 = Bytes.toString(value1);
  System.out.println("GET: " +"make: "+ valueStr+"   model: "+valueStr1);
  Scan s = new Scan();
  s.addColumn(Bytes.toBytes("vi"), Bytes.toBytes("make"));
  s.addColumn(Bytes.toBytes("vi"), Bytes.toBytes("model"));
  ResultScanner scanner = table.getScanner(s);
  try
  {
   for (Result rr = scanner.next(); rr != null; rr = scanner.next())
   {
    System.out.println("Found row : " + rr);
   }
  } finally
  {
   // Make sure you close your scanners when you are done!
   scanner.close();
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM