【问题标题】:Getting Started with Avro [closed]Avro 入门 [关闭]
【发布时间】:2011-07-25 17:12:59
【问题描述】:

我想开始使用 Avro 和 Map Reduce。有人可以建议一个好的教程/示例来开始。我通过互联网搜索找不到太多内容。

【问题讨论】:

    标签: mapreduce avro


    【解决方案1】:

    Javascript + jQuery:https://codepen.io/GentjanLikaj/pen/dyPXLXX

    $( "#btn1" ).click(function() {
        $( "table" ).remove();
        var text1 = document.getElementById('text').value;
        var text1=text1.replace(/(\r\n|\n|\r)/gm,"");
        var sep = document.getElementById('sep').value;
        var aray = text1.split(sep);
        var formato = '<select class="form"><option value="string">string</option><option value="int">int</option><option value="date">date</option><option value="datetime">datetime</option><option value="float">float</option><option value="varchar(50)">varchar</option></select>';
        var univoco = '<input id="checkbox" type="checkbox" name="univoco" value="true" > <br>';
        var null1 = '<input id="null" class="null11" type="checkbox" name="univoco" value="null" checked> <br>';
        var html = '<table id="tbl" class="table table-sm  table-striped table-centered .thead-dark "><thead class="thead-dark"><tr class="table-primary"><th scope="col">ColumnName</th><th scope="col">Format</th>  <th scope="col">Null</th>   </tr>  </thead> <tbody>';
        $.each(aray, function(i, val) {
            html += '<tr><td id = "val">' + val + '</td><td id = "form">' + formato + '</td><td id = "nul">' + null1 + '</td></tr>';
        });
        html += '</tbody></table>'; 
        $("#table").append(html);
    });
    
    $( "#btn2").click(function() {
        $( "#avro" ).empty();
        var myRows = {myRows:[]};
        var $th = $('#tbl th');
        $('#tbl tbody tr').each(function(i, tr){
            var obj = {}, 
            $tds = $(tr).find('td');
            $td1 = $(tr).find('select.form').children("option:selected");
            $tds2 = $(tr).find('#checkbox');
            $tds3 = $(tr).find('#null');
            $th.each(function(){
                obj['name'] = $tds.eq(0).text();
                var type=$td1.eq(0).val();
                var nullv=$tds3.eq(0).is(':checked');
                if (type=='date') {
                    type={type:"int", logicalType:"date"};
                }else if (nullv == true) {
                    type = [type ,'null' ];
                }
                else{
                    type;
                }
                obj['type'] = type;
                // obj['univoco'] = $tds2.eq(0).is(':checked');
                // obj['null'] = $tds3.eq(0).is(':checked');
            });
            myRows.myRows.push(obj);        
        }); 
        console.log(JSON.stringify(myRows));
        var header = '{ <br> &nbsp &nbsp "type": "record"  ,  <br>   &nbsp &nbsp "namespace": "Mezzora" ,  <br>  &nbsp &nbsp "name": "ReportDSL" ,  <br>   &nbsp &nbsp  "fields": [ <br>';
        $('#avro').append(header);
        text ='&nbsp &nbsp &nbsp &nbsp &nbsp '+JSON.stringify(myRows.myRows[0])  +"<br>";
        $('#avro').append(text);
        var i;
        for (i = 1; i < myRows.myRows.length; i++) {
            text ='&nbsp &nbsp &nbsp &nbsp &nbsp  ,'+ JSON.stringify(myRows.myRows[i])  +"<br>";
            $('#avro').append(text);
        }
        var footer = '&nbsp &nbsp &nbsp &nbsp  ] <br> }';
        $('#avro').append(footer);  
    });
    
    $( "#btn3").click(function() {
        $( "#sql" ).empty();
        var myRows = {myRows:[]};
        var $th = $('#tbl th');
        $('#tbl tbody tr').each(function(i, tr){
            var obj = {}, 
            $tds = $(tr).find('td');
            $td1 = $(tr).find('select.form').children("option:selected");
            $tds2 = $(tr).find('#checkbox');
            $tds3 = $(tr).find('#null');
            $th.each(function(){
                obj['name'] = $tds.eq(0).text();
                var type=$td1.eq(0).val();
                var nullv=$tds3.eq(0).is(':checked');
                if (nullv == false) {
                    type= type +' not null';
                }else if (nullv == true) {
                    type = type +' null' ;
                }else{
                    if (type == 'string') {
                        type = 'varchar(50)';
                    }else{
                        type;
                    }
                }           
                obj['type'] = type;
                // obj['univoco'] = $tds2.eq(0).is(':checked');
                // obj['null'] = $tds3.eq(0).is(':checked');
            });
            myRows.myRows.push(obj);        
        }); 
        console.log(JSON.stringify(myRows));
        var header = 'CREATE TABLE [schema].[tblName] &nbsp ( <br>';
        $('#sql').append(header);
        var i;
        for (i = 0; i < myRows.myRows.length; i++) {
            text ='&nbsp &nbsp &nbsp &nbsp &nbsp '+ JSON.stringify(myRows.myRows[i].name).replace('\"',' ').replace('\"',' ') +JSON.stringify(myRows.myRows[i].type).replace('\"',' ').replace('\"',' ')   +", <br>";
            $('#sql').append(text);
        }
        var footer = ');';
        $('#sql').append(footer);   
    });
    .parent {
        display: grid;
        grid-template-columns: repeat(10, 1fr);
        grid-template-rows: repeat(12, 1fr);
        grid-column-gap: 20px;
        grid-row-gap: 0px;
        }
        
        .div1 { grid-area: 1 / 2 / 2 / 11; }
        .div2 { grid-area: 5 / 6 / 6 / 7; }
        .div3 { grid-area: 3 / 1 / 5 / 12; }
        .div4 { grid-area: 2 / 1 / 3 / 2; }
        .div5 { grid-area: 6 / 1 / 13 / 4; }
        .div6 { grid-area: 6 / 4 / 13 / 8; }
        .div7 { grid-area: 6 / 8 / 13 / 12; }
        
        .div6 , .div7{
    
            border: 1px solid black;
            margin-right: 10px;
        }
    
        #btn1{
            margin-bottom: 30px;
        }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title></title>
      <script  src="https://code.jquery.com/jquery-3.4.1.js"  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="   crossorigin="anonymous"> </script>
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    </head>
    <body>
        <h2>Insert the text and the separator and press the button: </h2>
        <div class="parent">
            <div class="div1"></div>
            <div class="div2 btn btn-alert"></div>
            <div class="div3">
          <textarea name="text" id="text" cols="120" rows="4" placeholder="ColumnName1,ColumName2,ColumName3,ColumName4,............"></textarea>
          <input type="button"  value="Convert to Table" id="btn1" class="btn btn-primary"> 
        </div>
            <div class="div4">Separatetor<input type="text" name="sep" id="sep" value=","> </div>
            <div class="div5">
                <input type="button" value="Convert to Avro" class="btn btn-success" id="btn2">
                <input type="button" value="Convert to SQL Create" class="btn btn-info" id="btn3">
                <div id="table"></div>
            </div>
            <div class="div6 card">
                <div class="card-header font-weight-bolder">Avro Format</div>
                <div class="card-body" id='avro'>
                </div>
            </div>
            <div class="div7 card">
                <div class="card-header font-weight-bolder">SQL Create</div>
                <div class="card-body" id='sql'>
                </div>
            </div>
        </div>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      https://github.com/apache/avro/blob/trunk/lang/java/mapredavro 源代码有例子。例如TestReflectJob 帮助我使用预定义的域对象编写 map-reduce 作业

      【讨论】:

        【解决方案3】:

        其他链接:

        我在文档中看到的主要问题(几乎不存在)是它侧重于非常费力的“通用”方法;这看起来很奇怪,因为它结合了两全其美——您仍然必须为数据提供完整的模式,但不能从静态类型等中获益。 自动代码生成更方便,但覆盖面较差。

        【讨论】:

          【解决方案4】:

          我最近做了一个严重基于 Avro 数据的项目,之前没有使用过这种数据格式,我不得不从头开始。你是对的,在开始使用 Avro 时很难从在线资源中获得很多帮助。我向您推荐的材料是:

          • 到目前为止,我找到的最有用的资源是 Tom White 的 Hadoop: The Definitive Guide 书中的 Avro 部分 (p103-p116) 以及他在书中使用的代码的 Github page
          • 有关其他代码示例,我查看了 Ron Bodkin 的 Github 页面 avro-mr-sample
          • 就我而言,我使用 Python 来读取和写入 Avro 文件,为此我使用了 tutorial
          • 尽管很明显,我还是将链接添加到Avro Users mailing list。在那里可以找到大量信息,在我阅读了上述材料并实现了一堆代码之后,我发现自己花了几个小时浏览档案。

          最后,我给你的最后一个建议是使用 Avro 1.4.1 和 Hadoop 0.20.2 并且仅使用这种组合。我在使用 Hadoop 0.21 和更新的 Avro 版本运行代码时遇到了一些重大问题。

          【讨论】:

          • 我要补充一点,关于 Avro 的部分在 Hadoop: The Definitive Guide Second Edition 中。我的第一版严重不足。
          • @RichardClayton 截至目前,最新的第 4 版相当不错。它比 Avro 文档中的(几乎没用的)“Hadoop MapReduce 指南”更详细。请注意,这本书本身不需要购买 - 可以订阅 Safari Books,甚至可以试用。
          猜你喜欢
          • 2011-02-28
          • 2023-03-04
          • 2012-08-11
          • 2012-08-12
          • 2010-09-26
          • 2014-06-29
          • 2011-03-25
          • 2011-03-15
          • 2014-02-14
          相关资源
          最近更新 更多