如何在DatastoreIO.v1.write中提供种类名称

时间:2018-07-04 13:56:24

标签: google-cloud-dataflow

我正在尝试从Google数据存储区中的一种类型进行读取,并应用一些转换并写回另一种类型。我正在使用谷歌数据流来实现这一目标。从数据存储区读取数据时,我们可以给出Kind。但是在写作时无法给予善意。如何实现这一目标。

1 个答案:

答案 0 :(得分:3)

编辑:糟糕,我刚刚注意到您要使用Java。 WriteToDatastore为我着眼于CreateEntityFn的Java等效物,在这种情况下,您必须在上一步中设置您的实体(包括种类)。在此示例中签出import apache_beam from apache_beam.io.gcp.datastore.v1.datastoreio import WriteToDatastore from google.cloud.proto.datastore.v1 import entity_pb2 from googledatastore import helper class MakeEntity(object): def __init__(self, project): self._project = project def make(self, element): try: entity = entity_pb2.Entity() helper.add_key_path(entity.key, 'EntityKind', element['id']) helper.add_properties(entity, { "created": datetime.datetime.now(), "email": unicode(element['email'], "count": int(element['count'], "amount": float(element['amount'], }) return entity except: logging.error(traceback.format_exc()) raise def build_pipeline(project, pipeline_options): p = apache_beam.Pipeline(options=pipeline_options) _ = \ (p # other transforms | 'create entity' >> apache_beam.Map(MakeEntity(project=project).make) | 'write to datastore' >> WriteToDatastore(project=project)) return p https://github.com/mbrukman/apache-beam/blob/master/examples/java/src/main/java/org/apache/beam/examples/cookbook/DatastoreWordCount.java#L197

原始:

这就是我的做法

public class ModifyEntityKindFn extends DoFn<Entity, Entity> { 

    @ProcessElement 
    public void processElement(ProcessContext context) { 

        Key.Builder keyBuilder = makeKey(NEW_KIND, inputEntity.getKey());
        keyBuilder.getPartitionIdBuilder().setNamespaceId(NEW_NAMESPACE); 
        Entity.Builder entityBuilder = Entity.newBuilder().setKey(keyBuilder.build()); 
        entityBuilder.getMutableProperties().put("content", makeValue(context.element()).build());
        context.output(entityBuilder.build()); 

    } 

} 

编辑#2::我调整了您的代码,使其更接近遵循我链接到的示例。希望这行得通

class WelcomeActivity: AppCompatActivity()
{
    private lateinit var viewPager: ViewPager
    private lateinit var myViewPagerAdapter: MyViewPagerAdapter
    private lateinit var dotsLayout: LinearLayout
    private lateinit var dots: Array<TextView>
    private lateinit var layouts: IntArray
    private lateinit var btnSkip: Button
    private lateinit var btnNext: Button

    val viewPagerPageChangeListener = object : ViewPager.OnPageChangeListener{

        override fun onPageSelected(position: Int) {
            addBottomDots(position)

            // changing the next button text 'NEXT' / 'GOT IT'
            if (position == layouts.size - 1) {
                // last page. make button text to GOT IT
                btnNext.text = "GOT IT"
                btnSkip.visibility = View.GONE
            } else {
                // still pages are left
                btnNext.text = "NEXT"
                btnSkip.visibility = View.VISIBLE
            }
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            TODO("not implemented")
        }

        override fun onPageScrollStateChanged(state: Int) {
            TODO("not implemented")
        }
    }
    private lateinit var prefManager: PrefManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Checking for first time launch - before calling setContentView()
        prefManager = PrefManager(this)
        if(!prefManager.isFirstTimeLaunch()){
            launchHomeScreen()
            finish()
        }

        if(Build.VERSION.SDK_INT >= 21){
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }

        setContentView(R.layout.activity_intro)

        viewPager = view_pager
        dotsLayout = layoutDots
        btnSkip = btn_skip
        btnNext = btn_next

        // layouts of all welcome sliders
        // add few more layouts if you want
        layouts = intArrayOf(
                R.layout.introslide1,
                R.layout.introslide2,
                R.layout.introslide3
        )

        // adding bottom dots
        addBottomDots(0)

        // making notification bar transparent
        changeStatusBarColor()

        myViewPagerAdapter = MyViewPagerAdapter()
        viewPager.adapter = myViewPagerAdapter
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener)

        btnSkip.setOnClickListener {launchHomeScreen()}

        btnNext.setOnClickListener {
            // checking for last page
            // if last page home screen will be launched
            val current: Int = getItem(+1)
            if (current < layouts.size) {
                // move to next screen
                viewPager.currentItem = current
            } else {
                launchHomeScreen()
            }
        }
    }

    private fun addBottomDots(currentPage: Int){

        dots = Array(layouts.size, {TextView(this)})

        val colorsActive = resources.getIntArray(R.array.array_dot_active)
        val colorsInactive = resources.getIntArray(R.array.array_dot_inactive)

        dotsLayout.removeAllViews()
        for(i in 0 until dots.size)
        {
            dots[i] = TextView(this)
            dots[i].setText(Html.fromHtml("&#8226;")) // this function (Html.fromHtml) are deprecated
            dots[i].textSize = 35.toFloat()
            dots[i].setTextColor(colorsInactive[currentPage])
            dotsLayout.addView(dots[i])
        }

        if(dots.size > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage])

    }

    private fun getItem(i: Int): Int{
        return viewPager.currentItem + i
    }

    private fun launchHomeScreen(){
        prefManager.setFirstTimeLaunch(false)
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    /**
     * Making notification bar transparent
     */
    private fun changeStatusBarColor(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            this.window.statusBarColor = Color.TRANSPARENT
        }
    }

    /**
     * View pager adapter
     */
    inner class MyViewPagerAdapter: PagerAdapter() {

        private lateinit var layoutInflater: LayoutInflater

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = LayoutInflater.from(container.context)

            val view: View = layoutInflater.inflate(layouts[position], container, false)
            container.addView(view)

            return view
        }

        override fun getCount(): Int{
            return layouts.size
        }

        override fun isViewFromObject(view: View, obj: Any): Boolean {
            return view == obj
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            val view: View = `object` as View
            container.removeView(view)
        }
    }
}